voilatech
1 week ago
177
9
Laravel

Laravel 6 custom guard issue

Posted 1 week ago by voilatech

Hi Guys, I came across an issue with laravel 6 Auth where I added a new custom guard and when trying to authenticate a user, the Auth would check it with the default guard which is the api and it points to users table. Not to mention the default api guard, it works just fine! Even I tried to add the protected guard method in my Controller,

    protected function guard()
    {
        return \Illuminate\Support\Facades\Auth::guard('partner_api');
    }

but got no luck!

my Controller:

 public function __construct()
    {
            $this->middleware('auth:partner_api');
    }

    public function loginPartner(){
        if(Auth::attempt([
                'partner_email' => request('partner_email'), 'password' => request('password')]) && isset
            (Auth::user()
                    ->email_verified_at)){
            $partner = Auth::user();

            $success['token'] = $partner->createToken('MyApp')->accessToken;
            $success['partner'] = Partner::with('partnerAddresses')->find($partner->id);

            return response()->json(['success' => $success], 201);
        }
        else{
            return response()->json(['error'=>'Unauthorised'], 401);
        }
    }

My Auth.php

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
            'hash' => false,
        ],
        'partner_api' => [
            'driver' => 'passport',
            'provider' => 'partners',
            'hash' => false,
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'partners' => [
            'driver' => 'eloquent',
            'model' => App\Partner::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

my Model:

    protected  $guard = 'partner_api';

Any thought?

Please sign in or create an account to participate in this conversation.