Skip to content

Commit ba6afe2

Browse files
committed
Fixed Invite Method
1 parent ed97398 commit ba6afe2

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/client.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ use std::env;
44

55
use reqwest::{
66
header::{self, HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE},
7-
Client, Response, Url,
7+
Client, Url,
88
};
9-
use serde_json::from_str;
9+
use serde_json::{from_str, Value};
1010

1111
use crate::{
1212
error::Error::{self, AuthError},
1313
models::{
14-
AuthClient, AuthServerHealth, AuthServerSettings, IdTokenCredentials, LogoutScope,
15-
OAuthResponse, OTPResponse, Provider, RefreshSessionPayload, RequestMagicLinkPayload,
16-
ResendParams, ResetPasswordForEmailPayload, SendSMSOtpPayload, Session,
17-
SignInEmailOtpParams, SignInWithEmailAndPasswordPayload, SignInWithEmailOtpPayload,
18-
SignInWithOAuthOptions, SignInWithPhoneAndPasswordPayload, SignInWithSSO,
19-
SignUpWithEmailAndPasswordPayload, SignUpWithPasswordOptions,
14+
AuthClient, AuthServerHealth, AuthServerSettings, IdTokenCredentials, InviteParams,
15+
LogoutScope, OAuthResponse, OTPResponse, Provider, RefreshSessionPayload,
16+
RequestMagicLinkPayload, ResendParams, ResetPasswordForEmailPayload, SendSMSOtpPayload,
17+
Session, SignInEmailOtpParams, SignInWithEmailAndPasswordPayload,
18+
SignInWithEmailOtpPayload, SignInWithOAuthOptions, SignInWithPhoneAndPasswordPayload,
19+
SignInWithSSO, SignUpWithEmailAndPasswordPayload, SignUpWithPasswordOptions,
2020
SignUpWithPhoneAndPasswordPayload, UpdateUserPayload, User, VerifyOtpParams, AUTH_V1,
2121
},
2222
};
@@ -555,14 +555,27 @@ impl AuthClient {
555555
Ok(session)
556556
}
557557

558-
// TODO: Add test
559558
/// Sends an invite link to an email address.
560-
pub async fn invite_user_by_email<S: Into<String>>(&self, email: S) -> Result<User, Error> {
559+
pub async fn invite_user_by_email<S: Into<String>>(
560+
&self,
561+
email: S,
562+
data: Option<Value>,
563+
bearer_token: S,
564+
) -> Result<User, Error> {
561565
let mut headers = HeaderMap::new();
562566
headers.insert("apikey", HeaderValue::from_str(&self.api_key)?);
563567
headers.insert(CONTENT_TYPE, HeaderValue::from_str("application/json")?);
568+
headers.insert(
569+
AUTHORIZATION,
570+
HeaderValue::from_str(&format!("Bearer {}", &bearer_token.into()))?,
571+
);
572+
573+
let invite_payload = InviteParams {
574+
email: email.into(),
575+
data,
576+
};
564577

565-
let body = serde_json::to_string(&email.into())?;
578+
let body = serde_json::to_string(&invite_payload)?;
566579

567580
let response = self
568581
.client

tests/client_tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,23 @@ async fn test_sso_login() {
398398
assert!(url.to_string().len() > 1);
399399
}
400400

401+
#[tokio::test]
402+
async fn invite_by_email_test() {
403+
let auth_client = create_test_client();
404+
405+
let demo_email = env::var("DEMO_INVITE").unwrap();
406+
407+
println!("{}", auth_client.api_key());
408+
409+
let user = auth_client
410+
// NOTE: Requires admin permissions to issue invites
411+
.invite_user_by_email(&demo_email, None, auth_client.api_key())
412+
.await
413+
.unwrap();
414+
415+
assert!(user.email == demo_email)
416+
}
417+
401418
#[tokio::test]
402419
async fn get_settings_test() {
403420
let auth_client = create_test_client();

0 commit comments

Comments
 (0)