normogen/backend/src/handlers/users.rs
goose 8b2c13501f Phase 2.3: JWT Authentication implementation
- Implemented JWT-based authentication system with access and refresh tokens
- Added password hashing service using PBKDF2
- Created authentication handlers: register, login, refresh, logout
- Added protected routes with JWT middleware
- Created user profile handlers
- Fixed all compilation errors
- Added integration tests for authentication endpoints
- Added reqwest dependency for testing
- Created test script and environment example documentation

All changes:
- backend/src/auth/: Complete auth module (JWT, password, claims)
- backend/src/handlers/: Auth, users, and health handlers
- backend/src/middleware/: JWT authentication middleware
- backend/src/config/: Added AppState with Clone derive
- backend/src/main.rs: Fixed imports and added auth routes
- backend/src/db/mod.rs: Changed error handling to anyhow::Result
- backend/Cargo.toml: Added reqwest for testing
- backend/tests/auth_tests.rs: Integration tests
- thoughts/: Documentation updates (STATUS.md, env.example, test_auth.sh)
2026-02-14 20:03:11 -03:00

42 lines
1.2 KiB
Rust

use axum::{
extract::State,
response::Json,
http::StatusCode,
Extension,
};
use serde_json::{json, Value};
use crate::config::AppState;
use crate::auth::claims::AccessClaims;
use crate::models::user::UserRepository;
pub async fn get_profile(
State(state): State<AppState>,
Extension(claims): Extension<AccessClaims>,
) -> Result<Json<Value>, (StatusCode, Json<Value>)> {
let user_repo = UserRepository::new(state.db.collection("users"));
let user = match user_repo.find_by_user_id(&claims.sub).await {
Ok(Some(user)) => user,
Ok(None) => {
return Err((
StatusCode::NOT_FOUND,
Json(json!({ "error": "User not found" }))
));
}
Err(e) => {
return Err((
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({ "error": format!("Database error: {}", e) }))
));
}
};
Ok(Json(json!({
"user_id": user.user_id,
"email": user.email,
"family_id": user.family_id,
"profile_ids": user.profile_ids,
"token_version": user.token_version,
"created_at": user.created_at,
"updated_at": user.updated_at
})))
}