Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we customise the claims? #2228

Open
kirkbushell opened this issue Aug 22, 2023 · 2 comments
Open

How do we customise the claims? #2228

kirkbushell opened this issue Aug 22, 2023 · 2 comments

Comments

@kirkbushell
Copy link

I have a "multi-tenant" game application, and instead of having to store the accessed account in the URL, I want it to be part of the JWT token payload. So, a user can login to an account, and that account will be stored as part of the payload.

However, I also want them to be able to switch accounts. This would involve another endpoint, and then I issue the user a new token to be used.

On subsequent requests I would check check the game they've switched to and double-check they can access.

But this saves having to swap domains or have a URL prefix (which I don't want, in this context it doesn't make any sense).

@azzumed
Copy link

azzumed commented Nov 14, 2023

use customClaims method

@Ramazanonat
Copy link

use customClaims method

i did that.

Add those on User model
//
public function getJWTIdentifier()
{
return $this->getKey();
}

public function getJWTCustomClaims()
{
    return $this->jwtCustomClaims;
}

public function setJWTCustomClaims(array $claims)
{
    $this->jwtCustomClaims = $claims;
}

//
then u can use setJWTCustomClaims method like that.
//
if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) {
$user = Auth::user();
$user->setJWTCustomClaims(['test' => 'test']);
$token = JWTAuth::fromUser($user);

        return response()->json([
            'success' => true,
            'token' => $token,
            'user' => $user,
        ]);
    }

//

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants