This commit is contained in:
Mashy 2019-07-19 19:19:29 +10:00
parent 7eb34b4350
commit 51d3c76460
2 changed files with 26 additions and 6 deletions

View File

@ -12,11 +12,9 @@ const addState = connect(
} = state;
function submitLogin(name, password) {
postData('/login', { name, password })
.then(res => {
if (!res.ok) return errorToast(res);
res.text()
})
.then(res => {
.then(res => res.json())
.then(data => {
if (!data.success) return errorToast(data.error_message);
ws.connect();
})
.catch(error => errorToast(error));

View File

@ -5,9 +5,11 @@ use iron::headers::{Cookie as CookieHdr, SetCookie};
use iron::prelude::*;
use iron::status;
use iron::typemap::Key;
use iron::mime::Mime;
use iron::{typemap, BeforeMiddleware,AfterMiddleware};
use persistent::Read;
use router::Router;
use serde::{Serialize, Deserialize};
// use warden::{warden};
// use pubsub::{pg_listen};
@ -37,6 +39,23 @@ pub enum MnmlHttpError {
InvalidCode,
}
#[derive(Serialize, Deserialize)]
struct JsonResponse {
response: String,
success: bool,
error_message: String
}
impl JsonResponse {
fn success(response: String) -> Self {
JsonResponse { response: response, success: true, error_message: "".to_string() }
}
fn error(msg: String) -> Self {
JsonResponse { response: "".to_string(), success: false, error_message: msg }
}
}
impl From<MnmlHttpError> for IronError {
fn from(m_err: MnmlHttpError) -> Self {
let (err, res) = match m_err {
@ -48,8 +67,11 @@ impl From<MnmlHttpError> for IronError {
MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest),
MnmlHttpError::InvalidCode => (m_err.compat(), status::Unauthorized),
};
let err_msg = JsonResponse::error("grep".to_string());
let err_out = serde_json::to_string(&err_msg).expect("Failed to encode response");
let content_type = "application/json".parse::<Mime>().expect("Failed to parse content type");
IronError::new(err, res)
IronError::new(err, (content_type, res, err_out))
}
}