2
votes

https://github.com/tymondesigns/jwt-auth is working perfectly for authenticating users for defualt guard but I want to login users from another table (auth:guard)

Here is config/auth.php:

'defaults' => [
        'guard' => 'user',
        'passwords' => 'users',
    ],

 'guards' => [
        'user' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
        'student' => [
            'driver' => 'jwt',
            'provider' => 'students',
        ],
        'father' => [
            'driver' => 'jwt',
            'provider' => 'fathers',
        ],
        'mother' => [
            'driver' => 'jwt',
            'provider' => 'mothers',
        ],
    ],

As I mentioned, jwt authentication is working for 'guard' => 'user' as it is default one but I want to authenticate student using 'guard' => 'student' without changing default guard.

Here is my login function:

public function login(Request $request){
        $credentials = $request->only('email', 'password');
        $jwt = '';
        try {
            if (!$jwt = JWTAuth::attempt($credentials)) {
                return response()->json([
                    'response' => 'error',
                    'message' => 'invalid_credentials',
                ], 401);
            }
        } catch (JWTAuthException $e) {
            return response()->json([
                'response' => 'error',
                'message' => 'failed_to_create_token',
            ], 500);
        }
        return response()->json([
            'message' => 'success',
            'result' => ['token' => $jwt]
        ]);
    }

For better understanding, I have followed all steps showed in documentation to install tymon/jwt-auth version dev-develop. Any help is highly appreciated

2
did you find any solution for this?Yousef Altaf
yeah. this function auth()->shouldUse('father'); but i couldn't find exactly what I wantRahimjon Rustamov

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.

FatherController

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

StudentController

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

Try something like this

 public function login(Request $request)
{

    //validate the user credentials 


$this->validate($request,[
      'email'=>'required | email',
      'password'=>'required',
]);

 //attempt to log in the admin

if(Auth::guard('student')->attempt(['email'=>$request->email,
'password'=>$request->password],$request->remember)){

//if successful, then redirect to their intended location 

return redirect()->intended(route('dashboard'));



    }

    // if unsuccessful, then redirect them back to the login form with form data

    return redirect()->back()->withInput($request->only('email','remember'));
}