Skip to content

Commit

Permalink
feat: support custom redirect base url for nuxt sdk (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyijun authored Sep 18, 2024
1 parent 5f4de8c commit 71f3b53
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/good-knives-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@logto/nuxt": minor
---

support custom redirect base url settings for nuxt SDK
17 changes: 16 additions & 1 deletion packages/nuxt/src/runtime/server/event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default defineEventHandler(async (event) => {
pathnames,
postCallbackRedirectUri,
postLogoutRedirectUri,
customRedirectBaseUrl,
...clientConfig
} = logtoConfig;

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

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

/**
* This approach allows us to:
* 1. Override the base URL when necessary (e.g., in proxy environments)
* 2. Preserve the original path and query parameters
* 3. Fall back to the original URL when no custom base is provided
*
* It's particularly useful in scenarios where the application is deployed
* behind a reverse proxy or in environments that rewrite URLs.
*/
const url = customRedirectBaseUrl
? new URL(requestUrl.pathname + requestUrl.search + requestUrl.hash, customRedirectBaseUrl)
: requestUrl;

const storage = new CookieStorage({
cookieKey: cookieName,
encryptionKey: cookieEncryptionKey,
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/src/runtime/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export const defaults = Object.freeze({
appId: '<replace-with-logto-app-id>',
appSecret: '<replace-with-logto-app-secret>',
cookieEncryptionKey: '<replace-with-random-string>',
customRedirectBaseUrl: '<replace-with-custom-redirect-base-url>',
} as const satisfies LogtoRuntimeConfigInput);
15 changes: 15 additions & 0 deletions packages/nuxt/src/runtime/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ export type LogtoRuntimeConfig = LogtoModuleOptions & {
* The secret used to encrypt the Logto cookie. It should be a random string.
*/
cookieEncryptionKey: string;
/**
* Custom base URL for redirects
*
* This URL is used as the base for generating redirect and callback URLs.
* It's particularly useful in environments where the application is behind
* a proxy or where URLs are rewritten.
*
* If set, this value will be used instead of the automatically detected URL.
* If not set, the system will use the URL obtained from `getRequestURL`.
*
* Example: 'https://your-public-facing-domain.com'
*
* Note: Provide only the base URL without paths or query parameters.
*/
customRedirectBaseUrl: string;
} & Omit<LogtoConfig, 'appSecret'> &
Required<Pick<LogtoConfig, 'appSecret'>>;

Expand Down

1 comment on commit 71f3b53

@MagmaBlock
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a starter of Logto and trying to build a Nuxt app that connects to it. However, I found that I couldn't build the program according to the Logto Nuxt documentation. After some investigation, I discovered this commit, seems introduced an unexpected behavior. The customRedirectBaseUrl is only mentioned in code but not in the documentation, and this option is required. If no customRedirectBaseUrl is provided, the following error will be thrown:

 WARN  The following Logto configuration keys have default values: customRedirectBaseUrl. Please replace them with your own values.

 ERROR  [nuxt] [request error] [unhandled] [500] Invalid URL
  at new URL (node:internal/url:797:36)
  at Object.handler (C:\Projects\ria-workbench\node_modules\.pnpm\@logto+nuxt@1.0.0_magicast@0.3.5_rollup@4.21.2\node_modules\@logto\nuxt\dist\runtime\server\event-handler.mjs:29:39)

Please sign in to comment.