diff --git a/src/modules/auth.ts b/src/modules/auth.ts index 0736da2..50f0e0e 100644 --- a/src/modules/auth.ts +++ b/src/modules/auth.ts @@ -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) { diff --git a/src/modules/auth.types.ts b/src/modules/auth.types.ts index 6124f4b..fe349a9 100644 --- a/src/modules/auth.types.ts +++ b/src/modules/auth.types.ts @@ -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. *