jwt-auth icon indicating copy to clipboard operation
jwt-auth copied to clipboard

return true instead of JWT token

Open Kistlak opened this issue 6 years ago • 7 comments

I successfully created a JWT token in one of the applications in cloud hosting using Tymon/jwt-auth. But, another app that has the same project that is in the same cloud hosting returns true for JWT instead of a token. How can I Fix this ??

Here is my config/app.php.

guard.

'admin' => [
'driver' => 'jwt',
'provider' => 'admins',
],

providers.

'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],

Kistlak avatar Dec 01 '19 08:12 Kistlak

In config/app.php change 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], to 'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ],

AhmadDastageer avatar Dec 03 '19 06:12 AhmadDastageer

I have added a new guard and provider to config/app.php like below.

'admin' => [ 'driver' => 'jwt', 'provider' => 'admins', ],

'admins' => [ 'driver' => 'eloquent', 'model' => App\Admin::class, ],

But, the ting is it works find in one sever and doesn't work in other sever. Why is that ??

Kistlak avatar Dec 03 '19 11:12 Kistlak

auth('api')->attempt($credentials)

Instead of

auth()->attempt($credentials)

you have to set the guard 'admin' in your case in the auth helper in order to get the correct behavior

Bouhnosaure avatar Dec 03 '19 15:12 Bouhnosaure

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.

stale[bot] avatar Dec 25 '20 20:12 stale[bot]

maybe you haven't added the following code in the User model

`<?php

namespace App;

use Tymon\JWTAuth\Contracts\JWTSubject; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject { use Notifiable;

// Rest omitted for brevity

/**
 * Get the identifier that will be stored in the subject claim of the JWT.
 *
 * @return mixed
 */
public function getJWTIdentifier()
{
    return $this->getKey();
}

/**
 * Return a key value array, containing any custom claims to be added to the JWT.
 *
 * @return array
 */
public function getJWTCustomClaims()
{
    return [];
}

}`

MyFRA avatar Mar 29 '21 14:03 MyFRA

In config/app.php change 'defaults' => [ 'guard' => 'web', 'passwords' => 'users', ], to 'defaults' => [ 'guard' => 'api', 'passwords' => 'users', ],

It was really helpful, thank you.

RaihanulHoque avatar Aug 26 '22 04:08 RaihanulHoque

In my case, I wanted to keep the default laravel session authentication for the web app and let the mobile app consume jwt auth from the api. My solution is to change the guard whenever I'm trying to authenticate a user via api:

$token = auth()->guard('api')->attempt($credentials);

if (!$token) {
  return response()->json(['error' => 'Unauthorized'], 401);
}
    
return $token;

OR you can pass the guard as a parameter to auth() like this:

$token = auth('api')->attempt($credentials)

sixtusagbo avatar Oct 05 '23 12:10 sixtusagbo