jeudi 25 janvier 2018

Multiple Auth using JWT auth every-time i get wrong password

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'

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 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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire