Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/nuxt-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: "" } } }
Expand Down
7 changes: 5 additions & 2 deletions packages/authjs-nuxt/src/runtime/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions packages/authjs-nuxt/src/runtime/lib/types.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -44,3 +46,4 @@ export interface SignOutParams<R extends boolean = true> {
/** [Documentation](https://next-auth.js.org/getting-started/client#using-the-redirect-false-option-1 */
redirect?: R
}
export type AuthConfigFunction = (event: H3Event) => Promise<AuthConfig>