use actix_jwt_auth_middleware::use_jwt::UseJWTOnApp; use actix_jwt_auth_middleware::{AuthResult, Authority, FromRequest, TokenSigner}; use actix_web::{get, web, App, HttpResponse, HttpServer, Responder}; use ed25519_compact::KeyPair; use jwt_compact::alg::Ed25519; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone, FromRequest)] struct User { id: u32, } #[actix_web::main] async fn main() -> Result<(), Box> { let KeyPair { pk: public_key, sk: secret_key, } = KeyPair::generate(); HttpServer::new(move || { let authority = Authority::::new() .refresh_authorizer(|| async move { Ok(()) }) .token_signer(Some( TokenSigner::new() .signing_key(secret_key.clone()) .algorithm(Ed25519) .build() .expect(""), )) .verifying_key(public_key) .build() .expect(""); App::new() .service(login) .use_jwt(authority, web::scope("").service(hello)) }) .bind(("127.0.0.1", 8080))? .run() .await?; Ok(()) } #[get("/login")] async fn login(cookie_signer: web::Data>) -> AuthResult { let user = User { id: 1 }; Ok(HttpResponse::Ok() .cookie(cookie_signer.create_access_cookie(&user)?) .cookie(cookie_signer.create_refresh_cookie(&user)?) .body("You are now logged in")) } /* Your Claim type can be extracted from within the wrapped services. Note: your Claim type has to implement the FromRequest trait or you have to annotated with the FromRequest derive macro provided in this crate. */ #[get("/hello")] async fn hello(user: User) -> impl Responder { format!("Hello there, i see your user id is {}.", user.id) }