如何解决身份验证会话问题?

时间:2020-04-22 14:33:49

标签: laravel

$customer = Customer::find(6);
        Auth::login($customer);
        if (Auth::check()) {
            return Auth::user()->name;
        }

这是返回用户名,但身份验证会话不起作用,因为当重定向另一页时,我找不到身份验证用户的任何正文,请帮忙

完美适合用户实例

$user= User::find(6);
        Auth::login($user);
        if (Auth::check()) {
            return Auth::user()->name;
        } 

1 个答案:

答案 0 :(得分:0)

您需要在config/auth.php文件中为Customer声明一个新的用户提供程序:

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

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

        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Customer::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

然后为您的用户提供者定义新的防护措施

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],

        'web-customer' => [
            'driver' => 'session',
            'provider' => 'customers',
        ],
    ],

然后将Authenticatableable特性添加到您的客户模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;

class Customer extends Model implements Authenticatable
{
     //...
}

最后,您将可以使用:

登录
        $customer = Customer::find(6);
        Auth::guard('web-customer')->login($customer);
        if (Auth::check()) {
            return Auth::user()->name;
        }