Reputation: 185
if i set auth.php like this -->>
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
login maintains if i go mainpage , and return token is only true/false value like this {"access_token":true,"token_type":"bearer","expires_in":3600}
if i set auth
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
login can't maintain when i go to main page, but token will return fine like
{"access_token":eY ....bla bla..... ,"token_type":"bearer","expires_in":3600}
how can i solve it? it's a dilemma.
Upvotes: 2
Views: 5518
Reputation: 11
If you get return true/false
with this code :
$token = auth()->attempt($credentials);
please type this command in terminal/cmd :
php artisan optimize
Upvotes: 0
Reputation: 1773
Edit config/auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
];
to
return [
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
];
Upvotes: 2
Reputation: 172
Setting up your App's Laravel Authentication has a couple of requirements.
It seems that you chose Sanctum as your Authentication Provider.
EDIT: I seem to have overlooked the fact that the user was using Tymon/JWT-Auth instead of Passport/Sanctum. Please see my split answers below.
According to the sanctum documentation, you need to add the HasApiTokens trait to your User model class (Your user model is missing this Trait). This trait enables your model to be able to issue Access Tokens (via the createToken() method)
(For Tymon/JWT-Auth, you'll need to follow their documentation on the User Model requirements.)
Furthermore, in your app/Http/Controllers/AuthController.php,
your login method uses the auth()->attempt() function to authenticate the user's credentials against those stored in the database. This particular function will ALWAYS return a true/false, so it doesn't help to store the result in the $token variable, like you do below:
if (!$token = auth()->attempt($credentials)) {
What you need to do instead is:
FOR PASSPORT/SANCTUM:
Check whether attempt() succeeded (I.e, wrap it in an if/else block) and then issue the access token yourself:
if (auth()->attempt($credentials)) {
$user = Auth::user();
$token = $user->createToken('Your Token Name');
return $this->respondWithToken($token);
} else {
return response()->json(['error' => 'Unauthorized'], 401);
}
FOR TYMON/JWT-AUTH:
Tymon/Jwt-Auth changes how the attempt() function works by either returning the token on success, or false on failure, thus, change your login() code to:
$token = auth()->attempt($credentials);
if($token === false){
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
NOTE TO OTHER USERS: I've used sksmsWKd's custom respondWithToken wrapper function to return the token, but obviously you can return just the $token itself however you want. I.e:
return response()->json(['token' => $token]);
As for your config/auth.php configuration, it's pretty standard to set the default guard to 'web'. The web guard uses cookies to lug the access token around during app navigation.
I hope the above helps you.
Regards,
Fritz.
Upvotes: 1