matheen ulla
matheen ulla

Reputation: 568

how to create a token in laravel tymons/jwt-auth

I want to create a token encoded with user role. I have tried with seeing the documentation, But I am not getting a token. what I have tried.

I am using laravel 5.8 and package version "tymon/jwt-auth": "^1.0.0-rc.2"

Thank you

AuthController

 public function login()
    {
        $credentials = request(['email', 'password']);

        if (! $token = auth()->guard('api')->attempt($credentials)) {
            return response()->json(['errors' => 'In-valid username and Password'], 401);
        }

        $customClaims =[
           'role' => auth('api')->user()->getRoleNames()
        ];
        $payload = JWTFactory::make($customClaims); 
        $token = JWTAuth::encode($payload);

        return $this->respondWithToken($token);
    }

  protected function respondWithToken($token)
    {
        return response()->json([  
            'success' => true, 
            'access_token' => $token,
            'token_type' => 'bearer',
        ]);
    }  

Upvotes: 0

Views: 3934

Answers (3)

VIKAS KATARIYA
VIKAS KATARIYA

Reputation: 6005

Try This

use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthenticateController extends Controller
{
    public function login(Request $request)
    {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');

        try {
            // attempt to verify the credentials and create a token for the user
            if (!auth()->guard('api')->claims(['role' => 'bar'])->attempt($credentials)) 
                {
                 return response()->json(['errors' => 'In-valid username and Password'], 401);
                }

             $token = auth('api')->claims(['role' => auth('api')->user()->getRoleNames()])->attempt($credentials);

             return $this->respondWithToken($token);
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
             return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
         return response()->json(compact('token'));
    }
}

Upvotes: 1

albus_severus
albus_severus

Reputation: 3712

Your User MOdel should like this

class User extends Authenticatable implements JWTSubject
{
    use Notifiable, HasRoles;

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }
    public function getJWTCustomClaims()
    {
        return [];
    }


}



public function login()
    {
        $credentials = request(['email', 'password']);

        if (!$token = JWTAuth::attempt($credentials)) {
            return response()->json(['errors' => 'In-valid username and Password'], 401);
        }

        $customClaims =[
           'role' => auth('api')->user()->getRoleNames()
        ];
        $payload = JWTFactory::make($customClaims); 
        $token = JWTAuth::encode($payload);

        return $this->respondWithToken($token);
    }

Upvotes: 1

Adlan Arif Zakaria
Adlan Arif Zakaria

Reputation: 1745

Based on the documentation, you might need to do attempt() twice, like this:

public function login()
{
    $credentials = request(['email', 'password']);

    if (!auth()->guard('api')->claims(['role' => 'bar'])->attempt($credentials)) {
        return response()->json(['errors' => 'In-valid username and Password'], 401);
    }

    $token = auth('api')->claims(['role' => auth('api')->user()->getRoleNames()])->attempt($credentials);

    return $this->respondWithToken($token);
}

Upvotes: 2

Related Questions