last day (15 days later) » 

6:56 AM
0
Q: Laravel 8: Url Intended Not Working With Custom Login

Baby BabaiInto my project I want that the admin who will login will get back to the previous url that the admin has left off while logging out of the admin panel, for login I have used the process that is being described here in https://laravel.com/docs/8.x/authentication#authenticating-users but it is not...

 
Can you post your routes/web.php file too. Looks like you are trying to handle post and get in the same action. You might not have Route::post and Route::get set up correctly
 
@Brett Okay I am posting my routes file too
@Brett You can check I Have updated my post
 
Where is it redirecting to on successful login?
What middleware is responsible for redirecting them if they are not logged in as admin?
 
@Brett Going back to the admin dashbord
@Brett Admin Middleware
Okay Lets start
 
Can you post the admin middleware. And can you tell me what link you are attempting to go to when they are logged out?
 
6:57 AM
Okay
This is my middleware public function handle(Request $request, Closure $next)
{
if(!Auth::check()) {
return redirect('/admin');
}
return $next($request);
}
 
Hmm ok, so you are on an admin page... you wait for session to timeout, you attempt to go to /admin/users and it redirects you to the login page, then when you login it goes to admin/dashboard no matter what?
 
No i want that suppose if the user while logging in is in /admin/users then while logging out, after the next time logging in it should get back to the /admin/users but it is going back to admin/dashboard
 
Oh! ok, that won't work because logging out destroys the session which is where intended is stored.
If you wanted it to store that you'd have to somehow store it in the user account in the database when they click log out
 
So how can i do that
 
Intended is used for they've attempted to go to a page that is locked out, it redirects to admin for them to login then it goes back to the page they were attempting to before hand
 
7:04 AM
Hmm Okay
 
You could catch the logout action.

Route::get('/logout',[LoginController::class, 'logout']);

In the logout() action you would get the $request->previous() and store that against the user database
 
Can you ellaborate please?
 
    $previous = url()->previous('admin/dashboard');

    $user->previous = $previous;
    $user->save();
You would have to create a new column on user table to store ir
Then in the login action, check for a value in there and redirect
if ($user->previous) {
$redirect = $user->previous;
$user->previous = null;
$user->save();

return redirect($redirect);
}
// Otherwise do the usual stuff
Schema::table('users', function() {
 
Okay but here should i use the same $previous = url()->previous('admin/dashboard');
 
$table->string('previous')->nullable();
}
In the logout action you want to use $previous = url()->previous() to save the value BEFORE you do any of the logout stuff
public function logout(Request $request)
{
/// Add that code here
$this->guard()->logout();
 
7:09 AM
Sure Okay let me give it a try
 
I have to go, good luck, i'll check back later.
Have also posted an answer, if it works please mark it as Accepted. Bye
 
Okay Sure Thank You
 
7:30 AM
Thanks It worked for me
Oh I have marked it as accepted... :):)
 

last day (15 days later) »