1

I'm trying to build a laravel application and API for mobile users in the same project. The main web is actually a backend of a website and require proper authentication that I have already implemented. And the api also need to authenticate but with different credentials (not user model from web).

At this point I have added dingo api in my project and its working fine. But now I also need to add jwt for dingo. Problem is jwt works with App\User Model by default and I don't want to authenticate api users with web user credentials. Please suggest me what is the best way to work this out. Thanks

AHSAN
  • 101
  • 1
  • 3
  • 10
  • In case someone comes here to search for the answer, I have written an article for this topic: https://medium.com/@sirajul.anik/passwordless-otp-based-login-returning-jwt-with-tymon-jwt-auth-d463287cb6ca – ssi-anik Jul 23 '20 at 04:46

1 Answers1

0

Looking over the docs its looks like you specify the auth provider in the config.php file that get published with php artisan vendor:publish

Set the auth to your provider: https://github.com/tymondesigns/jwt-auth/blob/develop/config/config.php#L252

Or you can specify which driver and/or user provider you are using in the app.php config file:

https://github.com/laravel/laravel/blob/master/config/auth.php#L69 https://github.com/laravel/laravel/blob/master/config/auth.php#L70

Update:

  1. Make migration to hold clients table: php artisan make:migration create_clients_table

  2. Update the migration file as desired.

  3. Create the Clients model with php artisan make:model Client

  4. Update the model to match the migration and add any relationships.

  5. Run the migration.

That should make it so this will work

You may need to update accordingly to something like:

'api' => [
    'driver' => 'token',
    'provider' => 'clients',
],

or possibly:

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

https://github.com/laravel/laravel/blob/master/config/auth.php#L46

jeremykenedy
  • 4,150
  • 1
  • 17
  • 25
  • Yes I have already tried that auth.php configuration. I'm new to laravel. Do I have to create another provider for this? in my auth.php I have enabled 2nd array for api guard. And ` 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'clients' => [ 'driver' => 'eloquent', 'model' => App\Client::class, ], ],` – AHSAN Dec 28 '17 at 05:09
  • What is the "proper authentication" you implemented? – jeremykenedy Dec 28 '17 at 05:14
  • Yes I have already tried that auth.php configuration. I'm new to laravel. Do I have to create another provider for this? in my auth.php I have enabled 2nd array for api guard. [link] http://prntscr.com/ht7cjv [/link] and then api routes in my routes/api.php so technically it should use client model right? – AHSAN Dec 28 '17 at 05:15
  • laravel builtin auth – AHSAN Dec 28 '17 at 05:16
  • adding another key will not help in this situation unless you are referencing off that key – jeremykenedy Dec 28 '17 at 05:16
  • Do I have to create my own provider? – AHSAN Dec 28 '17 at 05:17
  • If you are using default built in authentication, then there is a default `User.php.` model. https://github.com/laravel/laravel/blob/master/app/User.php – jeremykenedy Dec 28 '17 at 05:19
  • Yes but I don't want to use User.php model for my api requests. I want to create another Model. Lets call it Client. That simply has key and secret which generates token. No password or anything. Api users are different in my case. – AHSAN Dec 28 '17 at 05:20
  • So do you want the JWT token to be completely separate from users, then you would need to create a new model and migration to hold that. – jeremykenedy Dec 28 '17 at 05:23
  • I have already created that model and its migrations. but the problem is when I try to generate token, it is still going to User Model. I have even defined authentication routes in routes/api.php so so these requests should use api guard that is App/Client right? – AHSAN Dec 28 '17 at 05:27
  • @AhsanShabbir I had the same problem and created an own authentication provider and referenced in `config/jwt.php`. In this provider you can implement the lookup method yourself. – Alex2php Jan 07 '18 at 19:19