php - Laravel 电子邮件验证 - 强制登录

标签 php laravel email-verification

按照启用新的内置电子邮件验证的安装后,一切正常(注册后发送电子邮件并单击激活启用帐户)。
但是,我遇到了用户必须登录才能进行验证过程的情况。意思是,如果用户在使用验证链接之前没有登录,他将被重定向到登录页面,然后显示 /resources/view/auth/verify.blade.php页。

我正在与社区联系,看看这是故意的,是错误还是我做错了什么?有人在同样的情况下运行吗?

我正在建立的站点可以公开访问大多数页面,但仅限于某些页面(现在是用户门户)。我设置了routes/web.php如下:

// Authentication
Auth::routes(['verify' => true]);
Route::group(['middleware' => ['auth', 'verified'], 'as' => 'portal.', 'prefix' => '/portal'], function () {
    Route::get('/', 'PortalController@index');
    Route::get('/profile', 'PortalController@index')->name('profile');
    Route::get('/orders', 'PortalController@index')->name('orders');
});

通过跟踪验证过程,我能够在 VerificationController 中发现该过程正在强制登录。构造函数通过如下所示的中间件。
public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }

通过注释第一行或添加 ->except('verify') ,登录页面未显示,但在 Traits VerifiesEmails 处抛出错误方法验证如下,因为用户显然没有登录($request->user() 为空)。
public function verify(Request $request)
    {
        if ($request->route('id') == $request->user()->getKey() &&
            $request->user()->markEmailAsVerified()) {
            event(new Verified($request->user()));
        }

        return redirect($this->redirectPath())->with('verified', true);
    }

我的问题是,有没有办法让它在没有事先登录的情况下工作,或者这就是 5.7 中验证过程的实现方式? ……或者我做错了什么?

最佳答案

is there a way to get it to work without being loged-in beforehand, or is this the way the Verification process is implemented in 5.7? ... or what am I doing wrong?



这是在 Laravel 5.7 中实现验证过程的方式。 Laravel 使用签名 URL 进行验证。 URL 是用 id 生成的参数(id 作为用户 ID),当用户点击验证链接时,会进行 3 项检查:
  • 签名有效吗? ( signed 中间件)
  • 签名中的用户ID是什么?这是最终将被验证的 ID
  • 当前登录的用户是否具有相同的 ID?

  • 您始终可以通过覆盖 VerificationController 中的 verify 方法来删​​除第三个检查,如下所示:
    public function verify(Request $request)
    {
    
        $userId = $request->route('id');
        $user = App\User::findOrFail($userId);
    
        if ($user->markEmailAsVerified()) {
            event(new Verified($user));
        }
    
        return redirect($this->redirectPath())->with('verified', true);
    }
    

    关于php - Laravel 电子邮件验证 - 强制登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52949374/

    相关文章:

    PHP mysql fetch 不返回值

    php - 为什么我在使用 enctype ="multipart/form-data"时会得到 undefined index

    php - 使用 mongoDB Jenssegers Laravel 运行原始查询

    python - 是否有自定义 Django 电子邮件确认链接模块?

    php - Laravel 5.1 注册后如何发送邮箱验证邮件?

    php - 我现在已经浏览了所有论坛,但仍然无法让我的 Php 将数据发送到我的数据库以供评论系统

    Php数据库-内容加密/解密

    php - Laravel 从软删除中获取关系

    php - 使用动态字段时发布复选框值

    php - 使用链接进行电子邮件验证是个坏主意吗