1
votes

I have 2 user i.e user and bus and for them i have different model I am using laravel 5.5 with tymondesigns/jwt-auth 1.0.0-rc.1 verson

user is working perfect for user i am getting token also but bus user us getting 'invalid_email_or_password'

link to full code full source code link

here is my user model:

    class User extends Authenticatable implements JWTSubject{
use Notifiable;
protected $fillable = ['name', 'email', 'password'];
public function getJWTIdentifier()
{
    return $this->getKey();
}
public function getJWTCustomClaims()
{
    return [];
}}  

my config/auth.php

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

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

    'bus' => [
        'driver' => 'session',
        'provider' => 'buses',
    ],
    'bus-api' => [
        'driver' => 'jwt',
        'provider' => 'buses',
    ],
],

my provider are :

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

    //next
    'buses' => [
        'driver' => 'eloquent',
        'model' => App\Bus::class,
    ],

my my buslogincontroller which is taking user name & password for user table

    public function busLogin(Request $request)
{
    \Config::set('jwt.user', "App\Bus");
    \Config::set('auth.providers.users.model', \App\Bus::class);
    $credentials = $request->only('email', 'password');
    try {
        \Config::set('jwt.user', "App\Bus");
        \Config::set('auth.providers.users.model', \App\Bus::class);
        if (!$token = JWTAuth::attempt($credentials)) {
            \Config::set('jwt.user', "App\Bus");
            \Config::set('auth.providers.users.model', \App\Bus::class);
            return response()->json([
                'response' => 'error',
                'message' => 'invalid_email_or_password',
            ]);
        }
    } catch (JWTAuthException $e) {
        return response()->json([
            'response' => 'error',
            'message' => 'failed_to_create_token',
        ]);
    }
    return response()->json([
        'response' => 'success',
        'result' => [
            'token' => $token,
            'message' => 'I am Admin user',
            'user' => '99',
        ],
    ]);
}

my routes api:

Route::post('bus/auth/login', 'Bus\Auth@busLogin');
Route::post('bus/auth/register', 'Bus\Auth@busRegister');

whenever I try to login with Bus model username & password i get invalid login in buscontroller login route but if i try to login with user model credintials i get token in return how to setup multiple auth with jwtauth with laravel 5.5

2
Where are you setting the authentication guards for users and buses? Kernel? RouteServiceProvider? I would be helpful to see that. - user320487
i am setting up here config/auth.php full source code here link . thanks - Kobid Kunda
It's much more useful to have the code posted within your question. I'd rather not dig through your repository when a simple edit would go further. - user320487
@btl . i config/auth.php is already given above thanks in advance - Kobid Kunda
You misunderstand, where are you setting which guard a route will use? Route::group(['middleware' => ['auth:api',...], function () { ...routes...}); for example. - user320487

2 Answers

1
votes

There is no need to change the providers in config/auth.php.

You can change the __construct function in each of your controllers as follows. So that jwt know which model to authenticate.

BusController

function __construct()
{
    Config::set('jwt.user', Bus::class);
    Config::set('auth.providers', ['users' => [
            'driver' => 'eloquent',
            'model' => Bus::class,
        ]]);
}

AdminController

function __construct()
{
    Config::set('jwt.user', Admin::class);
    Config::set('auth.providers', ['users' => [
            'driver' => 'eloquent',
            'model' => Admin::class,
        ]]);
}
0
votes

You just need to set below code before route call in route api

\Config::set('jwt.user', "App\Bus");
\Config::set('auth.providers.users.model', \App\Bus::class);