-
Notifications
You must be signed in to change notification settings - Fork 3
JWT Documentation
Benyamin Khalife edited this page Jun 29, 2023
·
1 revision
JWT (JSON Web Token) is a compact, URL-safe means of representing claims to be transferred between two parties. It is commonly used for authentication and authorization purposes in web applications.
To generate and verify JWT tokens, you can follow the example below:
use webrium\Jwt;
// Create a new instance of the Jwt class
$jwt = new Jwt('your-secret-key');
// Generate a JWT token
$payload = [
'user_id' => 123,
'username' => 'john.doe@example.com'
];
$token = $jwt->generateToken($payload);
// Verify a JWT token
$decodedPayload = $jwt->verifyToken($token);
if ($decodedPayload) {
// Token is valid
echo "Token is valid!";
print_r($decodedPayload);
} else {
// Token is invalid
echo "Token is invalid!";
}
__construct(string $secretKey, string $algorithm = 'sha3-256')
- Creates a new instance of the Jwt class.
- Parameters:
-
$secretKey
: The secret key used to sign the token. -
$algorithm
(optional): The algorithm used to sign the token (default: sha3-256).
-
generateToken(array $payload): string
- Generates a JWT token.
- Parameters:
-
$payload
: The payload to include in the token.
-
- Returns: The generated JWT token as a string.
verifyToken(string $jwt): mixed
- Verifies a JWT token.
- Parameters:
-
$jwt
: The JWT token to verify.
-
- Returns:
- If the token is valid, returns the decoded payload as an array.
- If the token is invalid, returns
false
.
- Make sure to keep your secret key secure and do not share it.
- Always validate and sanitize user inputs before generating or verifying tokens.
- Keep your JWT library up-to-date with the latest security patches.
That's it! You can now start using the JWT library in your PHP projects. For more information and advanced usage, refer to the official JWT documentation.