diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2024-06-24 14:54:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-06-24 14:54:54 +0100 |
| commit | 67d64ec4b368c48188c746f2dba2967ec4615fe5 (patch) | |
| tree | 9da8443d4baa424e99a806cb022d53daa6a8c30e /crates/atuin-client/src | |
| parent | fix: Some --help comments didn't show properly (#2176) (diff) | |
| download | atuin-67d64ec4b368c48188c746f2dba2967ec4615fe5.zip | |
feat: add user account verification (#2190)
* add verified column to users table
* add database functions to check if verified, or to verify
* getting there
* verification check
* use base64 urlsafe no pad
* add verification client
* clippy
* correct docs
* fix integration tests
Diffstat (limited to 'crates/atuin-client/src')
| -rw-r--r-- | crates/atuin-client/src/api_client.rs | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/crates/atuin-client/src/api_client.rs b/crates/atuin-client/src/api_client.rs index 60f2d300..53f24424 100644 --- a/crates/atuin-client/src/api_client.rs +++ b/crates/atuin-client/src/api_client.rs @@ -11,8 +11,9 @@ use reqwest::{ use atuin_common::{ api::{ AddHistoryRequest, ChangePasswordRequest, CountResponse, DeleteHistoryRequest, - ErrorResponse, LoginRequest, LoginResponse, MeResponse, RegisterResponse, StatusResponse, - SyncHistoryResponse, + ErrorResponse, LoginRequest, LoginResponse, MeResponse, RegisterResponse, + SendVerificationResponse, StatusResponse, SyncHistoryResponse, VerificationTokenRequest, + VerificationTokenResponse, }, record::RecordStatus, }; @@ -403,4 +404,35 @@ impl<'a> Client<'a> { bail!("Unknown error"); } } + + // Either request a verification email if token is null, or validate a token + pub async fn verify(&self, token: Option<String>) -> Result<(bool, bool)> { + // could dedupe this a bit, but it's simple at the moment + let (email_sent, verified) = if let Some(token) = token { + let url = format!("{}/api/v0/account/verify", self.sync_addr); + let url = Url::parse(url.as_str())?; + + let resp = self + .client + .post(url) + .json(&VerificationTokenRequest { token }) + .send() + .await?; + let resp = handle_resp_error(resp).await?; + let resp = resp.json::<VerificationTokenResponse>().await?; + + (false, resp.verified) + } else { + let url = format!("{}/api/v0/account/send-verification", self.sync_addr); + let url = Url::parse(url.as_str())?; + + let resp = self.client.post(url).send().await?; + let resp = handle_resp_error(resp).await?; + let resp = resp.json::<SendVerificationResponse>().await?; + + (resp.email_sent, resp.verified) + }; + + Ok((email_sent, verified)) + } } |
