Laravel multi auth
- make default auth
- php artisan make:auth
- make amother model+migration
- then extend that model by 'Authenticatable' and customize and set all things as like as default auth model.
- Add to the migration, remember token column and others columns => migrate as wel
========================================================================
- Note: No need to change or add another API until you want to check auth by external API. Or we can just add another API guard for farther use. :P [FOR API]
- 👌👌 Everything setup complete for multi auth. : JUST 1 thing should do more 👀😂
- set up password reset :-
**Now identifying which guard will work for which model **
Make login and registration or something else and add routes as your wish.
but the problem gonna be happen with the middle ware redirect .
in logout mood: when we trying to hit '/admin' it should redirect us to '/admin/login'
but it will redirect us only '/login' which is not expected.
but it will redirect us only '/login' which is not expected.
so,
Configuring Multi Auth Middleware:
- redirect URL is hardcoded for middleware. so when it try to kick out to another link for login/logout mood it try the links for for users/default auth.
- App => Http => Karnel.php
- $routeMiddleware =[] =>
- Redirect when not authenticate:
if there is a page that is protected by 'auth' middleware we will make sure to redirect them to the correct login page if they are not logged in: [1st problem ]
from here when we are not login => it throws an exception to => App=>Exceptions=>Handler.php ->
- $routeMiddleware =[] =>
unauthenticated() => if 5.5+ laravel hoile code will change to render() method...
Code:
public function render($request, Exception $exception){ if ($request->expectsJson()) { return response()->json(['message' => 'Unauthenticated.'], 401); } $guard = array_get($exception->guards(), 0); switch ($guard) { case 'admin': $login = 'admin.login'; break; case 'vendor': $login = 'vendor.login'; break; default: $login = 'login'; break; } return redirect()->guest(route($login)); //return parent::render($request, $exception);}
2. Redirect when authenticate:
if there is a page that is protected by 'guest' middleware
the guest middleware only wants people that are not logged in : so if you logged in so we don't want you to seeing that page[login form] instead we normally would redirect you the home page or admin dashboard correctly by user/admin check [2nd problem ]
app=> Http => Middleware => RedirectIfAuthenticated.php
Handle()
now redirection problem solved.
- Logout problem:
মন্তব্যসমূহ
একটি মন্তব্য পোস্ট করুন