Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allowing the base url to be overridden for the nuxt package #764

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const logtoModule: NuxtModule<LogtoRuntimeConfigInput> = defineNuxtModule<LogtoR
signOut: '/sign-out',
callback: '/callback',
},
baseUrl: undefined,
} satisfies LogtoRuntimeConfigInput
);

Expand Down
17 changes: 12 additions & 5 deletions packages/nuxt/src/runtime/server/event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default defineEventHandler(async (event) => {
pathnames,
postCallbackRedirectUri,
postLogoutRedirectUri,
baseUrl,
...clientConfig
} = logtoConfig;

Expand All @@ -35,7 +36,11 @@ export default defineEventHandler(async (event) => {
);
}

const url = getRequestURL(event);
const requestUrl = getRequestURL(event);

// Use the baseUrl if provided, otherwise use the request URL
const url = baseUrl ? new URL(baseUrl) : requestUrl;

const storage = new CookieStorage(
{
cookieKey: cookieName,
Expand All @@ -57,18 +62,20 @@ export default defineEventHandler(async (event) => {
storage,
});

if (url.pathname === pathnames.signIn) {
if (requestUrl.pathname === pathnames.signIn) {
await logto.signIn(new URL(pathnames.callback, url).href);
return;
}

if (url.pathname === pathnames.signOut) {
if (requestUrl.pathname === pathnames.signOut) {
await logto.signOut(new URL(postLogoutRedirectUri, url).href);
return;
}

if (url.pathname === pathnames.callback) {
await logto.handleSignInCallback(url.href);
if (requestUrl.pathname === pathnames.callback) {
// Use the baseUrl for the callback, but keep the original query parameters
const callbackUrl = new URL(requestUrl.pathname + requestUrl.search, url);
await logto.handleSignInCallback(callbackUrl.href);
await sendRedirect(event, postCallbackRedirectUri, 302);
return;
}
Expand Down
5 changes: 5 additions & 0 deletions packages/nuxt/src/runtime/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ type LogtoModuleOptions = {
*/
callback: string;
};
/**
* Optional override for the base URL used in redirect URIs.
* If not provided, the request URL will be used.
*/
baseUrl?: string;
};

/** The full runtime configuration for the Logto module. */
Expand Down