1. Go to this page and download the library: Download zotenme/hyperf-jwt-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/ */
zotenme / hyperf-jwt-auth example snippets
use Zotenme\JwtAuth\Contract\JwtManagerInterface;
class AuthController
{
public function __construct(
private JwtManagerInterface $jwtManager
) {}
public function login(LoginRequest $request): JsonResponse
{
$userId = $this->validateCredentials($request);
$tokenPair = $this->jwtManager->generateTokenPair(
subjectId: $userId,
payload: ['role' => 'user', 'permissions' => ['read', 'write']]
);
return new JsonResponse([
'access_token' => $tokenPair->accessToken,
'refresh_token' => $tokenPair->refreshToken,
'expires_in' => $tokenPair->accessExpiresIn,
]);
}
public function refresh(RefreshRequest $request): JsonResponse
{
$refreshToken = $request->input('refresh_token');
$tokenPair = $this->jwtManager->refreshAccessToken($refreshToken);
return new JsonResponse([
'access_token' => $tokenPair->accessToken,
'refresh_token' => $tokenPair->refreshToken,
'expires_in' => $tokenPair->accessExpiresIn,
]);
}
}