Laravel 停留在 email/verify
Laravel stuck on email/verify
我刚刚应用了 laravel 电子邮件验证,并希望在登录后进入页面之前确保我的用户已通过验证。
我添加了以下代码:
class User extends Authenticatable implements MustVerifyEmail
...
Auth::routes(['verify' => true]);
...
Route::get('management', function () {
// Only verified users may enter...
})->middleware('verified');
如果用户注册,他会收到一张便条和一封电子邮件来验证他的邮件。他点击邮件中的按钮,得到验证,一切正常。
但是我发现了另外一个案例:
如果用户注册但不验证他的邮件,他将始终被重定向到 email/verify。
例如,如果不小心输入了错误的电子邮件,他甚至无法访问注册页面,因为即使在 mypage.com/register 他也会被重定向到 mypage.com/email/verify!
这是Laravel故意的吗?我错过了什么?我是否必须/是否可以从验证中排除 login/register 页面?
提前致谢
我以前遇到过这个问题,我有这个方法可以解决,如果你想定制可以考虑这个方法。
在LoginController.php
中你可以添加这段代码,我覆盖了默认的登录方式:
public function login(Request $request)
{
$this->validateLogin($request);
$user = User::where($this->username(), $request->{$this->username()})->first();
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($user->hasVerifiedEmail()) {
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
})
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
您也可以覆盖并向 sendFailedLoginResponse 添加一个新参数,让该方法知道何时重定向到 email/verify 页面,或者只是在 $user->hasVerifiedEmail()
if 块中添加其他内容以将其重定向到 email/verify 页
编辑:
可以删除LoginController和RegisterController中的$this->middleware('guest')
,让已经登录的用户可以去注册登录页面,但是如果已经登录的人重新登录或注册就奇怪了。
我遇到了同样的问题,我非常友好地解决了它...(我认为!)
首先:在 View/Auth/verify.blade.php 中放置一个 link 到将清除 cookie 的新路由:
<a href="/clear-session" class="button text-center">My mail was wrong, I want to try another one</a>
其次:在您的 routes/web.php 文件中添加一个将清除会话 cookie 的路由:
// Clear session exception
Route::get('/clear-session', function(){
Cookie::queue(Cookie::forget(strtolower(config('app.name')) . '_session'));
return redirect('/');
});
如果用户按下按钮,这将清除 cookie,并重定向到主页。
如果这不起作用,请确保您试图忘记的 cookie 名称是正确的。 (使用您的 chrome 控制台检查:应用程序 -> cookies)
例如:
Cookie::queue(Cookie::forget('myapp_session'));
我刚刚应用了 laravel 电子邮件验证,并希望在登录后进入页面之前确保我的用户已通过验证。
我添加了以下代码:
class User extends Authenticatable implements MustVerifyEmail
...
Auth::routes(['verify' => true]);
...
Route::get('management', function () {
// Only verified users may enter...
})->middleware('verified');
如果用户注册,他会收到一张便条和一封电子邮件来验证他的邮件。他点击邮件中的按钮,得到验证,一切正常。
但是我发现了另外一个案例:
如果用户注册但不验证他的邮件,他将始终被重定向到 email/verify。
例如,如果不小心输入了错误的电子邮件,他甚至无法访问注册页面,因为即使在 mypage.com/register 他也会被重定向到 mypage.com/email/verify!
这是Laravel故意的吗?我错过了什么?我是否必须/是否可以从验证中排除 login/register 页面?
提前致谢
我以前遇到过这个问题,我有这个方法可以解决,如果你想定制可以考虑这个方法。
在LoginController.php
中你可以添加这段代码,我覆盖了默认的登录方式:
public function login(Request $request)
{
$this->validateLogin($request);
$user = User::where($this->username(), $request->{$this->username()})->first();
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if (method_exists($this, 'hasTooManyLoginAttempts') &&
$this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
return $this->sendLockoutResponse($request);
}
if ($user->hasVerifiedEmail()) {
if ($this->attemptLogin($request)) {
return $this->sendLoginResponse($request);
}
})
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
return $this->sendFailedLoginResponse($request);
}
您也可以覆盖并向 sendFailedLoginResponse 添加一个新参数,让该方法知道何时重定向到 email/verify 页面,或者只是在 $user->hasVerifiedEmail()
if 块中添加其他内容以将其重定向到 email/verify 页
编辑:
可以删除LoginController和RegisterController中的$this->middleware('guest')
,让已经登录的用户可以去注册登录页面,但是如果已经登录的人重新登录或注册就奇怪了。
我遇到了同样的问题,我非常友好地解决了它...(我认为!)
首先:在 View/Auth/verify.blade.php 中放置一个 link 到将清除 cookie 的新路由:
<a href="/clear-session" class="button text-center">My mail was wrong, I want to try another one</a>
其次:在您的 routes/web.php 文件中添加一个将清除会话 cookie 的路由:
// Clear session exception
Route::get('/clear-session', function(){
Cookie::queue(Cookie::forget(strtolower(config('app.name')) . '_session'));
return redirect('/');
});
如果用户按下按钮,这将清除 cookie,并重定向到主页。
如果这不起作用,请确保您试图忘记的 cookie 名称是正确的。 (使用您的 chrome 控制台检查:应用程序 -> cookies) 例如:
Cookie::queue(Cookie::forget('myapp_session'));