Multi Auth with Laravel 5.4 and Passport

后端 未结 7 2083
猫巷女王i
猫巷女王i 2020-12-31 20:46

I am trying to setup multi auth with Laravel Passport, but it doesn\'t seem to support it. I am using the Password Grant to issue tokens which requires me to pass username/p

7条回答
  •  隐瞒了意图╮
    2020-12-31 21:37

    I have created a small package for this issue. Here's the link for the complete doc link

    But the gist is, whenever a user entity gets logged in, it checks for the guards and providers.

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    
        'customers' => [
            'driver' => 'passport',
            'provider' => 'customers'
        ],
    ],
    
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => 'App\User',
        ],
        /**
         * This is the important part. You can create as many providers as you like but right now, 
         * we just need the customer
         */
         'customers' => [
             'driver' => 'eloquent',
             'model' => 'App\Customer',
         ],
    ],
    

    You should have a controller like this:

    getBody()->__toString();
             $token = json_decode($body, true);
    
             if (array_key_exists('error', $token)) {
                 return response()->json([
                     'error' => $token['error'],
                     'status_code' => 401
                 ], 401);
             }
    
            $data = $request->getParsedBody();
    
            $email = $data['username'];  
    
            switch ($data['provider']) {
                case 'customers';
    
                    try {
    
                     $user = Customer::where('email', $email)->firstOrFail();
    
                    } catch (ModelNotFoundException $e) {
                      return response()->json([
                          'error' => $e->getMessage(),
                          'status_code' => 401
                      ], 401);
                    }
    
                    break;
    
                default :
    
                    try {
    
                     $user = User::where('email', $email)->firstOrFail();
    
                    } catch (ModelNotFoundException $e) {
                      return response()->json([
                          'error' => $e->getMessage(),
                          'status_code' => 401
                      ], 401);
                    }        
            }
    
            return compact('token', 'user');
        }
    }
    

    and the request should be:

    POST /api/oauth/token HTTP/1.1
    Host: localhost
    Content-Type: application/x-www-form-urlencoded
    Cache-Control: no-cache
    
    grant_type=password&username=test%40email.com&password=secret&provider=customers
    

    To check in your controller who is the logged in user, you can do:

    auth()->guard('customers')->user()

提交回复
热议问题