Adding API
This commit is contained in:
28
api/components/JwtAuth.php
Normal file
28
api/components/JwtAuth.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace api\components;
|
||||
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
use Yii;
|
||||
use yii\filters\auth\AuthMethod;
|
||||
use yii\web\UnauthorizedHttpException;
|
||||
|
||||
class JwtAuth extends AuthMethod
|
||||
{
|
||||
public function authenticate($user, $request, $response)
|
||||
{
|
||||
$authHeader = $request->getHeaders()->get('Authorization');
|
||||
if ($authHeader && preg_match('/^Bearer\s+(.*?)$/', $authHeader, $matches)) {
|
||||
$jwt = $matches[1];
|
||||
try {
|
||||
$decoded = JWT::decode($jwt, new Key(Yii::$app->params['jwtSecret'], 'HS256'));
|
||||
return $user->loginByAccessToken($decoded->sub);
|
||||
} catch (\Exception $e) {
|
||||
throw new UnauthorizedHttpException('Invalid token');
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user