Reputation: 131
I have a laravel 5 backend that sends an jwt-token as a json response (with https://github.com/tymondesigns/jwt-auth). The user authentication works correctly.
What i need to do is to use jwt-token for admin authentication too. Admins are on different table named admin.
How can i use jwt-token for different tables?
Upvotes: 6
Views: 7838
Reputation: 622
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.
AdminController
function __construct()
{
Config::set('jwt.user', Admin::class);
Config::set('auth.providers', ['users' => [
'driver' => 'eloquent',
'model' => Admin::class,
]]);
}
Upvotes: 3
Reputation: 1020
It worked for me . you can use them
public function userLogin(Request $request){
Config::set('jwt.user', 'App\User');
Config::set('auth.providers.users.model', \App\User::class);
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
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 front user',
],
]);
}
public function adminLogin(Request $request){
Config::set('jwt.user', 'App\Admin');
Config::set('auth.providers.users.model', \App\Admin::class);
$credentials = $request->only('email', 'password');
$token = null;
try {
if (!$token = JWTAuth::attempt($credentials)) {
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',
],
]);
}
Upvotes: 1
Reputation: 55
It worked for me , I have 2 users client and salon_owner . To change I updated 2 things auth.model and jwt.user :
Config::set('jwt.user', 'App\Salon_owner');
Config::set('auth.providers.users.model', \App\SalonOwner::class);
Upvotes: 3
Reputation: 200
I had similar problem and for me was enough just create route middleware and update auth.model (needed to change only \App\User to \App\Customer):
Config::set('auth.model', App\Customer::class);
Tables and other db things you should have defined in models, so above entry should do work.
Upvotes: 3
Reputation: 196
currently that package do not have multiple table support for authentication of your application users.
What you can do for example is:
Create middleware for routes where you need admin table to work together with jwt-auth.
In that middleware apply config changes to the jwt and change model before authentication or working with jwt token.
PREFERED SOLUTION:
You can maybe see here for this package which is not connected to any of configuration, it is only a wrapper to work with JWT.
https://github.com/firebase/php-jwt
It is a lib for encoding and decoding JWT. You have your hands free to do what you want and also you can set up your key for every token. In your case I would use that.
Create wrapper or repositories to handle authentication like attempt, check, etc. and wrap them around this package.
Hope this will help you have to make your own solution.
Cheers.
Upvotes: 1