Register logic

This commit is contained in:
Adrian Woźniak 2020-04-17 22:12:03 +02:00
parent b157d718c7
commit 27db59aa26
2 changed files with 28 additions and 3 deletions

View File

@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::db::DbExecutor;
use crate::errors::ServiceErrors;
use crate::models::{IssueAssignee, User};
use crate::models::{IssueAssignee, User, UserForm};
#[derive(Serialize, Deserialize, Debug)]
pub struct FindUser {
@ -142,6 +142,18 @@ impl Handler<Register> for DbExecutor {
return Err(ServiceErrors::RegisterCollision);
}
let form = UserForm {
name: msg.name,
email: msg.email,
avatar_url: None,
project_id: None,
};
match diesel::insert_into(users).values(form).execute(conn) {
Ok(_) => (),
_ => return Err(ServiceErrors::RegisterCollision),
};
Ok(())
}
}

View File

@ -6,6 +6,7 @@ use jirs_data::WsMsg;
use crate::db::users::{LoadProjectUsers, Register};
use crate::db::DbExecutor;
use crate::mail::MailExecutor;
use crate::ws::auth::authenticate;
use crate::ws::{current_user, WsResult};
pub async fn load_project_users(
@ -24,14 +25,26 @@ pub async fn load_project_users(
pub async fn register(
db: &Data<Addr<DbExecutor>>,
_mail: &Data<Addr<MailExecutor>>,
mail: &Data<Addr<MailExecutor>>,
name: String,
email: String,
) -> WsResult {
let msg = match db.send(Register { name, email }).await {
let msg = match db
.send(Register {
name: name.clone(),
email: email.clone(),
})
.await
{
Ok(Ok(_)) => Some(WsMsg::SignUpSuccess),
Ok(Err(_)) => Some(WsMsg::SignUpPairTaken),
_ => None,
};
match authenticate(db, mail, name, email).await {
Ok(_) => (),
Err(e) => return Ok(Some(e)),
};
Ok(msg)
}