64 lines
2.2 KiB
Rust
64 lines
2.2 KiB
Rust
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()),
|
|
}),
|
|
}
|
|
}
|