fix: restartable server when autoconfirm is true

This commit is contained in:
Fu Zi Xiang 2023-11-06 13:50:38 +08:00
parent 41e01b91fc
commit b785b1a482
No known key found for this signature in database
GPG Key ID: 7AE0884D237CEE16
4 changed files with 52 additions and 25 deletions

View File

@ -0,0 +1,14 @@
{
"db_name": "PostgreSQL",
"query": "\n UPDATE auth.users\n SET role = 'supabase_admin', email_confirmed_at = NOW()\n WHERE id = $1\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": []
},
"hash": "141454ccce32ab6abd9fba21292d6290b7d73425f82bcf63ad4a0bd6959047d5"
}

View File

@ -6,7 +6,9 @@
GOTRUE_JWT_SECRET=hello456
# user sign up will automatically be confirmed if this is set to true
GOTRUE_MAILER_AUTOCONFIRM=false
# if you have OAuth2 set up or smtp configured, you can set this to false
# to enforce email confirmation or OAuth2 login instead
GOTRUE_MAILER_AUTOCONFIRM=true
# if you enable mail confirmation, you need to set the SMTP configuration below
GOTRUE_SMTP_HOST=smtp.gmail.com

View File

@ -36,11 +36,10 @@ cp dev.env .env
# This is the secret key for authentication, please change this and keep the key safe
GOTRUE_JWT_SECRET=hello456
# This determine if the user will be user automatically be confirmed when they sign up
# If this is enabled, it requires a clicking a confirmation link in the email which user
# use for sign up.
# Pre-requisite if you enable: you need to have your SMTP Service set up,
# which you can then fill in the details below
# This determine if the user will be user automatically be confirmed(verified) when they sign up
# If this is enabled, it requires a clicking a confirmation link in the email after a user signs up.
# If you do not have SMTP service set up, or any other OAuth2 method, you should set this to true,
# or else no user will be able to be authenticated
GOTRUE_MAILER_AUTOCONFIRM=true
# if you enable mail confirmation, you need to set the SMTP configuration below

View File

@ -213,26 +213,38 @@ async fn setup_admin_account(
) -> Result<(), Error> {
let admin_email = gotrue_setting.admin_email.as_str();
let password = gotrue_setting.admin_password.as_str();
gotrue_client
.sign_up(admin_email, password)
.await
.context("failed to sign-up for admin user")?;
let res_resp = gotrue_client.sign_up(admin_email, password).await;
// Unable to use query! macro here instead
// because of the auth is a not default schema
// hopefully this will be fixed in the future
sqlx::query(
r#"
UPDATE auth.users
SET role = 'supabase_admin', email_confirmed_at = NOW()
WHERE email = $1
"#,
)
.bind(admin_email)
.execute(pg_pool)
.await
.context("failed to update the admin user")?;
Ok(())
match res_resp {
Ok(resp) => match resp {
gotrue_entity::dto::SignUpResponse::Authenticated(_) => {
tracing::info!("Admin user already authenticated");
Ok(())
},
gotrue_entity::dto::SignUpResponse::NotAuthenticated(user) => {
let user_id = user.id.parse::<uuid::Uuid>().unwrap();
sqlx::query!(
r#"
UPDATE auth.users
SET role = 'supabase_admin', email_confirmed_at = NOW()
WHERE id = $1
"#, user_id
)
// .bind(user.id)
.execute(pg_pool)
.await
.context("failed to update the admin user")?;
Ok(())
},
},
Err(err) => match (err.code, err.msg.as_str()) {
(400, "User already registered") => {
tracing::info!("Admin user already registered");
Ok(())
},
_ => Err(err.into()),
},
}
}
async fn get_redis_client(redis_uri: &str) -> Result<redis::aio::ConnectionManager, Error> {