feat: add OTP http request (#1316)

* feat: add OTP http request

* feat: add password api
This commit is contained in:
Lucas 2025-04-09 13:22:42 +08:00 committed by GitHub
parent 3181b17d60
commit f300884dde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 24 deletions

View file

@ -15,8 +15,8 @@ use client_api_entity::WorkspaceInviteCodeParams;
use client_api_entity::WorkspaceInviteToken as WorkspaceInviteCode;
use gotrue::grant::PasswordGrant;
use gotrue::grant::{Grant, RefreshTokenGrant};
use gotrue::params::MagicLinkParams;
use gotrue::params::{AdminUserParams, GenerateLinkParams};
use gotrue::params::{MagicLinkParams, VerifyParams, VerifyType};
use reqwest::StatusCode;
use shared_entity::dto::workspace_dto::{CreateWorkspaceParam, PatchWorkspaceParam};
use std::borrow::Cow;
@ -248,6 +248,24 @@ impl Client {
self.token.read().subscribe()
}
#[instrument(skip_all, err)]
pub async fn sign_in_password(
&self,
email: &str,
password: &str,
) -> Result<GotrueTokenResponse, AppResponseError> {
let response = self
.gotrue_client
.token(&Grant::Password(PasswordGrant {
email: email.to_owned(),
password: password.to_owned(),
}))
.await?;
let _ = self.verify_token_cloud(&response.access_token).await?;
self.token.write().set(response.clone());
Ok(response)
}
/// Sign in with magic link
///
/// User will receive an email with a magic link to sign in.
@ -274,6 +292,30 @@ impl Client {
Ok(())
}
/// Sign in with passcode (OTP)
///
/// User will receive an email with a passcode to sign in.
///
/// For more information, please refer to the sign_in_with_magic_link function.
#[instrument(level = "debug", skip_all, err)]
pub async fn sign_in_with_passcode(
&self,
email: &str,
passcode: &str,
) -> Result<GotrueTokenResponse, AppResponseError> {
let response = self
.gotrue_client
.verify(&VerifyParams {
email: email.to_owned(),
token: passcode.to_owned(),
type_: VerifyType::Recovery,
})
.await?;
let _ = self.verify_token_cloud(&response.access_token).await?;
self.token.write().set(response.clone());
Ok(response)
}
/// 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`.
///
@ -787,31 +829,11 @@ impl Client {
process_response_data::<WorkspaceInviteCode>(resp).await
}
#[instrument(skip_all, err)]
pub async fn sign_in_password(
&self,
email: &str,
password: &str,
) -> Result<bool, AppResponseError> {
let access_token_resp = self
.gotrue_client
.token(&Grant::Password(PasswordGrant {
email: email.to_owned(),
password: password.to_owned(),
}))
.await?;
let is_new = self
.verify_token_cloud(&access_token_resp.access_token)
.await?;
self.token.write().set(access_token_resp);
Ok(is_new)
}
#[instrument(level = "info", skip_all, err)]
pub async fn sign_up(&self, email: &str, password: &str) -> Result<(), AppResponseError> {
match self.gotrue_client.sign_up(email, password, None).await? {
Authenticated(access_token_resp) => {
self.token.write().set(access_token_resp);
self.token.write().set(access_token_resp.clone());
Ok(())
},
NotAuthenticated(user) => {

View file

@ -19,7 +19,7 @@ pub struct AdminListUsersResponse {
pub aud: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct User {
pub id: String,
@ -72,7 +72,7 @@ pub struct Factor {
pub factor_type: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GotrueTokenResponse {
/// the token that clients use to make authenticated requests to the server or API. It is a bearer token that provides temporary, secure access to server resources.
pub access_token: String,