代码之家  ›  专栏  ›  技术社区  ›  Wai Yan Hein

如何在不要求用户登录Laravel的情况下验证电子邮件

  •  0
  • Wai Yan Hein  · 技术社区  · 7 年前

    我正在开发一个Laravel应用程序。我的应用程序正在使用Laravel内置的身份验证功能。在Laravel auth中,当用户注册时,会发送一封验证电子邮件。当用户验证电子邮件时,单击电子邮件内的链接,如果用户尚未登录,则必须再次登录以确认电子邮件。

    验证控制器

    class VerificationController extends Controller
    {
        use VerifiesEmails, RedirectsUsersBasedOnRoles;
    
        /**
         * Create a new controller instance.
         * @return void
         */
        public function __construct()
        {
            $this->middleware('auth');
            $this->middleware('signed')->only('verify');
            $this->middleware('throttle:6,1')->only('verify', 'resend');
        }
    
        public function redirectPath()
        {
            return $this->getRedirectTo(Auth::guard()->user());
        }
    }
    

    $this->middleware('auth');
    

    但它不起作用,而是抛出了一个错误。即使用户没有登录,我如何使Laravel能够验证电子邮件?

    2 回复  |  直到 7 年前
        1
  •  12
  •   flexponsive Andrew    6 年前

    首先,删除行 $this->middleware('auth'); 就像你一样。

    接下来,复制 verify VerifiesEmails 你的特点 VerificationController 再稍微改变一下。方法应该如下所示:

    public function verify(Request $request)
    {
        $user = User::find($request->route('id'));
    
        if (!hash_equals((string) $request->route('hash'), sha1($user->getEmailForVerification()))) {
            throw new AuthorizationException;
        }
    
        if ($user->markEmailAsVerified())
            event(new Verified($user));
    
        return redirect($this->redirectPath())->with('verified', true);
    }
    

    这将重写 VerifiesUsers

    安全(如果我错了,请纠正我!)

    它仍然是安全的,因为请求已经签名和验证。如果有人能够以某种方式访问验证电子邮件,他们可以验证另一个用户的电子邮件地址,但在99%的情况下,这几乎不是一个风险。

        2
  •  0
  •   Surf Junky    6 年前

    1-删除或注释VerificationControl中的身份验证中间件

    示例如下:

    public function __construct()
    {
        //$this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }
    

    文件路径:*:\yourproject\vendor\laravel\framework\src\illumed\Foundation\Auth\VerifiesEmails.php

    $user = User::findOrfail($request->route('id'));
    

    public function verify(Request $request)
    {
        $user = User::findOrfail($request->route('id'));
    
        if (! hash_equals((string) $request->route('id'), (string) $user->getKey())) {
            throw new AuthorizationException;
        }
    
        if (! hash_equals((string) $request->route('hash'), sha1($user->getEmailForVerification()))) {
            throw new AuthorizationException;
        }
    
        if ($user->hasVerifiedEmail()) {
            return redirect($this->redirectPath())->with('verified', true);
        }
    
        if ($user->markEmailAsVerified()) {
            event(new Verified($request->user()));
        }
    
        return redirect($this->redirectPath())->with('registered', true);
    }
    
        3
  •  0
  •   Abid Shah    6 年前
    // For Laravel 6 and Above 
    use Illuminate\Auth\Events\Verified; 
    use Illuminate\Http\Request; 
    use App\User;
    
    // comment auth middleware
    //$this->middleware('auth');
    
    public function verify(Request $request)
    {
        $user = User::find($request->route('id'));
    
        if (!hash_equals((string) $request->route('hash'), sha1($user->getEmailForVerification()))) {
            throw new AuthorizationException;
        }
    
        if ($user->markEmailAsVerified())
            event(new Verified($user));
    
        return redirect($this->redirectPath())->with('verified', true);
    }
    
    推荐文章