Documentation
¶
Overview ¶
Package auth provides functions for generating and verifying JWT tokens for user authentication.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateJWT ¶
GenerateJWT generates a JSON Web Token (JWT) for a user after login.
The token is signed using the provided jwtKey and includes the username in the claims. The token expires 24 hours after it is issued.
Parameters:
- jwtKey: A byte slice containing the key to sign the JWT.
- username: The username to include in the JWT claims.
Returns:
- A string representing the signed JWT.
- An error if there is an issue generating the token.
Types ¶
type Claims ¶
type Claims struct {
Username string `json:"username"`
Role string `json:"role"`
jwt.RegisteredClaims
}
Claims represents the JWT claims, which includes the username and registered claims.
func VerifyJWT ¶
VerifyJWT verifies a JSON Web Token (JWT) and returns the claims if the token is valid.
The token is parsed and validated using the provided jwtKey. If the token is valid and the signature is correct, the claims are returned. If the token is invalid or the signature does not match, an error is returned.
Parameters:
- jwtKey: A byte slice containing the key to verify the JWT signature.
- tokenString: The JWT string to be verified.
Returns:
- A pointer to the Claims struct if the token is valid.
- An error if the token is invalid or the signature does not match.