0
votes

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.

3
I think the guard driver can either be token or session.apokryfos
those are the default options, but we can use custom guard. Does it needs extra settings, that I'm not sure.!!Tarunn
Yes, you can use a custom guard but the guard's backing driver can either be token or session.apokryfos
I wish to use the jwt tokens. Can u guide me to integrate jwt. I tried with token,but they are giving some other error...Tarunn
The vital part here is not the guard driver but the guard provider. That's what you need to implement. The driver is just what will be used to lookup the current request's id which will be passed to the provider so the provider can look up the credentials (if any).apokryfos

3 Answers

0
votes
Auth Guard driver is defined in config/auth.php 

Like below

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

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

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

and also add in providers like

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

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

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

Try to clear the config cache php artisan config:clear or rebuild it php artisan config:cache

0
votes

You will have to also extend your authentication by adding this to the boot() method of your app's AuthServiceProvider:

public function boot()
{
    $this->registerPolicies();

    Auth::extend('customers', function ($app, $name, array $config) {
        return new CustomersGuard();
    });
}

See the documentation for adding custom guards