2017-10-04 91 views
0

我無法在Laravel中進行用戶登錄。我正在使用username而不是email無法在laravel中實現身份驗證方法

下面是我的路線

use App\Message; 

Route::get('/', function() { 
    return view('welcome'); 
}); 
// 


Route::post('/contact','[email protected]'); 

Route::get('/admin9419/dashboard','[email protected]')->name('dash'); 
Route::get('/admin9419/login','[email protected]')->name('home'); 
Route::post('/admin9419/login','[email protected]'); 
Route::post('/admin9419/logout','[email protected]'); 

這是我SessionController文件

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use Illuminate\Foundation\Auth\AuthenticatesUsers; 

class SessionsController extends Controller 
{ 



    public function __construct() 
    { 

     $this->middleware('guest'); 

    } 


    public function create(){ 


     return view('sessions.create'); 


    } 


    public function destroy(){ 

     auth()->logout(); 

     return redirect()->home(); 

    } 

    public function store(){ 



     if(!auth()->attempt(['username'=>request('username'),'password'=>request('password')])){ 

      return back(); 

     } 

     return redirect('/admin9419/dashboard'); 


    } 

} 

錯誤:

Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable 

用戶等級:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class User extends Model 
{ 
    // 
} 

任何人都可以幫助我錯過了什麼?

回答

1

的代碼似乎是正確的。

見類用戶,它應該是這樣的:

use App\Notifications\MailResetPasswordToken; 
use Illuminate\Notifications\Notifiable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 
    use Notifiable; 
    .... 
} 

所以可以粘貼User類?

+0

仍然有相同的錯誤。 – Abhishek

+0

我已編輯我的帖子,我試過你的代碼,它的工作,你可以發佈我一個類的用戶? 是否用戶類擴展Authenticatable? – LorenzoBerti

+0

添加用戶類文件 – Abhishek

0

默認情況下,Laravel使用電子郵件字段進行身份驗證。如果您想自定義此,您可以在自己的LoginController定義的用戶名的方法:

public function username() 
{ 
    return 'username'; 
} 

來源:https://laravel.com/docs/5.5/authentication

+0

這並沒有幫助我仍然得到同樣的錯誤,如果你想看看其他文件讓我知道。 – Abhishek

0
public function store(Request $request){ 

    $username= $request->input('username'); 
    $password= $request->input('password'); 

    if (Auth::guard('web')->attempt(['username' => $username , 'password'=> $password ])) { 
     return redirect()->intended('/admin9419/dashboard'); 
    } 
    else{ 
    return redirect('/login'); 
    } 
} 
+0

沒有工作相同的錯誤。 – Abhishek

+0

add use AuthenticatesUsers; –