diff --git a/admin_frontend/src/web_api.rs b/admin_frontend/src/web_api.rs index 03899a3d..3071c467 100644 --- a/admin_frontend/src/web_api.rs +++ b/admin_frontend/src/web_api.rs @@ -19,11 +19,12 @@ use gotrue::params::{ MagicLinkParams, }; use gotrue_entity::dto::{GotrueTokenResponse, SignUpResponse, UpdateGotrueUserParams, User}; -use gotrue_entity::error::GoTrueError; +use tracing::info; pub fn router() -> Router { Router::new() - .route("/login", post(login_handler)) + .route("/signin", post(sign_in_handler)) + .route("/signup", post(sign_up_handler)) .route("/login_refresh/:refresh_token", post(login_refresh_handler)) .route("/logout", post(logout_handler)) @@ -112,18 +113,14 @@ pub async fn open_app_handler(session: UserSession) -> Result, - session: UserSession, Form(param): Form, ) -> Result, WebApiError<'static>> { state .gotrue_client - .magic_link( - &session.token.access_token, - &MagicLinkParams { - email: param.email, - ..Default::default() - }, - ) + .magic_link(&MagicLinkParams { + email: param.email, + ..Default::default() + }) .await?; Ok(WebApiResponse::<()>::from_str("Invitation sent".into())) } @@ -275,13 +272,18 @@ pub async fn login_refresh_handler( // login and set the cookie // sign up if not exist -pub async fn login_handler( +pub async fn sign_in_handler( State(state): State, jar: CookieJar, Form(param): Form, ) -> Result<(CookieJar, HeaderMap, WebApiResponse<()>), WebApiError<'static>> { + if param.password.is_empty() { + let res = send_magic_link(State(state), ¶m.email).await?; + return Ok((CookieJar::new(), HeaderMap::new(), res)); + } + // Attempt to sign in with email and password - let token_res = state + let token = state .gotrue_client .token(&gotrue::grant::Grant::Password( gotrue::grant::PasswordGrant { @@ -289,48 +291,36 @@ pub async fn login_handler( password: param.password.to_owned(), }, )) - .await; + .await?; - match token_res { - Ok(token) => session_login(State(state), token, jar).await, // login success - Err(err) => match &err { - GoTrueError::ClientError(client_err) => { - match ( - client_err.error.as_str(), - client_err.error_description.as_deref(), - ) { - // Email not exist or wrong password - ("invalid_grant", Some("Invalid login credentials")) => { - let sign_up_res = state - .gotrue_client - .sign_up_with_referrer(¶m.email, ¶m.password, Some("/")) - .await; + session_login(State(state), token, jar).await +} - match sign_up_res { - Ok(resp) => match resp { - // when GOTRUE_MAILER_AUTOCONFIRM=true, auto sign in - SignUpResponse::Authenticated(token) => { - session_login(State(state), token, jar).await - }, - SignUpResponse::NotAuthenticated(user) => match user.identities { - Some(_identities) => { - // new user, awaiting email verification - Ok(( - jar, - HeaderMap::new(), - WebApiResponse::<()>::from_str("Email Verification Sent".into()), - )) - }, - None => Err(err.into()), // user exists but sign in password not correct - }, - }, - Err(err) => Err(err.into()), - } - }, - _ => Err(err.into()), - } - }, - _ => Err(err.into()), +pub async fn sign_up_handler( + State(state): State, + jar: CookieJar, + Form(param): Form, +) -> Result<(CookieJar, HeaderMap, WebApiResponse<()>), WebApiError<'static>> { + if param.password.is_empty() { + let res = send_magic_link(State(state), ¶m.email).await?; + return Ok((CookieJar::new(), HeaderMap::new(), res)); + } + + let sign_up_res = state + .gotrue_client + .sign_up_with_referrer(¶m.email, ¶m.password, Some("/")) + .await?; + + match sign_up_res { + // when GOTRUE_MAILER_AUTOCONFIRM=true, auto sign in + SignUpResponse::Authenticated(token) => session_login(State(state), token, jar).await, + SignUpResponse::NotAuthenticated(user) => { + info!("user signed up and not authenticated: {:?}", user); + Ok(( + jar, + HeaderMap::new(), + WebApiResponse::<()>::from_str("Email Verification Sent".into()), + )) }, } } @@ -382,6 +372,22 @@ async fn session_login( )) } +async fn send_magic_link( + State(state): State, + email: &str, +) -> Result, WebApiError<'static>> { + Ok( + state + .gotrue_client + .magic_link(&MagicLinkParams { + email: email.to_owned(), + ..Default::default() + }) + .await? + .into(), + ) +} + fn get_base_url(header_map: &HeaderMap) -> String { let scheme = get_header_value_or_default(header_map, "x-scheme", "http"); let host = get_header_value_or_default(header_map, "host", "localhost"); diff --git a/admin_frontend/templates/components/admin_sso_create.html b/admin_frontend/templates/components/admin_sso_create.html index dfa789d7..206b66df 100644 --- a/admin_frontend/templates/components/admin_sso_create.html +++ b/admin_frontend/templates/components/admin_sso_create.html @@ -1,6 +1,6 @@

Please enter the following information to create new SSO

-
+ diff --git a/admin_frontend/templates/components/admin_user_details.html b/admin_frontend/templates/components/admin_user_details.html index d0cd5db6..3b58ec4c 100644 --- a/admin_frontend/templates/components/admin_user_details.html +++ b/admin_frontend/templates/components/admin_user_details.html @@ -2,7 +2,7 @@ {% include "user_details.html" %}
- +
Email
diff --git a/admin_frontend/templates/components/change_password.html b/admin_frontend/templates/components/change_password.html index 204d6910..560154d0 100644 --- a/admin_frontend/templates/components/change_password.html +++ b/admin_frontend/templates/components/change_password.html @@ -1,6 +1,6 @@

Password Change

- +
Set Password:
diff --git a/admin_frontend/templates/components/create_user.html b/admin_frontend/templates/components/create_user.html index e8fc6a6c..5df3efb9 100644 --- a/admin_frontend/templates/components/create_user.html +++ b/admin_frontend/templates/components/create_user.html @@ -1,6 +1,6 @@

Please enter the following information to create a new user

- +
New Password:
diff --git a/admin_frontend/templates/components/invite.html b/admin_frontend/templates/components/invite.html index 6a41bd79..125deff1 100644 --- a/admin_frontend/templates/components/invite.html +++ b/admin_frontend/templates/components/invite.html @@ -1,6 +1,6 @@

Please enter the following email invite a new user

- +
Email:
diff --git a/admin_frontend/templates/layouts/base.html b/admin_frontend/templates/layouts/base.html index c329fcb0..ef8177cc 100644 --- a/admin_frontend/templates/layouts/base.html +++ b/admin_frontend/templates/layouts/base.html @@ -24,18 +24,31 @@
Email: