jwt-auth: Can't get token with Laravel 6.0 and jwt-auth 1.0.0-rc.5 when trying to authenticate
Token value is always false
Hello. I used jwt-auth 1.0.0-rc.5 to handle authentication with laravel 6.0. My AuthController code is pasted below.
When registering a new user with the method register in AuthController, I succesfully get a jwt token.
But when trying to athenticate the user I created earlier, token value is always false
`<?php
namespace App\Http\Controllers;
use DB; use Hash; use App\User; use App\RoleUser; use Illuminate\Http\Request;
class AuthController extends Controller { public function authenticate(Request $request) { $credentials = $request->only(‘email’, ‘password’);
$token = auth()->attempt($credentials);
// var_dump(Hash::make($credentials['password']));
// var_dump($token);
if($token)
return $token;
return response()->json(['error' => 'Unauthorized'], 401);
}
public function register(Request $request)
{
$request->validate([
'role' => 'required',
'email' => 'required|unique:users',
'password' => 'required',
'firstname' => 'required',
'lastname' => 'required'
],[
'role' => 'Le niveau de role est requis',
'email.unique' => "L'adresse email est requise",
'password' => 'Le mot de passe est requis',
'firstname' => 'Le prénom est requis',
'lastname' => 'Le nom de famille est requis'
]);
DB::beginTransaction();
$user = User::create([
'email' => $request->email,
'password' => $request->password,
'firstname' => $request->firstname,
'lastname' => $request->lastname
]);
RoleUser::create([
'role_name' => $request->role,
'user_id' => $user->id
]);
DB::commit();
$token = auth()->login($user);
// var_dump($token);
return $this->respondWithToken($token);
}
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60
]);
}
} `
`<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
]; `
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 2
- Comments: 30
@I2C-RoyYou Hi,i has been sloved this problem. You can read the wiki from https://github.com/tymondesigns/jwt-auth/wiki/Authentication
It tell us , when you login by the api middleware(jwt), and then, if you want to refresh the user’s token,you need to parsed the user’s token in request headers or in input request params firstly.
LOOK AT THIS:
DO NOT READ THE
https://jwt-auth.readthedocs.io/en/develop/auth-guard/, IT CAN NOT TELL US ABOUT DEFAULT GUARD WHICH IS NOTapiMY CODE, MAY BE HELPFUL TO U:
IMPORTANT :
JWTAuth::parseToken()->refresh()now, you can put the header parma
Authorization: bearer {user's token}for refresh user’s token from your clienthave you try this? I use this and success auth!