Home > database >  How to use temporarySignedRoute() in Laravel?
How to use temporarySignedRoute() in Laravel?

Time:10-13

I have this route in the web.php:

Route::get('/email/sent/{email}', function(){
        return view('auth.passwords.email_sent');
})->name('forgot-password.email.sent')->middleware(['signed']);

In a controller I am generating a temporary signed route using:

$url = URL::temporarySignedRoute('forgot-password.email.sent', now()->hours(1), ['email' => $email]);

How ever everytime I visit the route it returns 403 Forbidden. But if I use signedRoute():

$url = URL::signedRoute('forgot-password.email.sent', ['email' => $email]);

It works, I think the problem is with the generated timestamps but I am not really sure.

Has anyone encountered this before?

CodePudding user response:

now()->hours(1) sets the date to now except with the hour changed to the number passed, in your case 1, which is not what you want. And because 1 AM happens to be before the current time, the temporary route is already expired when you try to access it and you get a 403 error.

Try now()->addHours(1) instead.

In the future you could try to die & dump or log now()->hours(1) to check what's wrong with the date and time.

https://laravel.com/docs/9.x/helpers#method-now here the now helper is described. \Illuminate\Support\Carbon is instantiated, which wraps Carbon\Carbon, which means you can see the available methods here: https://carbon.nesbot.com/docs/.

You could also use Carbon directly if you wanted.

  • Related