【问题标题】:How to change the Authentication model in Laravel如何在 Laravel 中更改身份验证模型
【发布时间】:2019-08-23 12:18:49
【问题描述】:

我需要将 Laravel 应用程序中的默认用户模型更改为管理员模型。我按照change Authentication model in Laravel 5.3中给出的教程进行操作

Admin 模型必须进行身份验证、注册和重置密码。这就是我到目前为止所做的。

在做了php artisan make:auth之后,我创建了Admin.php模型

namespace Modules\Admin\Entities;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Admin extends Authenticatable
{
    use Notifiable;
    protected $fillable = ['name','email','password'];
    protected $hidden = [
        'password', 'remember_token',
    ];
}

这是config/auth.php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

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

    /*
    |--------------------------------------------------------------------------
    | 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' => 'admins',
        ],

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

    /*
    |--------------------------------------------------------------------------
    | 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' => \Modules\Admin\Entities\Admin::class,
        ],

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

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];

这是RegisterController.php

namespace App\Http\Controllers\Auth;

use Modules\Admin\Entities\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:admins'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return Admin::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

这段代码在admins 表中创建了一个用户,但是当我尝试登录时,它会抛出以下错误:

传递给 Illuminate\Auth\SessionGuard::__construct() 的参数 2 必须是 Illuminate\Contracts\Auth\UserProvider 的实例,给定 null,在 ...\vendor\laravel\framework\src\Illuminate\Auth 中调用\AuthManager.php 第 125 行

我不明白为什么会发生这个错误。

【问题讨论】:

  • AuthServiceProvider ..
  • @Demonowh Admin.php 在 Admin 模块中。请更具体

标签: laravel laravel-5


【解决方案1】:

在config/auth.php上更改您的代码:

'web' => [
        'driver' => 'session',
        'provider' => 'admins',
 ],

到:

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

【讨论】:

  • 哇!成功了,管理员在admins 表中,为什么会这样?
  • 因为您实际上没有一个名为 admins 的提供程序,所以您在 config/auth.php 中的提供程序仍称为 users,请参见此处:'users' => [ 'driver' => 'eloquent', 'model' => \Modules\Admin\Entities\Admin::class, ] 所以您实际需要做的就是更改您的模型以指向您所做的管理模型! :D
【解决方案2】:

所有身份验证驱动程序都有一个用户提供程序。

这定义了如何从您的数据库或此应用程序用于持久保存用户数据的其他存储机制中实际检索用户。

更改您的 config/auth.php 文件,如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2015-06-08
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    相关资源
    最近更新 更多