mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-05-04 06:02:49 +03:00
feat: implement user registration for postgres via sqlx
This commit is contained in:
parent
8dde022851
commit
26a0935e5f
3 changed files with 36 additions and 31 deletions
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue