使用多个用户模型登录nova和laravel

时间:2019-07-19 23:25:31

标签: authentication laravel-5 laravel-nova

我将laravel 5.8与nova一起使用,并且需要两个用户模型a_users和b_users。 a_users应该能够登录laravel应用程序,而b_users应该能够登录nova。

不同的用户不能在同一数据库表中。 <-这是我无法控制的要求。

当前,我有两个单独的注册表单,用于注册用户并将其添加到各自的表中。 b_users能够登录nova应用程序,而a_users从未进行身份验证(未找到)。

有两个单独的登录控制器(一个是nova,一个是一个App / Http / Controllers / Auth),我也一直在研究AuthenticatesUsers文件。

我不确定是否应该以某种方式扩展AuthenticatesUsers或使用控制器,防护装置或其他方法来实现此目的。

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

我使用以下代码绕过您的问题。编码愉快!

// database\migrations\2019_07_23_075330_create_a_users_table.php
    public function up()
    {
        Schema::create('a_users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

// database\migrations\2019_07_23_075443_create_b_users_table.php
    public function up()
    {
        Schema::create('b_users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

// .env
NOVA_GUARD=loginnova

// config\auth.php
<?php

return [

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

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

        'loginnova' => [
            'driver' => 'session',
            'provider' => 'b_users',
        ],

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

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

        'b_users' => [
            'driver' => 'eloquent',
            'model' => App\BUser::class,
        ],

        'a_users' => [
            'driver' => 'eloquent',
            'model' => App\AUser::class,
        ],

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

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

];

// app\Nova\AUser.php
<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class AUser extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}

// app\BUser.php
<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class BUser extends Model implements
    AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword, MustVerifyEmail;
}

注意:我使用Laravel的默认身份验证,以及Nova的登录形式!