14

I have a laravel 5 backend that sends an jwt-token as a json response on login with jwt-auth.

Now I would like to add the user role to the jwt token that laravel sends, I tried the following way:

This is my current controller

<?php 
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use Illuminate\Database\Eloquent\Model;
use App\User;

class AuthenticateController extends Controller
{
    public function authenticate(Request $request)
    {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');
        $user = User::where('email', '=', $credentials['email'])->first();
        $customClaims = ['role' => $user->role];

        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials, $customClaims)) {
                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);
        }
        // all good so return the token
        return response()->json(compact('token'));
    }
}
?>

Is there a cleaner way to do this ?

2
  • JWT for php is bad documented. To make a simple login/logout system is painful! See my questions about it. Commented Apr 16, 2015 at 2:07
  • you can create a token from anything you like. it doesn't have to be a user or a set of permissions. github.com/tymondesigns/jwt-auth/wiki/… So i think it's better to set this login on your model or repository and call it ? Commented Sep 4, 2015 at 5:59

4 Answers 4

3

You are currently querying for the user twice, once using the email for the purpose of getting the role and the second within the jwt::attempt() method.I would suggest reducing the queries to just one but doing the authentication {Auth::attempt($credientials)} and then passing the retrieved user into JWT::fromUser() method, along with the custom claim. so

JWT::fromUser($user,['role' => $user->role])
Sign up to request clarification or add additional context in comments.

1 Comment

This link auth.readthedocs.io/en/develop/quick-start solved my problem.
1

It's strange, specifying some $customClaims as second parameter for the attempt() method actually works for me.

However, have you tried to use the JWTFactory Facade? It lets you create every kind of token you want.

You can get more info here: JWT-Auth Creating Tokens Page.

As the installation guide suggests, don't forget to add it as a Facade while installing JWT-Auth!

Hope it helps!

Comments

0

It seems that there isn't a cleaner way in the current stable version of JWTAuth[0.5.9].

Take a look on the following issues #89 #108

As the author pointed out in his comments there will be improvements in his next release covering this issue. You can take a look on his develop branch.

Comments

0

JWT v1.0's documentation causes confusion.
Add this method to your user model, so you can add custom claims when you authenticate users:

public function getJWTCustomClaims()
{

        return = [
            'type' => 'driver',
            'id' => $this->driver->id
        ];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.