In this post, i will tell you how to redirect user back to URL they were trying to access before login using intended
method.
You can also save the previous url in session so that you can easily redirect back to their previous url after authentication filter.
Example 1 : Using intended method
- public function auth()
- {
- if (Auth::attempt(['email' => $email, 'password' => $password])) {
- // Authentication passed...
- return redirect()->intended('dashboard');
- }
- }
- public function getLogin(Request $request)
- {
- $request->session()->put('url.intended',url()->previous());
- return view('login');
- }
- public function postLogin(Request $request)
- {
- if (Auth::attempt(['email' => $email, 'password' => $password])) {
- return redirect($request->session()->get('url.intended'));
- }
- return back();
- }