简体   繁体   English

无法使用 Laravel 中的自定义防护成功登录

[英]Unable to successfully login using custom guard in Laravel

I am trying to create a new type of login that works alongside users table called business_users .我正在尝试创建一种与名为business_usersusers表一起使用的新型登录。

I have added the associated, tables, models and config into my auth.php files.我已将关联的表、模型和配置添加到我的 auth.php 文件中。

Model: Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class BusinessUser extends Authenticatable
{
    protected $fillable = ['first_name', 'last_name', 'email', 'password', 'username'];

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

    protected $guard = 'business_user';

    public function business()
    {
        return $this->belongsTo('App\Business');
    }

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

    public function getAuthPassword()
    {
        return $this->password;
    }

}

auth.php auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'business_user' => [
        'driver' => 'session',
        'provider' => 'business_users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
],
...
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

     'business_users' => [
         'driver' => 'eloquent',
         'model' => App\BusinessUser::class,
     ],
],

Route (which fakes a login for testing)路由(伪造登录以进行测试)

Route::get('/business/fake-login', function () {

    $user = \App\BusinessUser::first();

    if (Auth::guard('business_user')->attempt(['username' => $user->username, 'password' => $user->password])) {
        return redirect()->intended('/business/dashboard');
    }

});

I am trying to use the business.username and business.password to login but the Auth:guard condition above returns false.我正在尝试使用business.usernamebusiness.password登录,但上面的 Auth:guard 条件返回 false。

Can anyone explain what I'm doing wrong?谁能解释我做错了什么?

(fyi I am using Laravel 7.x) (仅供参考,我使用的是 Laravel 7.x)

You are retriving $user from the database, the password is encrypted.您正在从数据库中检索$user ,密码已加密。

Auth::attempt() will encrypt the password for you, so in the check password part, your password is actually being encrypted twice. Auth::attempt()将为您加密密码,因此在检查密码部分,您的密码实际上被加密了两次。

Instead, you may use Auth:attempt() like this:相反,您可以像这样使用Auth:attempt()

$res = Auth::guard('business_guard')->attempt([
   'username' => "test",
   'password' => "test",
]);

dd( $res );

To understand further, you can go to EloquentUserProvider.php要进一步了解,您可以 go 到EloquentUserProvider.php

public function validateCredentials(UserContract $user, array $credentials)
{
    $plain = $credentials['password'];
    return $this->hasher->check($plain, $user->getAuthPassword());
}

Use you original code, and dd() the $plain to see what's going on.使用您的原始代码,并dd() $plain以查看发生了什么。

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

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