tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.3k stars 1.54k forks source link

Multiple table authentication #111

Closed dixitchopra closed 9 years ago

dixitchopra commented 9 years ago

I have two different tables user_credentials and merchant_credentials. How can I authenticate one table at a time based on login. Merchant login will be different than User login.

pfazzino commented 9 years ago

This isn't really a jwt-auth concern, you need to implement a custom UserProvider and decide at request time how you want to authenticate the request. All jwt does is delegate to the UserProvider and authenticates based on the identifier in the token by calling retrieveById on your UserProvider. Take a look here for help on extending Laravel authentication - http://laravel.com/docs/5.0/extending#authentication

tymondesigns commented 9 years ago

@pfazzino is right.. although I have made this slightly easier over on develop.

In the new version you will have to implement the JWTAuthSubject interface and specify the identifier and any custom claims. Meaning this can be done on multiple entities/models

pfazzino commented 9 years ago

When do you plan to roll develop into a new release? Nice to see the token parsing pulled into jwt-auth directly. Having to add middleware to fix this was easy once I realised what was going on but I'll be happy to throw it out.

On 13 May 2015 at 22:27, Sean Tymon notifications@github.com wrote:

@pfazzino https://github.com/pfazzino is right.. although I have made this slightly easier over on develop https://github.com/tymondesigns/jwt-auth/tree/develop.

In the new version you will have to implement the JWTAuthSubject https://github.com/tymondesigns/jwt-auth/blob/develop/src/JWTAuthSubject.php interface and specify the identifier and any custom claims. Meaning this can be done on multiple entities/models

— Reply to this email directly or view it on GitHub https://github.com/tymondesigns/jwt-auth/issues/111#issuecomment-101820576 .

tymondesigns commented 9 years ago

roughly in the next couple of weeks I think.. need to fix some of the tests and make sure everything is tight

geoah commented 9 years ago

Is there an example of how this (the multi table auth) could be achieved using subjects? Sounds like just what we need here! :) Thanks.

geea-develop commented 8 years ago

why not use \Config::set('jwt.user' , "App\Customer"); for different tables in middleware ?

joelezeu commented 8 years ago

geea789 I followed your instruction and I'm getting my jwt token, but the problem now is that I can't use the token to retrieve my user details.

I'm always getting user_not_found

geea-develop commented 8 years ago

hi joelezeu have a look at tymon/jwt-auth/src/Middleware/GetUserFromToken.php check why your user is not found. you might need to rewrite this middleware for your needs (then place it in your own middleware folder) and replace its path for jwt.auth in Kernel.php under $routeMiddleware

mcblum commented 7 years ago

I'm having the same issue always getting user_not_found after changing the config. Any ideas how to solve this?

ghost commented 7 years ago

I would like to ask how to I make the JWT authenticate for multiple table ? For now JWT worked fine for my user table ( sign in and sign up ) then I implement it to my commercial table, it can sign up which come out token, but it cannot sign in. it always stated "commercials not found" .

I tried the method of \Config::set('jwt.user' , "App\Commercial"); however, it stated " Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of App\Commercial given, called in C:\xampp\htdocs\laravel-projects\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line 379"

Please help me. Thanks for your attention

fercho9212 commented 7 years ago

I worked with \Config::set('jwt.user' , "App\Dirver"); but when I log in I get the token from the table driver but that same token lets me log in to the other table as I can avoid that Thanks.

nsanzumuhire commented 7 years ago

to change config of the user model to be authenticated is working for example i have Drivers and Passengers tables which are different because driver will have too much info and in my AdminController i have a function to login driver so i have to set Config before $token = JWTAuth::attempt($credentials)

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

and another function to login a passenger

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

and its working but the problem i don't know to use these on my apis routes because they are confused sometimes i am getting data or user_not_found !

i want drivers and passengers to access specifics api routes and like this is for drivers

   Route::group(['prefix'=>'drivers','middleware' => ['jwt.auth']], function() {  
       Route::post('register/vehicle', 'VehicleController@RegisterVehicle');   
  });

and this for passengers

 Route::group(['prefix'=>'passengers','middleware' => ['jwt.auth']], function() {  
       Route::post('join/vehicle', 'PassengerController@RegisterVehicle');   
  });