Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions docs/cookbook/src/recipes/jwt_auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Claims {
pub sub: String, // Subject (User ID)
pub role: String, // Custom claim: "admin", "user"
pub exp: usize, // Expiration time (required for validation)
pub sub: String, // Subject (User ID)
pub role: String, // Custom claim: "admin", "user"
pub exp: usize, // Required for JWT expiration validation
}
```

Expand Down Expand Up @@ -57,20 +57,20 @@ async fn protected_profile(
#[rustapi::post("/login")]
async fn login(State(state): State<AppState>) -> Result<Json<String>> {
// In a real app, validate credentials first!

// Calculate expiration (1 hour from now)
let exp = SystemTime::now()
use std::time::{SystemTime, UNIX_EPOCH};
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import statement is redundant. SystemTime and UNIX_EPOCH are already imported at line 47 at the top of the code block. The inline import should be removed.

Suggested change
use std::time::{SystemTime, UNIX_EPOCH};

Copilot uses AI. Check for mistakes.

let expiration = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as usize + 3600;

.as_secs() + 3600; // Token expires in 1 hour (3600 seconds)
let claims = Claims {
sub: "user_123".to_owned(),
role: "admin".to_owned(),
exp,
exp: expiration as usize,
};

// Create the token using the secret from our shared state
// We use the secret from our shared state
let token = create_token(&claims, &state.secret)?;

Ok(Json(token))
Expand All @@ -92,7 +92,7 @@ async fn main() -> Result<()> {
};

// Configure JWT validation with the same secret
let jwt_layer = JwtLayer::new(secret);
let jwt_layer = JwtLayer::<Claims>::new(secret);

RustApi::auto()
.state(state) // Register the shared state
Expand Down
Loading