Interface luya\admin\base\JwtIdentityInterface

Extendsyii\base\Configurable
Available since version2.2.0
Source Code https://github.com/luyadev/luya-module-admin/blob/master/src/base/JwtIdentityInterface.php

The interface which integrates JWT authentification against users.

Public Methods

Hide inherited methods

Method Description Defined By
getId() Returns an ID that can uniquely identify a user identity. luya\admin\base\JwtIdentityInterface
loginByJwtToken() Ensure the user login by token. luya\admin\base\JwtIdentityInterface

Method Details

Hide inherited methods

getId() public abstract method

Returns an ID that can uniquely identify a user identity.

public abstract string|integer getId ( )
return string|integer

An ID that uniquely identifies a user identity.

                public function getId();

            
loginByJwtToken() public abstract static method

Ensure the user login by token.

The user id to get the given user is commonly stored as uid claim. Therefore in order to get the user id use getClaim:

public staitc function loginByJwtToken(Plain $token)
{
    // get the user id
    $userId = $token->claims()->get('uid');

    return User::find()->where(['id' => $userId, 'is_active' => true]);
}

Depending on your setup you also might to store the jwt token while authentication. Then you can retrieve the jwt token by calling toString() method.

public staitc function loginByJwtToken(Plain $token)
{
    // get the user id
    $userId = $token->claims()->get('uid');
    // get the jwt token
    $jwtToken = $token->toString();

    return User::findOne(['id' => $userId, 'jwt_access_token' => $jwtToken]);
}

Return false if no user is found or login is incorrect.

See also \luya\admin\base\Discussion regarding storing the jwt token: https://stackoverflow.com/a/42765870/4611030.

public abstract static self|boolean loginByJwtToken ( \Lcobucci\JWT\Token\Plain $token )
$token \Lcobucci\JWT\Token\Plain
return self|boolean

Return the user object which implements JwtIdentityInterface or false if not found and login is invalid.

                public static function loginByJwtToken(Plain $token);