【发布时间】:2016-03-24 16:43:30
【问题描述】:
我设法调整了默认的 Laravel 身份验证,以便它可以作为我的 AngularJS 的 API 工作,到目前为止一切正常。可以转到 /reset 并输入一封电子邮件,然后会收到一封带有密码重置链接的电子邮件,该链接转到 /reset/{token},如果您没有收到任何验证错误,您的密码将成功更改。
唯一的问题是,由于我使用的是 Angular 视图,因此在显示 reset-password 状态之前,实际上没有任何东西可以验证令牌并确保它不仅仅是胡言乱语。我尝试将其添加到控制器的顶部:
if ($stateParams.token != $cookies.get('XSRF_TOKEN')) {
$state.go('reset');
}
...这基本上会查看令牌是否是当前的 CSRF 令牌,但这不起作用,因为当发送密码重置链接时,CSRF 令牌已更改或其他...它不再是来自的令牌会议。
有人知道我该怎么做吗?如果在 `/reset/:token' 的 url 中输入的令牌无效,我只想重定向用户。
这是我的代码..
App.js:
.state('reset', {
url: '/reset',
data: {
permissions: {
except: ['isLoggedIn'],
redirectTo: 'user.dashboard'
}
},
templateUrl: 'views/auth/forgot-password.html',
controller: 'ForgotPasswordController as forgot'
})
.state('reset-password', {
url: '/reset/:token',
data: {
permissions: {
except: ['isLoggedIn'],
redirectTo: 'user.dashboard'
}
},
templateUrl: 'views/auth/reset-password.html',
controller: 'ResetPasswordController as reset'
})
这在 ResetsPassword.php 的 ResetsPassword 特征中。大多数已经设置好了,但我删除/更改了很多以作为 API 工作:
/**
* Send a reset link to the given user.
*/
public function postEmail(EmailRequest $request)
{
$response = Password::sendResetLink($request->only('email'), function (Message $message) {
$message->subject($this->getEmailSubject());
});
switch ($response) {
case Password::RESET_LINK_SENT:
return;
case Password::INVALID_USER:
return response()->json([
'denied' => 'We couldn\'t find your account with that information.'
], 404);
}
}
/**
* Get the e-mail subject line to be used for the reset link email.
*/
protected function getEmailSubject()
{
return property_exists($this, 'subject') ? $this->subject : 'Your Password Reset Link';
}
/**
* Reset the given user's password.
*/
public function postReset(ResetRequest $request)
{
$credentials = $request->only(
'password', 'password_confirmation', 'token'
);
$response = Password::reset($credentials, function ($user, $password) {
$this->resetPassword($user, $password);
});
switch ($response) {
case Password::PASSWORD_RESET:
return;
default:
return response()->json([
'error' => [
'message' => 'Could not reset password'
]
], 400);
}
}
/**
* Reset the given user's password.
*/
protected function resetPassword($user, $password)
{
$user->password = bcrypt($password);
$user->save();
}
【问题讨论】:
标签: php angularjs laravel laravel-5 csrf