I managed to adjust the default Laravel auth so that it will work as an API for my AngularJS, and so far everything works well. Can go to /reset and enter an email and get sent an email with a password reset link which goes to /reset/{token} and if you don't get any validation errors, your password will successfully be changed.
The only problem is since I am using an Angular view, there isn't really anything validating the token and making sure it's not just gibberish before showing the reset-password
state. I tried adding this to the top of the controller:
if ($stateParams.token != $cookies.get('XSRF_TOKEN')) {
$state.go('reset');
}
...which would basically see if the token is the current CSRF token, but that doesn't work because when the password reset link is sent the CSRF token is changed or something... it is no longer the token from the session.
Anyone have any ideas how I can do this? I want to just redirect the user if the token entered in the url on `/reset/:token' is not valid.
Here is my code..
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'
})
This is in the ResetsPassword trait in ResetsPassword.php. Most was already set up but I removed/changed a lot to work as an 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();
}