简体   繁体   English

多身份验证Laravel 5.2,自动验证不起作用

[英]Multi auth laravel 5.2, autentication not working

I am developing an app which has two types of users, users and admins on laravel 5.2. 我正在开发一个在laravel 5.2上具有两种类型的用户,用户和管理员的应用程序。 My problem is that the autenticacion is never successful. 我的问题是取消手术永远不会成功。

I changed config/auth to: 我将config / 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,
    ],
 ],

]; ];

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. 请注意,对于管理员登录,我们需要用户名,而不是用于用户登录的电子邮件。

Well i discovered the problem, it was a very stupid one, sorry about that. 好吧,我发现了这个问题,那是一个非常愚蠢的问题,对此感到抱歉。 The problem was this line $table->increments('username'); 问题是这行$table->increments('username'); and i didn't notice what were being stored in BD, it was an int (1) instead of "miguel". 而且我没有注意到BD中存储了什么,它是一个int(1)而不是“ miguel”。 Apart from that everything worked perfectly 除此之外,一切都运转良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM