feat: indempotency restart for email autoconfirm and non autoconfirm

This commit is contained in:
Fu Zi Xiang 2023-11-07 00:32:28 +08:00
parent c029759573
commit 17044ad4f0
No known key found for this signature in database
2 changed files with 37 additions and 27 deletions

View File

@ -197,7 +197,7 @@ impl OAuthProvider {
pub struct OAuthURL { pub struct OAuthURL {
pub url: String, pub url: String,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)] #[serde(untagged)]
pub enum SignUpResponse { pub enum SignUpResponse {
Authenticated(GotrueTokenResponse), Authenticated(GotrueTokenResponse),

View File

@ -215,7 +215,22 @@ async fn setup_admin_account(
let password = gotrue_setting.admin_password.as_str(); let password = gotrue_setting.admin_password.as_str();
let res_resp = gotrue_client.sign_up(admin_email, password).await; let res_resp = gotrue_client.sign_up(admin_email, password).await;
println!("res_resp: {:?}", &res_resp);
match res_resp { match res_resp {
Err(err) => {
if let app_error::gotrue::GoTrueError::Internal(err) = err {
match (err.code, err.msg.as_str()) {
(400, "User already registered") => {
tracing::info!("Admin user already registered");
Ok(())
},
_ => Err(err.into()),
}
} else {
Err(err.into())
}
},
Ok(resp) => match resp { Ok(resp) => match resp {
gotrue_entity::dto::SignUpResponse::Authenticated(resp) => { gotrue_entity::dto::SignUpResponse::Authenticated(resp) => {
tracing::info!( tracing::info!(
@ -224,7 +239,12 @@ async fn setup_admin_account(
); );
Ok(()) Ok(())
}, },
gotrue_entity::dto::SignUpResponse::NotAuthenticated(user) => { gotrue_entity::dto::SignUpResponse::NotAuthenticated(user) => match user.role.as_str() {
"supabase_admin" => {
tracing::info!("Admin user already created and set role to supabase_admin");
Ok(())
},
_ => {
let user_id = user.id.parse::<uuid::Uuid>().unwrap(); let user_id = user.id.parse::<uuid::Uuid>().unwrap();
let result = sqlx::query( let result = sqlx::query(
r#" r#"
@ -239,21 +259,11 @@ async fn setup_admin_account(
.context("failed to update the admin user")?; .context("failed to update the admin user")?;
assert_eq!(result.rows_affected(), 1); assert_eq!(result.rows_affected(), 1);
tracing::info!("Admin user created and set role to supabase_admin");
Ok(()) Ok(())
}, },
}, },
Err(err) => {
if let app_error::gotrue::GoTrueError::Internal(err) = err {
match (err.code, err.msg.as_str()) {
(400, "User already registered") => {
tracing::info!("Admin user already registered");
Ok(())
},
_ => Err(err.into()),
}
} else {
Err(err.into())
}
}, },
} }
} }