Questions & Answers

Laravel 8.x: redirect after Login to previous location, with all Input data reinserted

I'm trying to build an app with Laravel that allows users to book flights with several locations included.

For autentication I'm using the laravel/ui package.

To complete the booking, if the user is not logged, he's redirected to login form and if the credentials are valid he returns to the previous page.

The problem is once the user returns back, after login, the data passed previously with the POST request at that page do not exist anymore.

I put a flowchart to explain better what I would like to do:

So, what should I do to reload that page, with the previous POST values?

2023-01-07 20:30:42
You can use temporary table for this purpose.
2023-01-07 20:30:43
You can use session too.
Answers(4) :

In that case you should redirect user back using withInput(), eg.:

return back()->withInput();

Here you can find a more detailed explanation:

2023-01-07 20:30:43
I tried to flash the data to the session from the middleware, but I can't retrieve them after the submit of the login form
2023-01-07 20:30:43
I don't know how are you flashing data to session, but maybe this post should give you an answer….

You could make a temp table to save this data before redirecting the user to log in with a unique value in the session held in both the new user row and the previous temp table.

Then after redirection load the page with user temp data in the temp table through that key.

It would be nice if you do a corn job to delete these temp data daily.

2023-01-07 20:30:43
Maybe I'll try to check what is the better solution

I think you could do your submission with javascript and take advantage of the browser storage to persisit the temporary data. You can delete this once you are done.

I solved adding GET method to the page to book the flight, then I pass the input data from the middleware as route params to the login form. After the login form is submitted, if the user log successfully, that data are flashed to the session and are available after redirect to the previous page. I think is not the best solution because the data are shown in the URL in login page, but it works.