mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 18:15:39 +00:00
set email
This commit is contained in:
@@ -205,6 +205,47 @@ pub async fn update_user_secret(
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Email {
|
||||
pub email: String,
|
||||
}
|
||||
|
||||
#[post("/api/v1/account/email/")]
|
||||
pub async fn set_email(
|
||||
id: Identity,
|
||||
|
||||
payload: web::Json<Email>,
|
||||
|
||||
data: web::Data<Data>,
|
||||
) -> ServiceResult<impl Responder> {
|
||||
is_authenticated(&id)?;
|
||||
|
||||
let username = id.identity().unwrap();
|
||||
|
||||
data.creds.email(Some(&payload.email))?;
|
||||
|
||||
let res = sqlx::query!(
|
||||
"UPDATE mcaptcha_users set email = $1
|
||||
WHERE name = $2",
|
||||
&payload.email,
|
||||
&username,
|
||||
)
|
||||
.execute(&data.db)
|
||||
.await;
|
||||
if !res.is_ok() {
|
||||
if let Err(sqlx::Error::Database(err)) = res {
|
||||
if err.code() == Some(Cow::from("23505"))
|
||||
&& err.message().contains("mcaptcha_users_email_key")
|
||||
{
|
||||
Err(ServiceError::EmailTaken)?
|
||||
} else {
|
||||
Err(sqlx::Error::Database(err))?
|
||||
}
|
||||
};
|
||||
}
|
||||
Ok(HttpResponse::Ok())
|
||||
}
|
||||
|
||||
#[post("/api/v1/signout")]
|
||||
pub async fn signout(id: Identity) -> impl Responder {
|
||||
if let Some(_) = id.identity() {
|
||||
|
||||
@@ -36,6 +36,7 @@ pub fn services(cfg: &mut ServiceConfig) {
|
||||
cfg.service(auth::email_exists);
|
||||
cfg.service(auth::get_secret);
|
||||
cfg.service(auth::update_user_secret);
|
||||
cfg.service(auth::set_email);
|
||||
|
||||
// mcaptcha
|
||||
cfg.service(mcaptcha::mcaptcha::add_mcaptcha);
|
||||
|
||||
@@ -134,10 +134,12 @@ async fn auth_works() {
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn del_userworks() {
|
||||
async fn email_udpate_and_del_userworks() {
|
||||
const NAME: &str = "testuser2";
|
||||
const PASSWORD: &str = "longpassword2";
|
||||
const EMAIL: &str = "testuser1@a.com2";
|
||||
const DEL_URL: &str = "/api/v1/account/delete";
|
||||
const EMAIL_UPDATE: &str = "/api/v1/account/email/";
|
||||
|
||||
{
|
||||
let data = Data::new().await;
|
||||
@@ -148,13 +150,26 @@ async fn del_userworks() {
|
||||
let cookies = get_cookie!(signin_resp);
|
||||
let mut app = get_app!(data).await;
|
||||
|
||||
let email_payload = Email {
|
||||
email: EMAIL.into(),
|
||||
};
|
||||
let email_update_resp = test::call_service(
|
||||
&mut app,
|
||||
post_request!(&email_payload, EMAIL_UPDATE)
|
||||
.cookie(cookies.clone())
|
||||
.to_request(),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(email_update_resp.status(), StatusCode::OK);
|
||||
|
||||
let payload = Password {
|
||||
password: creds.password,
|
||||
};
|
||||
|
||||
let delete_user_resp = test::call_service(
|
||||
&mut app,
|
||||
post_request!(&payload, "/api/v1/account/delete")
|
||||
post_request!(&payload, DEL_URL)
|
||||
.cookie(cookies)
|
||||
.to_request(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user