Update token in actix-web 4.3.00

How do I update my token when it expires and pass it to the global reference?

pub async fn kc_init_context() -> InfraResult<Arc<KcContext>> {
    let kc_context = KcContext::new().await?; // connect an create token 

    let utc_now = Utc::now().timestamp();

    if kc_context.claims.exp < utc_now {
        println!("{:?}", "Expired token".to_string());
        /// How create a new token and update global reference ?
        /// kc_context = KcContext::new().await?;
    } 

   // println!("{:?}", kc_context.claims);

    Ok(Arc::new(kc_context))
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    dotenv().ok();

    let kc_context = kc_init_context().await?;

    HttpServer::new(move || {
        App::new()
        
            //.wrap_fn(|req, srv| counter_middleware(req, srv))
            .app_data(data.clone())
            .app_data(web::Data::new(graphql_scheme(kc_context.clone()).clone()))
            .configure(graphql_routes)
            .route("/ping", web::get().to(ping))
    })
    .bind(("127.0.0.1", 4042))?
    .run()
    .await
}

maybe using RwLock? I'm not sure how to do that

You should create a struct that is shared across your server, and store your token in such struct.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.