Laravel5.5中的多重身份验证

vmdwslir  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(252)

我想在身份验证中集成多个表,我经历了以下步骤:
第一个i。创建迁移:

php artisan make:migration createTouristsTable

在迁移文件中:

public function up()
{
    Schema::create('tourists', function (Blueprint $table) {
        $table->increments('id');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();            
        $table->timestamps();
    });
}

运行迁移

php artisan migrate

游客模式:

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

class Tourists extends Authenticatable
{
    use Notifiable;

    protected $table = 'tourists';

    protected $fillable = ['email',  'password'];

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

我已经修改了config/auth.php文件来添加我的自定义守卫“游客”。

'guards' => [

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

    'tourists'  => [
      'driver'  => 'session',
      'provider' => 'tourists',
    ],

],

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

    'tourists' => [
        'driver' => 'eloquent',
        'model'  => App\Tourists::class,
    ],
]

现在,在我的logincontroller中,我需要指定要使用的保护:

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function guard()
    {
        return Auth::guard('tourists');
    }
}

现在,我如何为这个表创建一个登录和注册表单?

kjthegm6

kjthegm61#

您将创建新的登录和注册页面,就像您将创建任何其他页面一样。重要的区别在于,每个登录/注册页面都有单独的路由,或者根据您定义的一些条件有条件地呈现包含表单的部分,以便访客进入访客登录,普通用户进入普通登录。
第三种选择,我认为最适合这种情况,是使用组件和插槽。
https://laravel.com/docs/5.5/blade#components-和插槽
问题是,您将如何确定未经验证的用户应该进入哪个页面?

相关问题