diff --git a/docs/nuxt-configuration.md b/docs/nuxt-configuration.md index d9dd42df..c76406c6 100644 --- a/docs/nuxt-configuration.md +++ b/docs/nuxt-configuration.md @@ -40,6 +40,8 @@ You *must* create a catch-all route at `server/api/auth/[...].ts`. ```ts import GithubProvider from "@auth/core/providers/github" import type { AuthConfig } from "@auth/core/types" +import type { H3Event } from "h3" +import { D1Adapter } from "@auth/d1-adapter" import { NuxtAuthHandler } from "#auth" // The #auth virtual import comes from this module. You can use it on the client @@ -59,6 +61,25 @@ export const authOptions: AuthConfig = { ] } +// authOptions can either be passed as an object like above, +// or as an async function which has the event as it's argument +// this is especially useful for database adapters that need access to the event object, +// such as D1Adapter +export async function authOptionsFunction(event: H3Event) { + const authOptions: AuthConfig = { + secret: runtimeConfig.authJs.secret, + providers: [ + GithubProvider({ + clientId: runtimeConfig.github.clientId, + clientSecret: runtimeConfig.github.clientSecret + }) + ], + adapter: D1Adapter(event.context.cloudflare.env.db) + } + + return authOptions +} + export default NuxtAuthHandler(authOptions, runtimeConfig) // If you don't want to pass the full runtime config, // you can pass something like this: { public: { authJs: { baseUrl: "" } } } diff --git a/packages/authjs-nuxt/src/runtime/lib/server.ts b/packages/authjs-nuxt/src/runtime/lib/server.ts index effe8544..ba19e1a9 100644 --- a/packages/authjs-nuxt/src/runtime/lib/server.ts +++ b/packages/authjs-nuxt/src/runtime/lib/server.ts @@ -5,6 +5,7 @@ import { eventHandler, getRequestHeaders, getRequestURL } from "h3" import type { AuthConfig, Session } from "@auth/core/types" import { getToken } from "@auth/core/jwt" import { checkOrigin, getAuthJsSecret, getRequestFromEvent, getServerOrigin, makeCookiesFromCookieString } from "../utils" +import type { AuthConfigFunction } from "./types" if (!globalThis.crypto) { // eslint-disable-next-line no-console @@ -21,12 +22,14 @@ if (!globalThis.crypto) { /** * This is the event handler for the catch-all route. * Everything can be customized by adding a custom route that takes priority over the handler. - * @param options AuthConfig + * @param options AuthConfig | AuthConfigFunction * @param runtimeConfig RuntimeConfig * @returns EventHandler */ -export function NuxtAuthHandler(options: AuthConfig, runtimeConfig: RuntimeConfig) { +export function NuxtAuthHandler(options: AuthConfig | AuthConfigFunction, runtimeConfig: RuntimeConfig) { return eventHandler(async (event) => { + if (typeof options === "function") options = await options(event) as AuthConfig + options.trustHost ??= true options.skipCSRFCheck = skipCSRFCheck const request = await getRequestFromEvent(event) diff --git a/packages/authjs-nuxt/src/runtime/lib/types.ts b/packages/authjs-nuxt/src/runtime/lib/types.ts index ef020a3c..fed64c1f 100644 --- a/packages/authjs-nuxt/src/runtime/lib/types.ts +++ b/packages/authjs-nuxt/src/runtime/lib/types.ts @@ -1,4 +1,6 @@ import type { BuiltInProviderType, ProviderType } from "@auth/core/providers" +import type { H3Event } from "h3" +import type { AuthConfig } from "@auth/core/types" /** * Util type that matches some strings literally, but allows any other string as well. @@ -44,3 +46,4 @@ export interface SignOutParams { /** [Documentation](https://next-auth.js.org/getting-started/client#using-the-redirect-false-option-1 */ redirect?: R } +export type AuthConfigFunction = (event: H3Event) => Promise