Skip to content

Commit

Permalink
Added send email with OTP
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Biggs committed Sep 14, 2024
1 parent 3592d0e commit ad9861d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
36 changes: 32 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use reqwest::{
use crate::{
error::Error,
models::{
Provider, RequestMagicLinkPayload, Session, SignInWithEmailAndPasswordPayload,
SignInWithIdTokenCredentials, SignInWithPhoneAndPasswordPayload,
SignUpWithEmailAndPasswordPayload, SignUpWithPhoneAndPasswordPayload, UpdateUserPayload,
User, VerifyOtpParams,
Provider, RequestMagicLinkPayload, Session, SignInEmailOtpParams,
SignInWithEmailAndPasswordPayload, SignInWithEmailOtpPayload, SignInWithIdTokenCredentials,
SignInWithPhoneAndPasswordPayload, SignUpWithEmailAndPasswordPayload,
SignUpWithPhoneAndPasswordPayload, UpdateUserPayload, User, VerifyOtpParams,
},
};

Expand Down Expand Up @@ -229,6 +229,34 @@ impl AuthClient {
Ok(response)
}

// Login with Email OTP
pub async fn send_email_with_otp<S: Into<String>>(
&self,
email: S,
options: Option<SignInEmailOtpParams>,
) -> Result<Response, Error> {
let payload = SignInWithEmailOtpPayload {
email: email.into(),
options,
};

let mut headers = header::HeaderMap::new();
headers.insert("Content-Type", "application/json".parse().unwrap());
headers.insert("apikey", self.api_key.parse().unwrap());

let body = serde_json::to_string(&payload)?;

let response = self
.client
.post(format!("{}/auth/v1/otp", self.project_url))
.headers(headers)
.body(body)
.send()
.await?;

Ok(response)
}

// TODO: Add scopes and redirects and query params
pub async fn sign_in_with_oauth(&self, provider: Provider) -> Result<Response, Error> {
let mut headers = header::HeaderMap::new();
Expand Down
64 changes: 62 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::fmt;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::fmt::{write, Display};

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct Session {
Expand Down Expand Up @@ -202,7 +202,67 @@ pub struct VerifyOtpOptions {
pub redirect_to: Option<String>,
}

#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Deserialize)]
pub enum SignInWithOtp {
Mobile(SignInMobileOtpParams),
Email(SignInEmailOtpParams),
WhatsApp(SignInMobileOtpParams),
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SignInWithEmailOtpPayload {
pub email: String,
pub options: Option<SignInEmailOtpParams>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SignInWithEmailOtp {
/// The user's phone number.
pub email: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<SignInEmailOtpParams>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SignInEmailOtpParams {
/// Verification token received when the user completes the captcha on the site.
pub captcha_token: Option<String>,
/// A custom data object to store the user's metadata. This maps to the `auth.users.raw_user_meta_data` column.
pub data: Option<serde_json::Value>,
/// The redirect url embedded in the email link
pub email_redirect_to: Option<String>,
/// If set to false, this method will not create a new user. Defaults to true.
pub should_create_user: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SignInMobileOtpParams {
/// Verification token received when the user completes the captcha on the site.
pub captcha_token: Option<String>,
/// A custom data object to store the user's metadata. This maps to the `auth.users.raw_user_meta_data` column.
pub data: Option<serde_json::Value>,
/// The redirect url embedded in the email link
pub channel: Option<Channel>,
/// If set to false, this method will not create a new user. Defaults to true.
pub should_create_user: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
pub enum Channel {
Sms,
Whatsapp,
}

impl Display for Channel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match *self {
Channel::Sms => write!(f, "sms"),
Channel::Whatsapp => write!(f, "whatsapp"),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
pub enum Provider {
Apple,
Azure,
Expand Down

0 comments on commit ad9861d

Please sign in to comment.