Login

  Thu 27 / 04 / 2023

  Posted by: Tarun Kumar Gupta

Category : Laravel

Tags : php laravel , backend

Description

I am not able to restrict user to redirect login page when it is logged in and also unable to restrict to redirect on dashboard when it is not logged in.
please give idea.


  Answered by CDL

Yes it's very simple in Laravel. You achieve this functionality using middleware, in middleware you can add the logic that if the user is logged in then he/she can visit the dashboard, and if the user is logged in and want to visit the login page, he will be redirected on the dashboard because he is already login.

In Laravel auth middleware comes default which checks that user is login or not

How to apply this middleware?

Simply go to your routes files and call this middleware like this ->middleware('auth');

Example route with middleware

~~~

Route::get('/home', [HomeController::class, 'home'])->middleware('auth');

~~~

That's it

Solved
  Comment   Share
1 Like   1 Comment


comment-author
Tarun Kumar Gupta Author

Thanks for answering.



Reply


commented 1 year ago
comment-author
Hadayat Niazi Super Admin

Glad to help you.