1. Go to this page and download the library: Download hnraytek/flame-auth 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/ */
use FlameModule\Attribute\NeedAuth;
use FlameModule\Auth\Attribute\NoAuth;
class UserController
{
#[NoAuth]
public function login()
{
// 无需鉴权的登录接口,strategy 为 block 生效
}
#[NeedAuth]
public function profile()
{
// 需要鉴权的用户信息接口,strategy 为 allow 才生效
}
}
use FlameModule\Auth\Attribute\NoAuth;use FlameModule\Auth\Controller\AuthApiController;
class ApiController extends AuthApiController
{
protected string $tokenType = 'user'; // (必填)token 类型,支持多种类型(如 `user`、`admin`、`member` 等)
#[NoAuth]
public function login()
{
// 验证用户...
$this->setUserId($userId); // 设置用户ID并生成 token
$this->success([
'token' => $this->getToken(),
'refresh_token' => $this->getRefreshToken(),
]);
}
public function userInfo()
{
$userId = $this->getUserId(); // 获取当前登录用户ID
// ...
}
public function logout()
{
$this->clearToken(); // 清除 token
$this->success(['message' => 'Logout success']);
}
}
use FlameModule\Auth\Exception\TokenExpirationException;
if ($e instanceof TokenExpirationException) {
return json([
'code' => 401,
'message' => 'Token has expired or is invalid',
], 401);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.