Skip to content

Commit

Permalink
Fixed Invite Method
Browse files Browse the repository at this point in the history
  • Loading branch information
Proziam committed Oct 21, 2024
1 parent ed97398 commit ba6afe2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ use std::env;

use reqwest::{
header::{self, HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE},
Client, Response, Url,
Client, Url,
};
use serde_json::from_str;
use serde_json::{from_str, Value};

use crate::{
error::Error::{self, AuthError},
models::{
AuthClient, AuthServerHealth, AuthServerSettings, IdTokenCredentials, LogoutScope,
OAuthResponse, OTPResponse, Provider, RefreshSessionPayload, RequestMagicLinkPayload,
ResendParams, ResetPasswordForEmailPayload, SendSMSOtpPayload, Session,
SignInEmailOtpParams, SignInWithEmailAndPasswordPayload, SignInWithEmailOtpPayload,
SignInWithOAuthOptions, SignInWithPhoneAndPasswordPayload, SignInWithSSO,
SignUpWithEmailAndPasswordPayload, SignUpWithPasswordOptions,
AuthClient, AuthServerHealth, AuthServerSettings, IdTokenCredentials, InviteParams,
LogoutScope, OAuthResponse, OTPResponse, Provider, RefreshSessionPayload,
RequestMagicLinkPayload, ResendParams, ResetPasswordForEmailPayload, SendSMSOtpPayload,
Session, SignInEmailOtpParams, SignInWithEmailAndPasswordPayload,
SignInWithEmailOtpPayload, SignInWithOAuthOptions, SignInWithPhoneAndPasswordPayload,
SignInWithSSO, SignUpWithEmailAndPasswordPayload, SignUpWithPasswordOptions,
SignUpWithPhoneAndPasswordPayload, UpdateUserPayload, User, VerifyOtpParams, AUTH_V1,
},
};
Expand Down Expand Up @@ -555,14 +555,27 @@ impl AuthClient {
Ok(session)
}

// TODO: Add test
/// Sends an invite link to an email address.
pub async fn invite_user_by_email<S: Into<String>>(&self, email: S) -> Result<User, Error> {
pub async fn invite_user_by_email<S: Into<String>>(
&self,
email: S,
data: Option<Value>,
bearer_token: S,
) -> Result<User, Error> {
let mut headers = HeaderMap::new();
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {}", &bearer_token.into()))?,
);

let invite_payload = InviteParams {
email: email.into(),
data,
};

let body = serde_json::to_string(&email.into())?;
let body = serde_json::to_string(&invite_payload)?;

let response = self
.client
Expand Down
17 changes: 17 additions & 0 deletions tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,23 @@ async fn test_sso_login() {
assert!(url.to_string().len() > 1);
}

#[tokio::test]
async fn invite_by_email_test() {
let auth_client = create_test_client();

let demo_email = env::var("DEMO_INVITE").unwrap();

println!("{}", auth_client.api_key());

let user = auth_client
// NOTE: Requires admin permissions to issue invites
.invite_user_by_email(&demo_email, None, auth_client.api_key())
.await
.unwrap();

assert!(user.email == demo_email)
}

#[tokio::test]
async fn get_settings_test() {
let auth_client = create_test_client();
Expand Down

0 comments on commit ba6afe2

Please sign in to comment.