sakhnovkrg / yii2-jwt-auth
Yii2 JWT Auth Module
Installs: 10
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Type:yii2-extension
Requires
- php: ^8.0
- firebase/php-jwt: ^6.10
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-11-09 21:57:49 UTC
README
An easy to use and fully customizable JWT authentication module for your Yii2 application.
Usage
Minimal example with Yii2 Basic Application
- Install extension
composer require --prefer-dist sakhnovkrg/yii2-jwt-auth "@dev"
- Run migrations
php yii migrate
- Add trait to your user model
<?php namespace app\models; class User extends \yii\base\BaseObject implements \yii\web\IdentityInterface { use \sakhnovkrg\yii2\jwt\traits\JWTAuthTrait; // ... }
- Enable pretty urls
'components' => [ 'urlManager' => [ 'enablePrettyUrl' => true, 'showScriptName' => false, 'rules' => [ ], ], // ... ]
Done ¯\(ツ)/¯
To protect your controllers you can use behaviour
public function behaviors() { return [ [ 'class' => \sakhnovkrg\yii2\jwt\filters\JWTAuthenticator::class, 'except' => ['safeAction'] ] ]; }
Endpoints
Method: POST
URL: /auth/login
Body: {
"login": "demo",
"password": "demo"
}
Result: Access token and refresh token in httponly cookie
Method: GET
URL: /@me
Header: Authorization: Bearer %Access token%
Result: Authentificated user info
Method: POST
URL: /auth/refresh
Cookie: Refresh token
Result: New access and refresh tokens
Method: POST
URL: /auth/logout
Header: Authorization: Bearer %Access token%
Result: Remove refresh token cookie
The Postman collection is located in the root of the repository.
Customize
Module settings
'modules' => [ 'jwt-auth' => [ 'class' => \sakhnovkrg\yii2\jwt\JWTModule::class, 'controllerNamespace' => 'sakhnovkrg\yii2\jwt\controllers', 'accessTokenExpirationMinutes' => 5, 'refreshTokenExpirationMinutes' => 24*60, 'jwtSecretKeyEnvVariable' => 'JWT_SECRET', // If the environment variable is not set, the JWT secret key will be automatically generated at the specified path 'jwtSecretKeyFilePathIfNoEnv' => '@runtime/jwt.secret', // Refresh tokens abuse protection 'maxRefreshTokensForUser' => 10 ] ],
You can also override any model, service, or repository using dependency injection.
'bootstrap' => ['log', \app\components\Bootstrap::class],
<?php namespace app\components; use app\models\MyLoginForm; use app\services\MyRefreshTokenService; use sakhnovkrg\yii2\jwt\models\AbstractLoginForm; use yii\base\BootstrapInterface; class Bootstrap implements BootstrapInterface { public function bootstrap($app) { $di = Yii::$container; // By default, the module is configured to work with the user model from the Yii2 Basic Application, so in a real application, you will need to customize the form for your own user model. $di->set(AbstractLoginForm::class, MyLoginForm::class); $di->setSingleton(RefreshTokenService::class, function () use ($di) { $refreshTokenRepository = $di->get(UserRefreshTokenRepository::class); return new MyRefreshTokenService($this, $refreshTokenRepository); }); // etc. } }