-
- Notifications
You must be signed in to change notification settings - Fork 186
fix(#1042,#1052): handle value returned by navigateToAuthPage #1057
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { hasProtocol, isScriptProtocol } from 'ufo' | ||
| import { abortNavigation, callWithNuxt, useRouter } from '#app' | ||
| import { callWithNuxt, useRouter } from '#app' | ||
| import type { NuxtApp } from '#app' | ||
| | ||
| export function navigateToAuthPageWN(nuxt: NuxtApp, href: string, isInternalRouting?: boolean) { | ||
| | @@ -16,16 +16,19 @@ const URL_QUOTE_RE = /"/g | |
| * manually set `window.location.href` on the client **and then fake return a Promise that does not immediately resolve to block navigation (although it will not actually be fully awaited, but just be awaited long enough for the naviation to complete)**. | ||
| * 2. Additionally on the server-side, we cannot use `navigateTo(signInUrl)` as this uses `vue-router` internally which does not know the "external" sign-in page of next-auth and thus will log a warning which we want to avoid. | ||
| * | ||
| * Adapted from https://github.com/nuxt/nuxt/blob/16d213bbdcc69c0cc72afb355755ff877654a374/packages/nuxt/src/app/composables/router.ts#L119-L217 | ||
| * Adapted from https://github.com/nuxt/nuxt/blob/dc69e26c5b9adebab3bf4e39417288718b8ddf07/packages/nuxt/src/app/composables/router.ts#L130-L247 | ||
| * | ||
| * @param nuxt Nuxt app context | ||
| * @param nuxtApp Nuxt app context | ||
| * @param href HREF / URL to navigate to | ||
| */ | ||
| function navigateToAuthPage(nuxt: NuxtApp, href: string, isInternalRouting = false) { | ||
| function navigateToAuthPage(nuxtApp: NuxtApp, href: string, isInternalRouting = false) { | ||
| const router = useRouter() | ||
| | ||
| // https://github.com/nuxt/nuxt/blob/dc69e26c5b9adebab3bf4e39417288718b8ddf07/packages/nuxt/src/app/composables/router.ts#L84-L93 | ||
| const inMiddleware = Boolean(nuxtApp._processingMiddleware) | ||
| | ||
| if (import.meta.server) { | ||
| if (nuxt.ssrContext) { | ||
| if (nuxtApp.ssrContext) { | ||
| const isExternalHost = hasProtocol(href, { acceptRelative: true }) | ||
| if (isExternalHost) { | ||
| const { protocol } = new URL(href, 'http://localhost') | ||
| | @@ -38,17 +41,31 @@ function navigateToAuthPage(nuxt: NuxtApp, href: string, isInternalRouting = fal | |
| // We also skip resolution for internal routing to avoid triggering `No match found` warning from Vue Router | ||
| const location = isExternalHost || isInternalRouting ? href : router.resolve(href).fullPath || '/' | ||
| | ||
| // TODO: consider deprecating in favour of `app:rendered` and removing | ||
| return nuxt.callHook('app:redirected').then(() => { | ||
| async function redirect(response: false | undefined) { | ||
| // TODO: consider deprecating in favour of `app:rendered` and removing | ||
| await nuxtApp.callHook('app:redirected') | ||
| const encodedLoc = location.replace(URL_QUOTE_RE, '%22') | ||
| const encodedHeader = encodeURL(location, isExternalHost) | ||
| nuxt.ssrContext!._renderResponse = { | ||
| | ||
| nuxtApp.ssrContext!._renderResponse = { | ||
| statusCode: 302, | ||
| body: `<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=${encodedLoc}"></head></html>`, | ||
| headers: { location: encodedHeader }, | ||
| } | ||
| abortNavigation() | ||
| }) | ||
| return response | ||
| } | ||
| | ||
| // We wait to perform the redirect last in case any other middleware will intercept the redirect | ||
| // and redirect somewhere else instead. | ||
| if (!isExternalHost && inMiddleware) { | ||
| // For an unknown reason, `final.fullPath` received here is not percent-encoded, leading to the check always failing. | ||
| // To preserve compatibility with NuxtAuth < 1.0, we simply return `undefined`. | ||
| // TODO: Find the reason or report the issue to Nuxt if `navigateTo` has the same problem (`router.resolve` handles the `%2F` in callback URL correctly) | ||
| // router.afterEach(final => final.fullPath === location ? redirect(false) : undefined) | ||
| // return href | ||
| Comment on lines +64 to +65 Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this code leftover/dead? it doesnt seem to belong to the comment above? Collaborator Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, this code was commented exactly due to the explanation above Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didnt get that, a colon would be helpful! But alas: definitely a nit, wont block the merge for this (: Collaborator Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI: The problem is also present in Nuxt's Their implementation gets the same problem: Collaborator Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reproduction repo: https://github.com/phoenix-ru/nuxt-navigate-to-fullpath-reproduction Issue will be created shortly Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please link it here as well! Collaborator Author There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue created: nuxt/nuxt#33273 | ||
| return redirect(undefined) | ||
| } | ||
| return redirect(!inMiddleware ? undefined : /* abort further route navigation */ false) | ||
| } | ||
| } | ||
| | ||
| | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a. Any chance we can make this type more speaking? eg making this a string-union with speaking members for different known results?
b. can/should we document this somewhere? either inside the docs or as a docstring in-code that explains the different variants and what this key means?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a. Not really, as this is a value returned by either
navigateToAuthPageWNorvue-router. I would prefer to modify this value as little as possible, since we need to immediately pass it back tovue-router.b. I can add a doctext explaining what this value is