1- use super::auth_dto::AuthDto;
2- use axum::Json;
1+ use axum::{
2+ http::StatusCode,
3+ response::{IntoResponse, Response},
4+ Json,
5+ };
36use serde_json::json;
47use std::env;
58use utils::{
69 format_error, format_success, jwt::decode_refresh_token,
710 jwt::encode_access_token, jwt::encode_refresh_token,
811};
912
13+ use super::auth_dto::AuthDto;
14+
1015#[derive(serde::Serialize)]
1116pub struct ErrorResponse {
1217 pub message: String,
@@ -18,33 +23,43 @@ pub struct LoginResponse {
1823 pub refresh_token: String,
1924}
2025
21- pub async fn login(Json(credentials): Json<AuthDto>) -> Json<serde_json::Value> {
26+ pub async fn login(Json(credentials): Json<AuthDto>) -> Response {
2227 let email = env::var("USER_EMAIL").expect("USER_EMAIL must be set");
2328 let password = env::var("USER_PASSWORD").expect("USER_PASSWORD must be set");
2429
2530 if credentials.email != email {
26- return Json(format_error("Email or password is incorrect"));
31+ return (
32+ StatusCode::UNAUTHORIZED,
33+ Json(format_error("Email or password is incorrect")),
34+ )
35+ .into_response();
2736 }
2837
2938 if credentials.password != password {
30- return Json(format_error("Email or password is incorrect"));
39+ return (
40+ StatusCode::UNAUTHORIZED,
41+ Json(format_error("Email or password is incorrect")),
42+ )
43+ .into_response();
3144 }
3245
3346 let access_token = encode_access_token(email.clone()).unwrap();
3447 let refresh_token = encode_refresh_token(email.clone()).unwrap();
3548
36- Json(format_success(
37- json!({
38- "access_token": access_token,
39- "refresh_token": refresh_token,
40- }),
41- None,
42- ))
49+ (
50+ StatusCode::OK,
51+ Json(format_success(
52+ json!({
53+ "access_token": access_token,
54+ "refresh_token": refresh_token,
55+ }),
56+ None,
57+ )),
58+ )
59+ .into_response()
4360}
4461
45- pub async fn refresh_token(
46- Json(payload): Json<serde_json::Value>,
47- ) -> Json<serde_json::Value> {
62+ pub async fn refresh_token(Json(payload): Json<serde_json::Value>) -> Response {
4863 let refresh_token = payload
4964 .get("refresh_token")
5065 .and_then(|token| token.as_str())
@@ -55,13 +70,21 @@ pub async fn refresh_token(
5570 let access_token =
5671 encode_access_token(env::var("USER_EMAIL").unwrap().to_string())
5772 .unwrap();
58- Json(format_success(
59- json!({
60- "access_token": access_token
61- }),
62- None,
63- ))
73+ (
74+ StatusCode::OK,
75+ Json(format_success(
76+ json!({
77+ "access_token": access_token
78+ }),
79+ None,
80+ )),
81+ )
82+ .into_response()
6483 }
65- Err(_) => Json(format_error("Invalid or expired refresh token")),
84+ Err(_) => (
85+ StatusCode::UNAUTHORIZED,
86+ Json(format_error("Invalid or expired refresh token")),
87+ )
88+ .into_response(),
6689 }
6790}
0 commit comments