我正在研究Laravel 5.6的电子邮件激活和密码忘记功能。注册后我成功发送了电子邮件,或者输入用户的电子邮件后单击了“忘记密码”按钮。但是,一旦用户单击电子邮件中的激活链接并且其帐户处于登录状态,该网站将重定向到主页(127.0.0.1:8000),而不执行激活或重置密码操作。
此处的示例(电子邮件激活):
单击激活电子邮件中的链接(127.0.0.1:8000/register/activate/{sometokenhere})仅会转到首页本身(127.0.0.1:8000)
在RegisterController.php内,
...
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'activate' => 0,
'activation_token' => str_random(60),
]);
$user->notify(new SignupActivate($user));
return $user;
}
public function register(Request $request)
{
$this->validator($request->all())->validate();
$this->guard()->login($this->create($request->all()));
return redirect()->to($this->redirectTo); //homepage
}
public function signupActivate($token)
{
$user = User::where('activation_token', $token)->first();
if (!$user) {
return response()->json([
'user' => $user,
'token' => $token,
'message' => 'This activation token is invalid.'
], 404);
}
$user->active = 1;
$user->activation_token = '';
$user->save();
return $user;
}
...
在Notification / SignupActivate.php内,
...
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
$url = url('/register/activate/'.$notifiable->activation_token);
$name = $this->user->name;
$email = $this->user->email;
return Mail::send('activate', ['name'=>$name, 'url'=>$url], function($message) use($email, $name){
$message->to($email, $name)
->from('test@test.com', 'Admin')
->subject('Activate your account');
});
}
...
相关路线
Route::group(['namespace' => 'Auth'], function () {
Route::get('/register/activate/{token}', 'RegisterController@signupActivate'); //added
});
activate.blade.php,
Hi {{$name}},<br><br>
This email is a confirmation email.<br>
<a href={{$url}} data-method="delete">Activation link</a>