Skip to content
Open
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
24 changes: 21 additions & 3 deletions src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,33 @@
);
}

const hostname = window.location.hostname;
const isPreview = hostname.startsWith("preview--");

Check failure on line 47 in src/modules/auth.ts

View workflow job for this annotation

GitHub Actions / test

tests/unit/auth.test.js > Auth Module > login() > should redirect to login page with correct URL in browser environment

TypeError: Cannot read properties of undefined (reading 'startsWith') ❯ Object.redirectToLogin src/modules/auth.ts:47:34 ❯ tests/unit/auth.test.js:148:19

// Skip redirect if already on login page (but not on preview - preview should redirect to main app)
if (window.location.pathname === "/login" && !isPreview) {
return;
}

// If nextUrl is not provided, use the current URL
const redirectUrl = nextUrl
? new URL(nextUrl, window.location.origin).toString()
: window.location.href;

// For preview URLs (preview--*), redirect to main app's login page
// but keep from_url pointing to the preview URL
let loginBaseUrl = options.appBaseUrl ?? "";
if (isPreview) {
const mainHostname = hostname.replace(/^preview--/, "");
loginBaseUrl = `${window.location.protocol}//${mainHostname}${
window.location.port ? ":" + window.location.port : ""
}`;
}

// Build the login URL
const loginUrl = `${
options.appBaseUrl ?? ""
}/login?from_url=${encodeURIComponent(redirectUrl)}`;
const loginUrl = `${loginBaseUrl}/login?from_url=${encodeURIComponent(
redirectUrl
)}`;

// Redirect to the login page
window.location.href = loginUrl;
Expand Down
Loading