2016-03-04 182 views
0

我正在開發一個應用程序,它在laravel 5.2上有兩種類型的用戶,用戶和管理員。我的問題是,這個仇恨永遠不會成功。Multi auth laravel 5.2,驗證不起作用

我改變配置/ AUTH到:

return [ 

'defaults' => [ 
    'guard' => 'user', 
    'passwords' => 'user', 
], 

'guards' => [ 
    'user' => [ 
     'driver' => 'session', 
     'provider' => 'users', 
    ], 
    'admin' => [ 
     'driver' => 'session', 
     'provider' => 'admins', 
    ], 
], 

'providers' => [ 
    'users' => [ 
     'driver' => 'eloquent', 
     'model' => App\User::class, 
    ], 
    'admins' => [ 
     'driver' => 'eloquent', 
     'model' => App\Admin::class, 
    ], 
], 

'passwords' => [ 
    'user' => [ 
     'provider' => 'users', 
     'email' => 'auth.emails.password', 
     'table' => 'password_resets', 
     'expire' => 60, 
    ], 
    'admin' => [ 
     'provider' => 'admins', 
     'username' => 'auth.username.password', 
     'table' => 'password_resets', 
     'expire' => 60, 
    ], 
], 

];

型號聯繫(用戶模式是除了表中, '用戶' 相同):

namespace App; 
use Illuminate\Foundation\Auth\User as Authenticatable; 

class Admin extends Authenticatable 
{ 
    public $timestamps = False; 
    protected $table = 'admins'; 
    protected $fillable = [ 
     'name', 'username', 'password', 
    ]; 

    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 
} 

控制器管理:

public function auth(Request $request) { 
    $credentials = $request->except('_token'); // "username" => "ewfer" "password" => "fwerfwegf" 

    if(!Auth::guard('admin')->attempt($credentials)) { // always enters here, even with the right details 
     Session::flash('flash_error', 'Something went wrong with your login'); 
     return redirect(url('/admin/login')); 
    } 
    return redirect('/admin'); 
} 

我的管理員表的默認值:

Schema::create('admins', function (Blueprint $table) { 
     $table->increments('username'); 
     $table->string('password'); 
     $table->rememberToken(); 
    }); 
DB::table('admins')->insert([ 
     'username' => 'miguel', 
     'password' => Hash::make('password'), 
    ]); 

請注意,對於管理員登錄,我們需要用戶名,而不是用於用戶登錄的電子郵件。

回答

0

嗯,我發現這個問題,這是一個非常愚蠢的,對此感到抱歉。問題是這條線$table->increments('username');,我沒有注意到什麼被存儲在BD中,它是一個int(1)而不是「miguel」。除此之外,一切都很完美