@@ -21,9 +21,9 @@ use serde::{Deserialize, Serialize};
2121
2222#[derive(Debug , Serialize , Deserialize , Clone )]
2323pub struct Claims {
24- pub sub : String , // Subject (User ID)
25- pub role : String , // Custom claim: "admin", "user"
26- pub exp : usize , // Expiration time (required for validation)
24+ pub sub : String , // Subject (User ID)
25+ pub role : String , // Custom claim: "admin", "user"
26+ pub exp : usize , // Required for JWT expiration validation
2727}
2828```
2929
@@ -57,20 +57,20 @@ async fn protected_profile(
5757#[rustapi:: post(" /login" )]
5858async fn login (State (state ): State <AppState >) -> Result <Json <String >> {
5959 // In a real app, validate credentials first!
60-
61- // Calculate expiration (1 hour from now)
62- let exp = SystemTime :: now ()
60+ use std :: time :: { SystemTime , UNIX_EPOCH };
61+
62+ let expiration = SystemTime :: now ()
6363 . duration_since (UNIX_EPOCH )
6464 . unwrap ()
65- . as_secs () as usize + 3600 ;
66-
65+ . as_secs () + 3600 ; // Token expires in 1 hour (3600 seconds)
66+
6767 let claims = Claims {
6868 sub : " user_123" . to_owned (),
6969 role : " admin" . to_owned (),
70- exp ,
70+ exp : expiration as usize ,
7171 };
7272
73- // Create the token using the secret from our shared state
73+ // We use the secret from our shared state
7474 let token = create_token (& claims , & state . secret)? ;
7575
7676 Ok (Json (token ))
@@ -92,7 +92,7 @@ async fn main() -> Result<()> {
9292 };
9393
9494 // Configure JWT validation with the same secret
95- let jwt_layer = JwtLayer :: new (secret );
95+ let jwt_layer = JwtLayer :: < Claims > :: new (secret );
9696
9797 RustApi :: auto ()
9898 . state (state ) // Register the shared state
0 commit comments