Skip to content

Commit 0b8ee4a

Browse files
authored
fix: correctly add base path to host, refactor detectHost to work better with h3 (#156)
* fix: also add base path to host detection to fix credentials form action * fix: also add base path to host detection to fix credentials form action
1 parent 86c5677 commit 0b8ee4a

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/runtime/server/services/nuxtAuthHandler.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,23 @@ export const getServerOrigin = (event?: H3Event): string => {
8686

8787
/** Extract the host from the environment */
8888
const detectHost = (
89-
trusted: boolean,
90-
forwardedValue: string | string[] | undefined | null,
91-
defaultValue: string | false
89+
event: H3Event,
90+
{ trusted, basePath }: { trusted: boolean, basePath: string }
9291
): string | undefined => {
93-
if (trusted && forwardedValue) {
94-
return Array.isArray(forwardedValue) ? forwardedValue[0] : forwardedValue
92+
if (trusted) {
93+
const forwardedValue = getURL(event.node.req)
94+
if (forwardedValue) {
95+
return Array.isArray(forwardedValue) ? forwardedValue[0] : forwardedValue
96+
}
9597
}
9698

97-
return defaultValue || undefined
99+
let origin
100+
try {
101+
origin = getServerOrigin(event)
102+
} catch (error) {
103+
return undefined
104+
}
105+
return joinURL(origin, basePath)
98106
}
99107

100108
/** Setup the nuxt (next) auth event handler, based on the passed in options */
@@ -130,13 +138,7 @@ export const NuxtAuthHandler = (nuxtAuthOptions?: NextAuthOptions) => {
130138
*/
131139
const getInternalNextAuthRequestData = async (event: H3Event): Promise<RequestInternal> => {
132140
const nextRequest: Omit<RequestInternal, 'action'> = {
133-
host: detectHost(
134-
options.trustHost,
135-
// Forwarded host
136-
getURL(event.node.req),
137-
// Origin
138-
getServerOrigin(event)
139-
),
141+
host: detectHost(event, { trusted: useRuntimeConfig().auth.trustHost, basePath: useRuntimeConfig().auth.basePath }),
140142
body: undefined,
141143
cookies: parseCookies(event),
142144
query: undefined,

0 commit comments

Comments
 (0)