Goal: When a user tries to access a route only meant for 'logged in users'. I want to redirect the user back from where he came with a basic login modal shown.
My Auth middleware:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->back()->with('error_code', 5);
}
}
}
On redirect i successfuly managed to open the basic login modal.
Problem: After entering the credentials and hitting login. The user is redirected to homepage and not to the intended route/page.
The logic works fine when i use
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('/')->with('error_code', 5);
}
}
}
But this always redirects to homepage with the modal open. Which is not what i desire.
How can i fix this?
TIA