Skip to main content

generate_refresh_token

Function generate_refresh_token 

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

Generates a new refresh token for a user.

Creates a JWT refresh token with the user’s ID and username. The token is signed using the configured JWT_SECRET and expires after JWT_REFRESH_DAYS days. Refresh tokens are long-lived and used to obtain new access tokens without re-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_refresh_token("user123", "john_doe")
    .expect("Failed to generate token");
println!("Refresh token: {}", token);