mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
* chore: dev -> dev/nextjs * chore: move to /examples * chore: move to playgrounds, add dev/sveltekit * Update sync.yml * chore: dev scripts
46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { AuthHandler, AuthOptions, Session } from "@auth/core"
|
|
import { fromNodeMiddleware, H3Event } from "h3"
|
|
import getURL from "requrl"
|
|
import { createMiddleware } from "@hattip/adapter-node"
|
|
|
|
export function NuxtAuthHandler(options: AuthOptions) {
|
|
async function handler(ctx: { request: Request }) {
|
|
options.trustHost ??= true
|
|
|
|
return AuthHandler(ctx.request, options)
|
|
}
|
|
|
|
const middleware = createMiddleware(handler)
|
|
|
|
return fromNodeMiddleware(middleware)
|
|
}
|
|
|
|
export async function getSession(
|
|
event: H3Event,
|
|
options: AuthOptions
|
|
): Promise<Session | null> {
|
|
options.trustHost ??= true
|
|
|
|
const headers = getRequestHeaders(event)
|
|
const nodeHeaders = new Headers()
|
|
|
|
const url = new URL("/api/auth/session", getURL(event.node.req))
|
|
|
|
Object.keys(headers).forEach((key) => {
|
|
nodeHeaders.append(key, headers[key] as any)
|
|
})
|
|
|
|
const response = await AuthHandler(
|
|
new Request(url, { headers: nodeHeaders }),
|
|
options
|
|
)
|
|
|
|
const { status = 200 } = response
|
|
|
|
const data = await response.json()
|
|
|
|
if (!data || !Object.keys(data).length) return null
|
|
if (status === 200) return data
|
|
throw new Error(data.message)
|
|
}
|