Kiow
Kiow

Reputation: 880

Laravel reset password route not working

I am building custom views to reset passwords. The routes looks like this:

  Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.reset');
  Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
  Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset.token');
  Route::post('password/reset', 'Auth\ResetPasswordController@reset');

In ResetPasswordController.php I have added this:

//Show form to seller where they can reset password
    public function showResetForm(Request $request, $token = null)
    {
        return view('auth.passwords.reset')->with(
            ['token' => $token, 'email' => $request->email]
        );
    }

The link sent to me looks like this:

https://myapp.dev/password/reset?451c70284a9d4b41123c4ec3efe83602b6cb955427ac48835200a45980bcf9f3

If I now enter that link I will go straight to the password/reset view and not the password/reset/{token}

However if I change the link in my broswer to

https://myapp.dev/password/reset/451c70284a9d4b41123c4ec3efe83602b6cb955427ac48835200a45980bcf9f3 (changing "?" to "/") I it works

So why doesnt the ? version of the URL work? I am using laravel 5.5

And since I dotn use the Auth:routes() is there any way to see what routes laravel generates when you use that?

Upvotes: 2

Views: 12691

Answers (3)

syafiq zahir
syafiq zahir

Reputation: 31

First of all, in your route

Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.reset');

will take precedence over your url. since the route was declared on top and thus will goes into that showLinkRequestForm function first.

Meanwhile in your '/{token}' it will take slashes with the value that you sent thru the get route. Which currently you get.

Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset.token');

Note that, position of route declaration is also affecting . Given example of 2 route with the same url but different name

//1st password/reset
Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.reset');

//2nd password/reset 
Route::get('password/reset', 'Auth\ForgotPasswordController@showTokenForm')->name('password.reset.form');

In that case route naming will take the latest/last declaration which is 2nd password/reset and the first password/reset will be ignored or made into not available (tested).

So to answer your question which you asking Muhammad Nauman :

"How should I then change my code in order to get given route to work?"

Route::get('password/reset/{token}', 
'Auth\ResetPasswordController@showResetForm')->name('password.reset.token');

In blade template you can adjust your routing value looks like this

<a href="{{route('password.reset.token',['token'=> $token_value ])}}">Reset Form</a>

Inside your new ResetPasswordController.php

public function showResetForm(Request $request)
{
    $token = $request->route()->parameter('token');

    return view('auth.passwords.reset')->with(
        ['token' => $token, 'email' => $request->email]
    );
}

But if you are kinda like doing dirty way which pass the GET request token email "?token="somevalue"&email="somevalue" you could do something like this

public function showResetForm()
{
  return view('auth.passwords.reset')->with(
        ['token' => request('token'), 'email' => request('email')]
    );
}

Then in the blade you add additional routing parameter of email too

<a href="{{route('password.reset.token',['token'=> $token_value,'email'=> $user->email ])}}">Reset Form</a>

Upvotes: 0

Muhammad Nauman
Muhammad Nauman

Reputation: 1309

There are two different things with parameters.

  1. Route Parameters: These are included in the routes with '/' as in your example. You can get them by:

    $request->parameter('parameter_name'); $request->parameters(); // for all parameters

  2. Request Parameters: These are request parameters which attached in the URL after '?'. Parameters are sent this way in GET request. You can get them by:

    $request->input('parameter_name'); $request->all(); // for all parameters

Upvotes: 1

iamab.in
iamab.in

Reputation: 2070

Laravel doc..

Probably you are confused with required parameters and optional parameters.

When you are defining the following route..

  Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset.token');

Laravel expects the token value as a required parameter in the third segment of the route.

But when you are accessing the route as

https://myapp.dev/password/reset?451c70284a9d4b41123c4ec3efe8360..

There is only two segment for the route. The token value is assigned as the get parameter or optional parameter. As you already defined as follows..

Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm')->name('password.reset');

your generated link points to the password/reset and ?451c70284a9d4b41123c4ec3efe83602b6cb955427ac48835200a45980bcf9f3 value is passed as the get parameter.

To trigger your reset the following route

Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset.token');

you should use the following link format

https://myapp.dev/password/reset/451c70284a9d4b41123c4ec3efe83602b6cb955427ac48835200a45980bcf9f3

Upvotes: 0

Related Questions