首页 > 解决方案 > 电子邮件验证的多重身份验证 | 拉拉维尔

问题描述

在 laravel 的注册系统中有一个函数 guard()

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

在位置

供应商/laravel/ui/auth-backend/RegisterUsers.php

而寄存器的功能就是使用这个守卫功能

public function register(Request $request){
    $this->validator($request->all())->validate();

    event(new Registered($user = $this->create($request->all())));

    $this->guard()->login($user);

    if ($response = $this->registered($request, $user)) {
        return $response;
    }

    return $request->wantsJson()
                ? new Response('', 201)
                : redirect($this->redirectPath());
}

当我想为另一个守卫创建一个注册系统时,我只会覆盖功能守卫功能并像这样

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

所有这些都非常好,但是在电子邮件验证中,guard() 的功能不存在, $request->user() 而是像那样使用

public function resend(Request $request)
{
    if ($request->user()->hasVerifiedEmail()) {
        return $request->wantsJson()
                    ? new Response('', 204)
                    : redirect($this->redirectPath());
    }

    $request->user()->sendEmailVerificationNotification();

    return $request->wantsJson()
                ? new Response('', 202)
                : back()->with('resent', true);
}

那么,如果我想在这种情况下覆盖警卫,我想覆盖(重新发送)的整个功能,但我认为这不是最好的解决方案所以谁能告诉我该怎么做

标签: phplaravelauthentication

解决方案


而不是覆盖守卫的功能,我只会 shouldUse()像这样在构造函数中使用函数, auth()->shouldUse('admin'); 然后当我使用request->user()它时自动将其用作管理员守卫


推荐阅读