Multi auth laravel 5.2, autentication not working

时间:2016-03-04 17:51:17

标签: php laravel-5.2

I am developing an app which has two types of users, users and admins on laravel 5.2. My problem is that the autenticacion is never successful.

I changed config/auth to:

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,
    ],
 ],

];

Model Admin (The User model is the same except for the table, 'users'):

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',
   ];
}

Controller admin:

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');
}

Default of my admins table:

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

Note that for the Admin login we need the username, instead of the email which is used for the users login.

1 个答案:

答案 0 :(得分:0)

我发现了这个问题,这是一个非常愚蠢的问题,对此感到抱歉。问题是这一行$table->increments('username');并且我没有注意到BD中存储了什么,它是一个int(1)而不是“miguel”。除此之外一切都很完美