diff --git a/src/client.ts b/src/client.ts index 261367c..3a035f8 100644 --- a/src/client.ts +++ b/src/client.ts @@ -31,6 +31,7 @@ export function createClient(config: { serviceToken?: string; requiresAuth?: boolean; functionsVersion?: string; + headers?: Record; options?: CreateClientOptions; onRedirectToLogin?: () => void; }) { @@ -43,6 +44,7 @@ export function createClient(config: { options, functionsVersion, onRedirectToLogin, + headers: optionalHeaders, } = config; const socketConfig: RoomsSocketConfig = { @@ -58,6 +60,7 @@ export function createClient(config: { }); const headers = { + ...optionalHeaders, "X-App-Id": String(appId), }; diff --git a/src/modules/auth.ts b/src/modules/auth.ts index db10ed0..651e941 100644 --- a/src/modules/auth.ts +++ b/src/modules/auth.ts @@ -54,7 +54,9 @@ export function createAuthModule( const redirectUrl = nextUrl || window.location.href; // Build the login URL - const loginUrl = `/login?from_url=${encodeURIComponent(redirectUrl)}`; + const loginUrl = `${ + options.serverUrl + }/login?from_url=${encodeURIComponent(redirectUrl)}&app_id=${appId}`; // Redirect to the login page window.location.href = loginUrl; @@ -172,5 +174,67 @@ export function createAuthModule( return false; } }, + + inviteUser(userEmail: string, role: string) { + return axios.post(`/apps/${appId}/users/invite-user`, { + user_email: userEmail, + role, + }); + }, + + register(payload: { + email: string; + password: string; + turnstile_token?: string | null; + referral_code?: string | null; + }) { + return axios.post(`/apps/${appId}/auth/register`, payload); + }, + + verifyOtp({ email, otpCode }: { email: string; otpCode: string }) { + return axios.post(`/apps/${appId}/auth/verify-otp`, { + email, + otp_code: otpCode, + }); + }, + + resendOtp(email: string) { + return axios.post(`/apps/${appId}/auth/resend-otp`, { email }); + }, + + resetPasswordRequest(email: string) { + return axios.post(`/apps/${appId}/auth/reset-password-request`, { + email, + }); + }, + + resetPassword({ + resetToken, + newPassword, + }: { + resetToken: string; + newPassword: string; + }) { + return axios.post(`/apps/${appId}/auth/reset-password`, { + reset_token: resetToken, + new_password: newPassword, + }); + }, + + changePassword({ + userId, + currentPassword, + newPassword, + }: { + userId: string; + currentPassword: string; + newPassword: string; + }) { + return axios.post(`/apps/${appId}/auth/change-password`, { + user_id: userId, + current_password: currentPassword, + new_password: newPassword, + }); + }, }; } diff --git a/tests/unit/auth.test.js b/tests/unit/auth.test.js index 1e9afbb..e871ceb 100644 --- a/tests/unit/auth.test.js +++ b/tests/unit/auth.test.js @@ -149,7 +149,7 @@ describe('Auth Module', () => { // Verify the redirect URL was set correctly expect(mockLocation.href).toBe( - `/login?from_url=${encodeURIComponent(nextUrl)}` + `${serverUrl}/login?from_url=${encodeURIComponent(nextUrl)}&app_id=${appId}` ); // Restore window @@ -169,7 +169,7 @@ describe('Auth Module', () => { // Verify the redirect URL uses current URL expect(mockLocation.href).toBe( - `/login?from_url=${encodeURIComponent(currentUrl)}` + `${serverUrl}/login?from_url=${encodeURIComponent(currentUrl)}&app_id=${appId}` ); // Restore window