Open mikahoy045 opened 8 years ago
I have the same issue -- it creates a token but then when I try to use it I always get user_not_found
.
I'm trying the following in a middleware that specifically switches the config to use a different table for users:
Config::set('auth.providers.users.model', OtherUser::class);
Config::set('auth.providers.users.table', 'other_users');
Any ideas as to why it won't then find the user when I authenticate?
Are you using laravel 5.2? If yes then as far as i know It still read your old table (if you want to check it then try put your user and pass in old table). when you create your data bcrypt your password (there are issue with hash). Wrap your jwt.auth middleware group with your custom middleware group you created above (config middleware). Dont forget to change config/jwt user to your model and check idwntifier
@mikahoy045 I'm on 5.3. When the token and user object are returned the first time, everything is correct and the user has id:6 in the other_users
table. The issue is only when I try to validate a token, it's seeing the token is good and going to find the user but it looks in users
rather than other_users
.
Thats right, now wrap your jwt.auth middleware group in route with your custom middleware group you created above (config middleware). Dont forget to change config/jwt user to your model and check identifier, cause replacing config/jwt user is not enough.
@mikahoy045 ok, I did exactly that. No dice. I added a third line:
Config::set('auth.providers.users.model', PlotlyUser::class);
Config::set('auth.providers.users.table', 'plotly_users');
Config::set('jwt.user', OtherUser::class);
Still getting user_not_found
. Do I need to do anything to the actual OtherUser model? Is that why it's maybe not working?
Also thank you for your help on this -- it's messing up my day!
So there should be 1 last thing.... do you implements AuthenticatableContract, CanResetPasswordContract on your new user model as a trait?
Just got new info :
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class Newuser extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword
}
After that do composer dump autoload PS: I never heard about that 3rd line in custom middleware
I have the same problem using laravel 5.3. I did the following and so far have no problem when login normally from my local:
config/auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'member' => [
'driver' => 'session',
'provider' => 'members',
], ........
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'members' => [
'driver' => 'eloquent',
'model' => App\Member::class,
], .........
],
MemberLoginController.php
public function loginMember(Request $request)
{
auth()->shouldUse('member');
$credentials = $request->only('email', 'password');
.....
}
I don't know if this is the correct way to solve this problem
Another reason is to use the user object to create a license
$token = JWTAuth::fromUser($user)
try { // attempt to verify the credentials and create a token for the user if (!$token = JWTAuth::fromUser($user)) { return response()->json(['error' => 'invalid_credentials'], 401); } } catch (JWTException $e) { // something went wrong whilst attempting to encode the token return response()->json(['error' => 'could_not_create_token'], 500); }
Having same issue can any one help out
`<?php
namespace App\Libraries;
use Tymon\JWTAuth\Contracts\Providers\Auth;
// @todo 后期考虑使用中间件的方式 class ApiAuthProvider implements Auth { /**
@var \Illuminate\Contracts\Auth\Guard */ protected $auth;
/**
@return void */ public function __construct() { $this->auth = app('auth')->gurad('api'); }
/**
@return bool */ public function byCredentials(array $credentials) { return $this->auth->once($credentials); }
/**
@return bool */ public function byId($id) { return $this->auth->onceUsingId($id); }
/**
<?php/*
return [
/*
|--------------------------------------------------------------------------
| JWT Authentication Secret
|--------------------------------------------------------------------------
|
| Don't forget to set this in your .env file, as it will be used to sign
| your tokens. A helper command is provided for this:
| `php artisan jwt:secret`
|
| Note: This will be used for Symmetric algorithms only (HMAC),
| since RSA and ECDSA use a private/public key combo (See below).
|
*/
'secret' => env('JWT_SECRET'),
/*
|--------------------------------------------------------------------------
| JWT Authentication Keys
|--------------------------------------------------------------------------
|
| The algorithm you are using, will determine whether your tokens are
| signed with a random string (defined in `JWT_SECRET`) or using the
| following public & private keys.
|
| Symmetric Algorithms:
| HS256, HS384 & HS512 will use `JWT_SECRET`.
|
| Asymmetric Algorithms:
| RS256, RS384 & RS512 / ES256, ES384 & ES512 will use the keys below.
|
*/
'keys' => [
/*
|--------------------------------------------------------------------------
| Public Key
|--------------------------------------------------------------------------
|
| A path or resource to your public key.
|
| E.g. 'file://path/to/public/key'
|
*/
'public' => env('JWT_PUBLIC_KEY'),
/*
|--------------------------------------------------------------------------
| Private Key
|--------------------------------------------------------------------------
|
| A path or resource to your private key.
|
| E.g. 'file://path/to/private/key'
|
*/
'private' => env('JWT_PRIVATE_KEY'),
/*
|--------------------------------------------------------------------------
| Passphrase
|--------------------------------------------------------------------------
|
| The passphrase for your private key. Can be null if none set.
|
*/
'passphrase' => env('JWT_PASSPHRASE'),
],
/*
|--------------------------------------------------------------------------
| JWT time to live
|--------------------------------------------------------------------------
|
| Specify the length of time (in minutes) that the token will be valid for.
| Defaults to 1 hour.
|
| You can also set this to null, to yield a never expiring token.
| Some people may want this behaviour for e.g. a mobile app.
| This is not particularly recommended, so make sure you have appropriate
| systems in place to revoke the token if necessary.
|
*/
'ttl' => env('JWT_TTL', 600),
/*
|--------------------------------------------------------------------------
| Refresh time to live
|--------------------------------------------------------------------------
|
| Specify the length of time (in minutes) that the token can be refreshed
| within. I.E. The user can refresh their token within a 2 week window of
| the original token being created until they must re-authenticate.
| Defaults to 2 weeks.
|
| You can also set this to null, to yield an infinite refresh time.
| Some may want this instead of never expiring tokens for e.g. a mobile app.
| This is not particularly recommended, so make sure you have appropriate
| systems in place to revoke the token if necessary.
|
*/
'refresh_ttl' => env('JWT_REFRESH_TTL', 20160),
/*
|--------------------------------------------------------------------------
| JWT hashing algorithm
|--------------------------------------------------------------------------
|
| Specify the hashing algorithm that will be used to sign the token.
|
| See here: https://github.com/namshi/jose/tree/master/src/Namshi/JOSE/Signer/OpenSSL
| for possible values.
|
*/
'algo' => env('JWT_ALGO', 'HS256'),
/*
|--------------------------------------------------------------------------
| Required Claims
|--------------------------------------------------------------------------
|
| Specify the required claims that must exist in any token.
| A TokenInvalidException will be thrown if any of these claims are not
| present in the payload.
|
*/
'required_claims' => [
'iss',
'iat',
'exp',
'nbf',
'sub',
'jti',
],
/*
|--------------------------------------------------------------------------
| Persistent Claims
|--------------------------------------------------------------------------
|
| Specify the claim keys to be persisted when refreshing a token.
| `sub` and `iat` will automatically be persisted, in
| addition to the these claims.
|
| Note: If a claim does not exist then it will be ignored.
|
*/
'persistent_claims' => [
// 'foo',
// 'bar',
],
/*
|--------------------------------------------------------------------------
| Lock Subject
|--------------------------------------------------------------------------
|
| This will determine whether a `prv` claim is automatically added to
| the token. The purpose of this is to ensure that if you have multiple
| authentication models e.g. `App\User` & `App\OtherPerson`, then we
| should prevent one authentication request from impersonating another,
| if 2 tokens happen to have the same id across the 2 different models.
|
| Under specific circumstances, you may want to disable this behaviour
| e.g. if you only have one authentication model, then you would save
| a little on token size.
|
*/
'lock_subject' => true,
/*
|--------------------------------------------------------------------------
| Leeway
|--------------------------------------------------------------------------
|
| This property gives the jwt timestamp claims some "leeway".
| Meaning that if you have any unavoidable slight clock skew on
| any of your servers then this will afford you some level of cushioning.
|
| This applies to the claims `iat`, `nbf` and `exp`.
|
| Specify in seconds - only if you know you need it.
|
*/
'leeway' => env('JWT_LEEWAY', 0),
/*
|--------------------------------------------------------------------------
| Blacklist Enabled
|--------------------------------------------------------------------------
|
| In order to invalidate tokens, you must have the blacklist enabled.
| If you do not want or need this functionality, then set this to false.
|
*/
'blacklist_enabled' => env('JWT_BLACKLIST_ENABLED', true),
/*
| -------------------------------------------------------------------------
| Blacklist Grace Period
| -------------------------------------------------------------------------
|
| When multiple concurrent requests are made with the same JWT,
| it is possible that some of them fail, due to token regeneration
| on every request.
|
| Set grace period in seconds to prevent parallel request failure.
|
*/
'blacklist_grace_period' => env('JWT_BLACKLIST_GRACE_PERIOD', 0),
/*
|--------------------------------------------------------------------------
| Cookies encryption
|--------------------------------------------------------------------------
|
| By default Laravel encrypt cookies for security reason.
| If you decide to not decrypt cookies, you will have to configure Laravel
| to not encrypt your cookie token by adding its name into the $except
| array available in the middleware "EncryptCookies" provided by Laravel.
| see https://laravel.com/docs/master/responses#cookies-and-encryption
| for details.
|
| Set it to false if you don't want to decrypt cookies.
|
*/
'decrypt_cookies' => true,
/*
|--------------------------------------------------------------------------
| Providers
|--------------------------------------------------------------------------
|
| Specify the various providers used throughout the package.
|
*/
'providers' => [
/*
|--------------------------------------------------------------------------
| JWT Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to create and decode the tokens.
|
*/
'jwt' => Tymon\JWTAuth\Providers\JWT\Lcobucci::class,
/*
|--------------------------------------------------------------------------
| Authentication Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to authenticate users.
|
*/
// 'auth' => Tymon\JWTAuth\Providers\Auth\Illuminate::class,
'auth' => App\Libraries\ApiAuthProvider::class,
/*
|--------------------------------------------------------------------------
| Storage Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to store tokens in the blacklist.
|
*/
'storage' => Tymon\JWTAuth\Providers\Storage\Illuminate::class,
],
]; `
Changing model with Config::set()
not effect on auth. It changes but not effect!
any ETA on this? We have a use case for this and new version doesn't supports this for long enough now I guess.
If we can contribute in any way or if PR's are accepted we will be happy to provide a fix for this.
Appreciate all the hard work done so far on this.
in Your User class model add the following:
protected $table = 'your_table_name;'
Can anyone please tell me where to change the default User table of JWT in the yii2 framework. ?
Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
I have 2 auth system using Laravel 5.2. One for apps (I use a Model called "User") and others for API (I use Model called "Ngate")
My Model :
Controller
But why it always result in invalid credential. If i set it back to users table, everything works fine. PS: I already change config/jwt 'user' => 'App\Ngate' and identifier to id.