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 GOTRUE_JWT_SECRET=hello456
# user sign up will automatically be confirmed if this is set to true # 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 # if you enable mail confirmation, you need to set the SMTP configuration below
GOTRUE_SMTP_HOST=smtp.gmail.com 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 # This is the secret key for authentication, please change this and keep the key safe
GOTRUE_JWT_SECRET=hello456 GOTRUE_JWT_SECRET=hello456
# This determine if the user will be user automatically be confirmed when they sign up # 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 which user # If this is enabled, it requires a clicking a confirmation link in the email after a user signs up.
# use for sign up. # If you do not have SMTP service set up, or any other OAuth2 method, you should set this to true,
# Pre-requisite if you enable: you need to have your SMTP Service set up, # or else no user will be able to be authenticated
# which you can then fill in the details below
GOTRUE_MAILER_AUTOCONFIRM=true GOTRUE_MAILER_AUTOCONFIRM=true
# if you enable mail confirmation, you need to set the SMTP configuration below # 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> { ) -> Result<(), Error> {
let admin_email = gotrue_setting.admin_email.as_str(); let admin_email = gotrue_setting.admin_email.as_str();
let password = gotrue_setting.admin_password.as_str(); let password = gotrue_setting.admin_password.as_str();
gotrue_client let res_resp = gotrue_client.sign_up(admin_email, password).await;
.sign_up(admin_email, password)
.await
.context("failed to sign-up for admin user")?;
// Unable to use query! macro here instead match res_resp {
// because of the auth is a not default schema Ok(resp) => match resp {
// hopefully this will be fixed in the future gotrue_entity::dto::SignUpResponse::Authenticated(_) => {
sqlx::query( tracing::info!("Admin user already authenticated");
r#" Ok(())
UPDATE auth.users },
SET role = 'supabase_admin', email_confirmed_at = NOW() gotrue_entity::dto::SignUpResponse::NotAuthenticated(user) => {
WHERE email = $1 let user_id = user.id.parse::<uuid::Uuid>().unwrap();
"#, sqlx::query!(
) r#"
.bind(admin_email) UPDATE auth.users
.execute(pg_pool) SET role = 'supabase_admin', email_confirmed_at = NOW()
.await WHERE id = $1
.context("failed to update the admin user")?; "#, user_id
Ok(()) )
// .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> { async fn get_redis_client(redis_uri: &str) -> Result<redis::aio::ConnectionManager, Error> {