more refactor on backend
This commit is contained in:
1 parent
941afa2485
commit
7f7936d37f
20 files changed
+1603
-1527
No files matched your search
@@ -0,0 +1,63 @@
|
||||
use crate::model;
|
||||
use crate::state;
|
||||
use crate::utils;
|
||||
use axum::extract::{Json, State};
|
||||
use sqlx::Row;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub async fn validate_token_handler(
|
||||
State(state): State<Arc<state::AppState>>,
|
||||
Json(payload): Json<model::ValidateTokenRequest>,
|
||||
) -> Json<model::ValidateTokenResponse> {
|
||||
match utils::verify_token(&state.db_pool, &payload.token).await {
|
||||
Ok(Some(username)) => {
|
||||
// Get user details
|
||||
let user_result = sqlx::query("SELECT phone, avatar_url FROM users WHERE username = ?")
|
||||
.bind(&username)
|
||||
.fetch_optional(&state.db_pool)
|
||||
.await;
|
||||
|
||||
match user_result {
|
||||
Ok(Some(row)) => {
|
||||
let phone: String = row.get("phone");
|
||||
let avatar_url: Option<String> = row.get("avatar_url");
|
||||
Json(model::ValidateTokenResponse {
|
||||
success: true,
|
||||
username: Some(username),
|
||||
phone: Some(phone),
|
||||
avatar_url,
|
||||
message: None,
|
||||
})
|
||||
}
|
||||
Ok(None) => Json(model::ValidateTokenResponse {
|
||||
success: false,
|
||||
username: None,
|
||||
phone: None,
|
||||
avatar_url: None,
|
||||
message: Some("User not found".to_string()),
|
||||
}),
|
||||
Err(_) => Json(model::ValidateTokenResponse {
|
||||
success: false,
|
||||
username: None,
|
||||
phone: None,
|
||||
avatar_url: None,
|
||||
message: Some("Database error".to_string()),
|
||||
}),
|
||||
}
|
||||
}
|
||||
Ok(None) => Json(model::ValidateTokenResponse {
|
||||
success: false,
|
||||
username: None,
|
||||
phone: None,
|
||||
avatar_url: None,
|
||||
message: Some("Invalid or expired token".to_string()),
|
||||
}),
|
||||
Err(_) => Json(model::ValidateTokenResponse {
|
||||
success: false,
|
||||
username: None,
|
||||
phone: None,
|
||||
avatar_url: None,
|
||||
message: Some("Token verification error".to_string()),
|
||||
}),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user