Skip to content
Open
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
17 changes: 17 additions & 0 deletions src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ export function createAuthModule(
window.location.href = loginUrl;
},

// Redirects the user to a provider's login page
loginWithProvider(provider: string, fromUrl: string = "/") {
// Build the full redirect URL
const redirectUrl = new URL(fromUrl, window.location.origin).toString();

// Build the provider login URL (google is the default, so no provider path needed)
const providerPath = provider === "google" ? "" : `/${provider}`;
const loginUrl = `${
options.serverUrl
}/api/apps/auth${providerPath}/login?app_id=${appId}&from_url=${encodeURIComponent(
redirectUrl
)}`;

// Redirect to the provider login page
window.location.href = loginUrl;
},

// Logout the current user
// Removes the token from localStorage and optionally redirects to a URL or reloads the page
logout(redirectUrl?: string) {
Expand Down
22 changes: 22 additions & 0 deletions src/modules/auth.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@ export interface AuthModule {
*/
redirectToLogin(nextUrl: string): void;

/**
* Redirects the user to a third-party authentication provider's login page.
*
* Initiates OAuth/SSO login flow with providers like Google, Microsoft, etc. Requires a browser environment and can't be used in the backend.
*
* @param provider - Name of the supported authentication provider (e.g., 'google', 'microsoft').
* @param fromUrl - URL to redirect to after successful authentication. Defaults to '/'.
*
* @example
* ```typescript
* // Login with Google and return to current page
* base44.auth.loginWithProvider('google', window.location.pathname);
* ```
*
* @example
* ```typescript
* // Login with GitHub and redirect to dashboard
* base44.auth.loginWithProvider('microsoft', '/dashboard');
* ```
*/
loginWithProvider(provider: string, fromUrl?: string): void;

/**
* Logs out the current user.
*
Expand Down