【问题标题】:Laravel 5.7 Email Verification Not Working With Different BrowsersLaravel 5.7 电子邮件验证不适用于不同的浏览器
【发布时间】:2019-06-01 21:30:31
【问题描述】:

最近我正在使用 Laravel 5.7 实现电子邮件验证。

如果您遵循以下情况,我发现电子邮件验证不起作用。

场景 1

用户在浏览器A上注册了一个账号,在浏览器B上激活。

然后它将用户重定向到登录页面。

显然电子邮件激活失败。

场景 2(对于我的用例)

用户在浏览器 A 上注册了一个帐户。

激活电子邮件已发送到移动收件箱。

用户从手机激活电子邮件,但激活无效。


如果使用其他浏览器代理激活,我猜激活失败是因为找不到激活密钥会话密钥。

任何人都面临同样的问题并设法提出解决方案?

【问题讨论】:

    标签: laravel-5.7 email-verification


    【解决方案1】:

    由于 Laravel 电子邮件验证的工作方式,它需要用户登录才能处理验证链接。 您可以通过在 VerificationController 中更改以下内容来解决此问题

    1. 在 __construct() 函数中为验证函数设置身份验证中间件异常
        $this->middleware('auth')->except('verify');
    
    1. 创建一个验证函数来覆盖 laravel 验证函数
        public function verify(Request $request)
        {
            $userId = $request->route('id');
            $user = User::findOrFail($userId);
    
            if ($user->hasVerifiedEmail()) {
                return redirect($this->redirectPath());
            }
    
            if ($user->markEmailAsVerified()) {
                event(new Verified($user));
            }
    
            return redirect($this->redirectPath())->with('verified', true);
        }
    

    【讨论】:

      【解决方案2】:

      回答@Gopal Gautam。它对我来说就像这样:

      public function verify(Req $request)
      {         
          $userId = $request->route('id');
          $user = \App\User::findOrFail($userId);
      
          if ($user->hasVerifiedEmail()) {
              \Illuminate\Support\Facades\Auth::login($user);
              return redirect($this->redirectPath())->with('verified', true);
          }
      
          if ($user->markEmailAsVerified()) {
              event(new Verified($user));
          }
      
          return redirect($this->redirectPath())->with('verified', true);
      }
      

      【讨论】:

      • 您是否尝试在浏览器上注册您的帐户并尝试通过单击电子邮件中的链接在您的手机上激活您的帐户?
      • 不,我没有在手机上测试过,但是如果电脑没问题,为什么不上手机呢?
      • 呃,在你尝试之前很难在这里解释。当您在手机上激活时,预期的输出将是“什么都没有发生”或“激活失败”。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 2016-01-15
      • 2019-03-05
      • 2023-03-28
      • 2019-03-04
      相关资源
      最近更新 更多