From 09469e86ca2bcc4eae7acb7a80ed02710444b0e9 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 2 Apr 2024 14:45:50 +0800 Subject: [PATCH 1/3] feat: expose magic link api --- libs/client-api/src/http.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/libs/client-api/src/http.rs b/libs/client-api/src/http.rs index 7d6e1585..50ac6150 100644 --- a/libs/client-api/src/http.rs +++ b/libs/client-api/src/http.rs @@ -215,6 +215,26 @@ impl Client { self.token.read().subscribe() } + /// Sign in with magic link + #[instrument(level = "debug", skip_all, err)] + pub async fn sign_in_with_magic_link( + &self, + email: &str, + redirect_to: Option, + ) -> Result<(), AppResponseError> { + self + .gotrue_client + .magic_link( + &MagicLinkParams { + email: email.to_owned(), + ..Default::default() + }, + redirect_to, + ) + .await?; + Ok(()) + } + /// Attempts to sign in using a URL, extracting refresh_token from the URL. /// It looks like, e.g., `appflowy-flutter://#access_token=...&expires_in=3600&provider_token=...&refresh_token=...&token_type=bearer`. /// From 73e4d28500350d16fcca6e04cf861583a1faf86a Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Wed, 10 Apr 2024 10:34:44 +0800 Subject: [PATCH 2/3] test: add magic link test --- tests/user/sign_in.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/user/sign_in.rs b/tests/user/sign_in.rs index 0d859409..59d2591c 100644 --- a/tests/user/sign_in.rs +++ b/tests/user/sign_in.rs @@ -101,3 +101,11 @@ async fn sign_in_with_url() { let is_new = c.sign_in_with_url(&sign_in_url).await.unwrap(); assert!(is_new); } + +#[tokio::test] +async fn sign_in_with_magic_link() { + let c = localhost_client(); + let email = generate_unique_email(); + let resp = c.sign_in_with_magic_link(&email, None).await; + assert!(resp.is_ok()); +} From 85ac96a24b0045ff9fad0f6dd77e873033039401 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Wed, 10 Apr 2024 11:07:14 +0800 Subject: [PATCH 3/3] chore: add comment for sign_in_with_magic_link --- libs/client-api/src/http.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libs/client-api/src/http.rs b/libs/client-api/src/http.rs index 50ac6150..48813f5e 100644 --- a/libs/client-api/src/http.rs +++ b/libs/client-api/src/http.rs @@ -216,6 +216,12 @@ impl Client { } /// Sign in with magic link + /// + /// User will receive an email with a magic link to sign in. + /// The redirect_to parameter is optional. If provided, the user will be redirected to the specified URL after signing in. + /// If not, the user will be redirected to the appflowy-flutter:// by default + /// + /// The redirect_to should be the scheme of the app, e.g., appflowy-flutter:// #[instrument(level = "debug", skip_all, err)] pub async fn sign_in_with_magic_link( &self,