【问题标题】:Laravel 5.6 multi auth keeps on authenticating on default modelLaravel 5.6 多重身份验证继续在默认模型上进行身份验证
【发布时间】:2018-04-11 10:11:13
【问题描述】:

我有两个表登录:应试者和公司。公司是我的默认身份验证, 每次我尝试在应试者中尝试身份验证时,它都会继续使用公司的默认表。

我看到了几个与我的问题相似的问题,但它似乎对我不起作用,或者我可能忽略了一些你们中的一些人可能能够看到的问题。

我做错了什么?

Auth.php:

'defaults' => [
    'guard' => 'web',
    'passwords' => 'companies',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'companies',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'companies',
    ],
    'examinees' => [
        'driver' => 'session',
        'provider' => 'examinees',
    ],
],

'providers' => [
    'companies' => [
        'driver' => 'eloquent',
        'model' => App\Company::class,
    ],
    'examinees' => [
        'driver' => 'eloquent',
        'model' => App\Examinee::class,
    ],
],

'passwords' => [
    'companies' => [
        'provider' => 'companies',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'examinees' => [
        'provider' => 'examinees',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

登录控制器:

public function showLoginForm()
{
    return view('examinee.auth.login');
}

protected function guard()
{
    return Auth::guard('examinees');
}

public function login(Request $request) {
    $user = Examinee::where('email', $request->get('email'))->first();

    if (Auth::attempt(['id' => $user->id, 'password' => $request->get('password')])) {

        // prints data from Company table instead of Examinee...
        echo "AUTH USER:<pre>";
        print_r(Auth::user());
        echo "</pre>";
        // return redirect('/home');
    }
}

型号:

namespace App;

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

class Examinee extends Authenticatable
{
    use Notifiable;

    protected $table = 'examinees';
    protected $fillable = ['email',  'password'];
    protected $hidden = ['password',  'remember_token'];

    public $timestamps = false;

}

【问题讨论】:

    标签: php laravel laravel-5.6


    【解决方案1】:

    我过去的做法是将保护添加到您的身份验证中

    改变 Auth::attempt(['id' =&gt; $user-&gt;id, 'password' =&gt; $request-&gt;get('password')

    Auth::guard('examinees')-&gt;attempt(['id' =&gt; $user-&gt;id, 'password' =&gt; $request-&gt;get('password')

    【讨论】:

    • 我刚刚意识到我之前尝试过这个并且我很困惑,因为它说我已经登录但 Auth::user() 没有显示任何内容。原来我必须使用 Auth::guard('examinees')->user() 来获取身份验证数据。但这是否意味着我必须每次都添加警卫?
    猜你喜欢
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2018-07-21
    • 2018-10-17
    • 2023-01-11
    • 2018-11-25
    相关资源
    最近更新 更多