RedirectIfAuthenticated redirect if trying to open another login form

I have two login forms with two different tables. One has a default /login route, and the other has a /myportal route. I have an additional login

  protected $redirectTo = '/student-home'; public function showLoginForm() { return view('my_portal'); } public function logout(Request $request) { $this->guard()->logout(); $request->session()->flush(); $request->session()->regenerate(); return redirect('/my_portal'); } protected function guard() { return Auth::guard('web_student'); } public function username () { return 'username'; } 

This login is working fine. But I have a problem with RedirectIfAuthenticated

 public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { return redirect('/home'); } else if(Auth::guard('web_student')->check()) { return redirect('student-home'); } return $next($request); } 

Now, if the user is already registered, he will be redirected to /student-home only if the route is /login and not /my-portal . ie only if I click on the regular form, and not this additional form that I created. How can I redirect to student-home if the user clicked on /my-portal ?

+5
source share
2 answers

You can connect the controller to the route of my portal with:

 Route::get('test', ' exampleController@example ') ; 

Then in the controller function you can check if the user has already been registered

 public function example() { if(Auth::check()) { //This condition will run if the user is logged in ! return redirect('student-home'); } //Do whatever you want if user is not logged in! } 

Hope this answers your question!

+4
source

Modify your RedirectIfAuthenticated middleware as follows

 public function handle($request, Closure $next, $guard = null) { if (Auth::guard($guard)->check()) { if(guard == 'web_student') { return redirect('student-home'); }else return redirect('/home'); } return $next($request); } 

The problem with your code is that the next segment will always be true if the user is logged in. You should check if a specific guard is set inside this if if you want to redirect accordingly.

if (Auth::guard($guard)->check()) { return redirect('/home'); }

+2
source

Source: https://habr.com/ru/post/1272926/


All Articles