PHP code example of asinfotrack / yii2-jwt
1. Go to this page and download the library: Download asinfotrack/yii2-jwt library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
asinfotrack / yii2-jwt example snippets
class User extends \yii\db\ActiveRecorde implements \yii\web\IdentityInterface
{
//...
use JwtTokenTrait;
//...
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type=null)
{
/* @var $result \asinfotrack\yii2\jwt\helpers\JwtTokenDecodingResult */
try {
//try decoding the token
$result = $this->decodeJwtToken($token, Yii::$app->params['myJwtTokenSecret'], true, true);
} catch (JwtException $e) {
//check if token is valid but expired
if ($e instanceof \asinfotrack\yii2\jwt\exceptions\JwtExpiredException) {
//delete expired token from db
}
//return null to signal user could not be found
return null;
}
//token was valid so we can extract the id
$modelId = $result->getJti();
//return the user model or null if not found
return static::findOne($modelId);
}
//...
/**
* Create a token for the current user model instance. You might want to persist
* the result in a token table to keep track of the tokens created.
*
* @return string the created token
*/
protected function createTokenForUser()
{
/* @var $request \asinfotrack\yii2\jwt\helpers\JwtTokenIssueRequest */
//optional request if additional data is