diff --git a/server/src/api.rs b/server/src/api.rs index 27ddda2..e8bd073 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -13,7 +13,7 @@ use parking_lot::RwLock; use std::sync::Arc; #[derive(Serialize)] -struct Options { +pub struct Message { message: String, } @@ -29,6 +29,10 @@ impl API { } fn json(t: &T) -> Response { + API::json_with_code(t, StatusCode::OK) + } + + fn json_with_code(t: &T, status_code: StatusCode) -> Response { let j = warp::reply::json(t); let mut rs = j.into_response(); let hs = rs.headers_mut(); @@ -41,6 +45,7 @@ impl API { "Access-Control-Allow-Headers", HeaderValue::from_static("*"), ); + *rs.status_mut() = status_code; rs } @@ -48,7 +53,7 @@ impl API { let not_found = warp::any().map(|| warp::reply::with_status("Not found", StatusCode::NOT_FOUND)); let options = warp::any().and(options()).map(|| { - API::json(&Options { + API::json(&Message { message: "ok".to_owned(), }) }); diff --git a/server/src/api/auth.rs b/server/src/api/auth.rs index f52110e..6ba95f7 100644 --- a/server/src/api/auth.rs +++ b/server/src/api/auth.rs @@ -2,13 +2,13 @@ use hastic::services::user_service; use warp::filters::method::post; use warp::http::HeaderValue; -use warp::hyper::Body; +use warp::hyper::{Body, StatusCode}; use warp::{http::Response, Filter}; use warp::{Rejection, Reply}; use serde::Serialize; -use crate::api; +use crate::api::{self, API}; use parking_lot::RwLock; use std::sync::Arc; @@ -26,11 +26,15 @@ pub fn get_route( .and(warp::body::json()) .map(move |user: user_service::User| { let us = user_service.write().login(&user); - match us { - Some(token) => api::API::json(&SigninResp { token }), - None => api::API::json(&SigninResp { - token: "no token".to_string(), - }), + if let Some(token) = us { + return api::API::json(&SigninResp { token }); + } else { + return api::API::json_with_code( + &api::Message { + message: "wrong login or password".to_owned(), + }, + StatusCode::UNAUTHORIZED, + ); } }); }