PHP code example of zotenme / hyperf-jwt-auth

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,
        ]);
    }
}



return [
    'algorithm' => 'HS256',
    'keys' => [
        'secret_key' => env('JWT_SECRET', 'your-secret-key-change-this'),
    ],
    'access_token' => ['ttl' => 900],  // 15 minutes
    'refresh_token' => ['ttl' => 604800], // 7 days
    'blacklist' => ['enabled' => true],
    'sso_mode' => false,
];
bash
composer rf.php vendor:publish zotenme/hyperf-jwt-auth