register account proper err handling

This commit is contained in:
Mashy 2019-07-20 01:04:02 +10:00
parent b289ed92de
commit c44cd44933
3 changed files with 32 additions and 11 deletions

View File

@ -23,11 +23,10 @@ const addState = connect(
function submitRegister(name, password, code) {
postData('/register', { name, password, code })
.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);
console.log(data.response);
ws.connect();
})
.catch(error => errorToast(error));

View File

@ -8,6 +8,7 @@ use serde_cbor::{from_slice};
use postgres::transaction::Transaction;
use net::MnmlHttpError;
use names::{name as generate_name};
use construct::{Construct, construct_recover, construct_spawn};
use instance::{Instance, instance_delete};
@ -212,17 +213,17 @@ pub fn set_subscribed(tx: &mut Transaction, id: Uuid, subscribed: bool) -> Resul
Ok(name)
}
pub fn create(name: &String, password: &String, code: &String, tx: &mut Transaction) -> Result<String, Error> {
pub fn create(name: &String, password: &String, code: &String, tx: &mut Transaction) -> Result<String, MnmlHttpError> {
if password.len() < PASSWORD_MIN_LEN {
return Err(err_msg("password must be at least 12 characters"));
return Err(MnmlHttpError::PasswordUnacceptable);
}
if code.to_lowercase() != "grep842" {
return Err(err_msg("https://discord.gg/YJJgurM"));
return Err(MnmlHttpError::InvalidCode);
}
if name.len() == 0 {
return Err(err_msg("account name not supplied"));
return Err(MnmlHttpError::AccountNameNotProvided);
}
let id = Uuid::new_v4();
@ -246,7 +247,7 @@ pub fn create(name: &String, password: &String, code: &String, tx: &mut Transact
match result.iter().next() {
Some(row) => row,
None => return Err(err_msg("account not created")),
None => return Err(MnmlHttpError::DbError),
};
// 3 constructs for a team and 1 to swap

View File

@ -32,6 +32,8 @@ pub enum MnmlHttpError {
#[fail(display="bad request")]
BadRequest,
#[fail(display="account name taken or invalid")]
AccountNameNotProvided,
#[fail(display="account name not provided")]
AccountNameTaken,
#[fail(display="password unacceptable. must be > 11 characters")]
PasswordUnacceptable,
@ -39,6 +41,24 @@ pub enum MnmlHttpError {
InvalidCode,
}
impl From<bcrypt::BcryptError> for MnmlHttpError {
fn from(_err: bcrypt::BcryptError) -> Self {
MnmlHttpError::ServerError
}
}
impl From<postgres::Error> for MnmlHttpError {
fn from(_err: postgres::Error) -> Self {
MnmlHttpError::DbError
}
}
impl From<failure::Error> for MnmlHttpError {
fn from(_err: failure::Error) -> Self {
MnmlHttpError::ServerError
}
}
#[derive(Serialize, Deserialize)]
struct JsonResponse {
response: Option<String>,
@ -73,6 +93,7 @@ impl From<MnmlHttpError> for IronError {
MnmlHttpError::DbError => (m_err.compat(), status::InternalServerError),
MnmlHttpError::Unauthorized => (m_err.compat(), status::Unauthorized),
MnmlHttpError::BadRequest => (m_err.compat(), status::BadRequest),
MnmlHttpError::AccountNameNotProvided => (m_err.compat(), status::BadRequest),
MnmlHttpError::AccountNameTaken => (m_err.compat(), status::BadRequest),
MnmlHttpError::PasswordUnacceptable => (m_err.compat(), status::BadRequest),
MnmlHttpError::InvalidCode => (m_err.compat(), status::Unauthorized),
@ -169,7 +190,7 @@ fn register(req: &mut Request) -> IronResult<Response> {
},
Err(e) => {
warn!("{:?}", e);
Err(IronError::from(MnmlHttpError::BadRequest))
Err(IronError::from(e))
}
}
}