Tuesday, February 28, 2017

Laravel 5.4 Auth guard driver [customers] is not defined

I had implemented custom auth in L5.2. I had followed those same steps but, I am not able to login/signup with custom auth. Following is the setup:

in auth.php i added customers custom auth:

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

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

    'customers' => [
        'driver' => 'jwt',
        'provider' => 'customers',
    ],


],

// Providers Section

providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

     'customers' => [
         'driver' => 'eloquent',
         'model' => App\Customer::class,
     ],
],

Then in routes/api.php I added following code after removing middleware from RouteServiceProvider.php

Route::group(['middleware' => 'customers'], function() {

    Route::post('login', 'JwtAuth\LoginController@login'); //Had made new auth for JWT
}

When I hit this login, instead of Customer table, Auth is done from User table!!

I also tried with following code inside Controller\JwtAuth\LoginController.php :

public function login(Request $request)
{
    $credentials = $request->only('email', 'password');
    $customer = Auth::guard('customers')->attempt($credentials);

    try {
        // attempt to verify the credentials and create a token for the user
        if (!$token = JWTAuth::attempt($credentials)) {
            return response()->json(['error' => 'invalid_credentials'], 401);
        }
    } catch (JWTException $e) {
        // something went wrong whilst attempting to encode the token
        return response()->json(['error' => 'could_not_create_token'], 500);
    }
    // all good so return the token
    return response()->json(compact('token'), Response::HTTP_OK);
}

This code throws error as:

Auth guard driver [customers] is not defined.

In my \App\Http\Kernel.php under protected $middlewareGroups i had added:

'api' => [
        'throttle:60,1',
        'bindings'
    ],

'customers' => [
        'throttle:60:1',
        'bindings'
    ]

Is there any change in token driver or custom driver. Or how to define custom Auth driver?

Any help/guidance would b much appreciated. Thanks in advance.




via Tarunn

Advertisement