From b785b1a4821cfd3de3e3cf455103734d49e0bb62 Mon Sep 17 00:00:00 2001 From: Fu Zi Xiang Date: Mon, 6 Nov 2023 13:50:38 +0800 Subject: [PATCH] fix: restartable server when autoconfirm is true --- ...d6290b7d73425f82bcf63ad4a0bd6959047d5.json | 14 ++++++ dev.env | 4 +- doc/deployment.md | 9 ++-- src/application.rs | 50 ++++++++++++------- 4 files changed, 52 insertions(+), 25 deletions(-) create mode 100644 .sqlx/query-141454ccce32ab6abd9fba21292d6290b7d73425f82bcf63ad4a0bd6959047d5.json diff --git a/.sqlx/query-141454ccce32ab6abd9fba21292d6290b7d73425f82bcf63ad4a0bd6959047d5.json b/.sqlx/query-141454ccce32ab6abd9fba21292d6290b7d73425f82bcf63ad4a0bd6959047d5.json new file mode 100644 index 00000000..6c359972 --- /dev/null +++ b/.sqlx/query-141454ccce32ab6abd9fba21292d6290b7d73425f82bcf63ad4a0bd6959047d5.json @@ -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" +} diff --git a/dev.env b/dev.env index eaa905f8..c21673b6 100644 --- a/dev.env +++ b/dev.env @@ -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 diff --git a/doc/deployment.md b/doc/deployment.md index a9cd989c..eb9d55c1 100644 --- a/doc/deployment.md +++ b/doc/deployment.md @@ -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 diff --git a/src/application.rs b/src/application.rs index 1265b345..885c11df 100644 --- a/src/application.rs +++ b/src/application.rs @@ -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::().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 {