Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function createClient(config: {
serviceToken?: string;
requiresAuth?: boolean;
functionsVersion?: string;
headers?: Record<string, string>;
options?: CreateClientOptions;
onRedirectToLogin?: () => void;
}) {
Expand All @@ -43,6 +44,7 @@ export function createClient(config: {
options,
functionsVersion,
onRedirectToLogin,
headers: optionalHeaders,
} = config;

const socketConfig: RoomsSocketConfig = {
Expand All @@ -58,6 +60,7 @@ export function createClient(config: {
});

const headers = {
...optionalHeaders,
"X-App-Id": String(appId),
};

Expand Down
66 changes: 65 additions & 1 deletion src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
});
},
};
}
4 changes: 2 additions & 2 deletions tests/unit/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down