email optional

This commit is contained in:
realaravinth 2021-04-12 11:14:21 +05:30
parent 1b0dfd8082
commit d32350e2b6
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
4 changed files with 47 additions and 14 deletions

View file

@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS mcaptcha_users (
name VARCHAR(100) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
email VARCHAR(100) UNIQUE DEFAULT NULL,
secret varchar(50) NOT NULL UNIQUE,
password TEXT NOT NULL,
ID SERIAL PRIMARY KEY NOT NULL

View file

@ -29,7 +29,7 @@ use crate::Data;
pub struct Register {
pub username: String,
pub password: String,
pub email: String,
pub email: Option<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
@ -53,22 +53,44 @@ pub async fn signup(
}
let username = data.creds.username(&payload.username)?;
let hash = data.creds.password(&payload.password)?;
data.creds.email(Some(&payload.email))?;
// let payload = payload.into_inner();
// let email = payload.email.clone();
// if payload.email.is_some() {
// let email = email.clone().unwrap();
// data.creds.email(Some(&email))?;
// }
if let Some(email) = &payload.email {
data.creds.email(Some(&email))?;
}
let mut secret;
loop {
secret = get_random(32);
let res = sqlx::query!(
"INSERT INTO mcaptcha_users
let res;
if let Some(email) = &payload.email {
res = sqlx::query!(
"INSERT INTO mcaptcha_users
(name , password, email, secret) VALUES ($1, $2, $3, $4)",
&username,
&hash,
&payload.email,
&secret,
)
.execute(&data.db)
.await;
&username,
&hash,
&email,
&secret,
)
.execute(&data.db)
.await;
} else {
res = sqlx::query!(
"INSERT INTO mcaptcha_users
(name , password, secret) VALUES ($1, $2, $3)",
&username,
&hash,
&secret,
)
.execute(&data.db)
.await;
}
if res.is_ok() {
break;
} else {

View file

@ -39,6 +39,17 @@ async fn auth_works() {
delete_user(NAME, &data).await;
// 1. Register with email == None
let msg = Register {
username: NAME.into(),
password: PASSWORD.into(),
email: None,
};
let resp = test::call_service(&mut app, post_request!(&msg, SIGNUP).to_request()).await;
assert_eq!(resp.status(), StatusCode::OK);
// delete user
delete_user(NAME, &data).await;
// 1. Register and signin
let (_, _, signin_resp) = register_and_signin(NAME, EMAIL, PASSWORD).await;
let cookies = get_cookie!(signin_resp);
@ -69,7 +80,7 @@ async fn auth_works() {
let msg = Register {
username: NAME.into(),
password: PASSWORD.into(),
email: EMAIL.into(),
email: Some(EMAIL.into()),
};
bad_post_req_test(
NAME,

View file

@ -76,7 +76,7 @@ pub async fn register<'a>(name: &'a str, email: &str, password: &str) {
let msg = Register {
username: name.into(),
password: password.into(),
email: email.into(),
email: Some(email.into()),
};
let resp =
test::call_service(&mut app, post_request!(&msg, "/api/v1/signup").to_request()).await;