Skip to main content

verify_token

Function verify_token 

Source
pub fn verify_token(token: &str) -> Result<Claims, Box<dyn Error + Send + Sync>>
Expand description

Verifies and decodes a JWT token.

Validates the token signature against the configured JWT_SECRET and checks expiration. Returns the decoded claims if the token is valid, allowing the caller to access the user ID and other claim data without re-parsing.

§Arguments

  • token - The encoded JWT token string to verify.

§Returns

  • Ok(Claims) - The decoded claims if the token is valid.
  • Err(Box<dyn Error>) - If token verification fails.

§Errors

Returns an error if:

  • The token is malformed or cannot be decoded
  • The signature does not match the configured secret
  • The token has expired
  • Required claims are missing
  • Config has not been initialized via Config::load()

§Example

use crate::utility::jwt;

let token = "eyJ0eXAiOiJKV1QiLCJhbGc..."; // Valid JWT
match jwt::verify_token(token) {
    Ok(claims) => println!("Authenticated user: {}", claims.sub),
    Err(_) => println!("Invalid or expired token"),
}