Question

I am trying to implement the password reset logic. The user gets the link to reset the password in the email. The url looks like

http://example.com/reset/resetcode

I have the route defined for it:

Route::get('reset/{resetcode}', function(){
    return View::make('users.reset_password');
});

The form is rendered in the view to submit the email, new password etc. For the post of the form I have route defined as:

Route::post('reset/{resetcode}', array( 'as' => 'reset', 'uses' => 'UserController@passwordReset'));

I grab the resetcode from post route inside the passwordReset controller below

public function passwordReset($resetcode)
{ 
    $validation = Validator::make(Input::all(), UserModel::$rulesPasswordReset);
    if ($validation->passes())
    { 
    try
    {
    // Find the user using the user email address
    $user = Sentry::findUserByLogin(Input::get('email'));

    // Check if the reset password code is valid
    if ($user->checkResetPasswordCode($resetcode))
    {
        // Attempt to reset the user password
        if ($user->attemptResetPassword($resetcode, 'new_password'))
        {
        // Password reset passed
         }
         else
         {
        // Password reset failed
         }
    }
    else
    {
        // The provided password reset code is Invalid
    }
        }
        catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
        {
       echo 'User was not found.';
        }
    }
    else return Redirect::route('reset')->withInput()
    ->withErrors($validation)
    ->with('title', 'resetrequestfailure')
    ->with('message', 'Seems like you made some errors.');
   }

The problem I am having is when I do Redirect::route after the validation fails. I am getting the resetcode from the route defined for post. When validation fails, the redirect route messes up and I cannot get the resetcode the second time. The supposed url of format

 http://example.com/reset/8f1Z7wA4uVt7VemBpGSfaoI9mcjdEwtK8elCnQOb

becomes

http://bcnet.org/reset/%7Bcode%7D

It has to do with /{resetcode} part of the route and this is variable, so how can I get the correct resetcode even after the validation fails meaning that the url remains intact. Or how can I fix it to the appropriate Redirect::route after the validation failure.

Was it helpful?

Solution

You need to include the $resetcode on your return

else return Redirect::route('reset', $resetcode)->withInput()
    ->withErrors($validation)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top