mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 10:05:41 +00:00
feat: implement user registration for postgres via sqlx
This commit is contained in:
@@ -26,8 +26,13 @@ pub fn map_register_err(e: Error) -> DBError {
|
||||
if let Error::Database(err) = e {
|
||||
if err.code() == Some(Cow::from("23505")) {
|
||||
let msg = err.message();
|
||||
if msg.contains("mcaptcha_users_username_key") {
|
||||
unimplemented!();
|
||||
println!("{}", msg);
|
||||
if msg.contains("mcaptcha_users_name_key") {
|
||||
DBError::UsernameTaken
|
||||
} else if msg.contains("mcaptcha_users_email_key") {
|
||||
DBError::EmailTaken
|
||||
} else if msg.contains("mcaptcha_users_secret_key") {
|
||||
DBError::SecretTaken
|
||||
} else {
|
||||
DBError::DBError(Box::new(Error::Database(err)))
|
||||
}
|
||||
|
||||
@@ -100,6 +100,35 @@ impl MCDatabase for Database {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// register a new user
|
||||
async fn register(&self, p: &Register) -> DBResult<()> {
|
||||
let res;
|
||||
if let Some(email) = &p.email {
|
||||
res = sqlx::query!(
|
||||
"insert into mcaptcha_users
|
||||
(name , password, email, secret) values ($1, $2, $3, $4)",
|
||||
&p.username,
|
||||
&p.hash,
|
||||
&email,
|
||||
&p.secret,
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await;
|
||||
} else {
|
||||
res = sqlx::query!(
|
||||
"INSERT INTO mcaptcha_users
|
||||
(name , password, secret) VALUES ($1, $2, $3)",
|
||||
&p.username,
|
||||
&p.hash,
|
||||
&p.secret,
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await;
|
||||
};
|
||||
res.map_err(map_register_err)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn now_unix_time_stamp() -> i64 {
|
||||
|
||||
Reference in New Issue
Block a user