Skip to main content

generate_access_token

Function generate_access_token 

Source
pub fn generate_access_token(
    user_id: &str,
    username: &str,
) -> Result<String, Box<dyn Error>>
Expand description

Generates a new access token for a user.

Creates a JWT access token with the user’s ID and username. The token is signed using the configured JWT_SECRET and expires after JWT_ACCESS_MINUTES minutes. Access tokens are short-lived and used for API request authentication.

§Arguments

  • user_id - The unique identifier of the user.
  • username - The username of the user.

§Returns

  • Ok(String) - The encoded JWT token string.
  • Err(Box<dyn Error>) - If token encoding fails or config is not initialized.

§Errors

Returns an error if:

  • JWT encoding fails (invalid secret or configuration)
  • The configured JWT secret is missing or invalid
  • Config has not been initialized via Config::load()

§Example

use crate::utility::jwt;

let token = jwt::generate_access_token("user123", "john_doe")
    .expect("Failed to generate token");
println!("Access token: {}", token);