Laravel multi auth






  1. make default auth 
    1. php artisan make:auth
  2. make amother model+migration 
    1. then extend that model by 'Authenticatable' and customize and set all things as like as default auth model. 
    2. Add to the migration, remember token column and others columns => migrate as wel  



========================================================================
  1. ** Adding more guard for extra auth to use the extra auth as like as default auth: **
    1. Config => auth.php
      • code : go to   'providers' and make another scope like users .
      • code : go to  'guards'   Make new guard like web [ default guard ] .  [FOR WEB ]


      • 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]

      •  
  1. 👌👌 Everything setup complete for multi auth. : JUST 1 thing should do more 👀😂 
      1. 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.

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 =[] =>

  1. 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 -> 

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:





মন্তব্যসমূহ

এই ব্লগটি থেকে জনপ্রিয় পোস্টগুলি

SEO [Search Engine Optimization ] (সার্চ ইঞ্জিন অপ্টিমাইজেশান)

Web Scraping With PHP