Skip to content

Commit 56dcb43

Browse files
authored
Merge pull request #81 from Tuntii/copilot/sub-pr-80
docs: fix JWT auth recipe API errors from review feedback
2 parents 61d5b9f + 007dd12 commit 56dcb43

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

docs/cookbook/src/recipes/jwt_auth.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ use serde::{Deserialize, Serialize};
2121

2222
#[derive(Debug, Serialize, Deserialize, Clone)]
2323
pub 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")]
5858
async 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

Comments
 (0)