Skip to content

Commit f278079

Browse files
authored
feat(next-auth): awaitable lazy init (#11364)
1 parent 1bd4dd8 commit f278079

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

packages/next-auth/src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ import { reqWithEnvURL, setEnvDefaults } from "./lib/env.js"
7272
import { initAuth } from "./lib/index.js"
7373
import { signIn, signOut, update } from "./lib/actions.js"
7474

75-
import type { Session } from "@auth/core/types"
75+
import type { Awaitable, Session } from "@auth/core/types"
7676
import type { BuiltInProviderType } from "@auth/core/providers"
7777
import type {
7878
GetServerSidePropsContext,
@@ -358,7 +358,7 @@ export interface NextAuthResult {
358358
* import NextAuth from "next-auth"
359359
* import GitHub from "@auth/core/providers/github"
360360
*
361-
* export const { handlers, auth } = NextAuth((req) => {
361+
* export const { handlers, auth } = NextAuth(async (req) => {
362362
* console.log(req) // do something with the request
363363
* return {
364364
* providers: [GitHub],
@@ -369,11 +369,11 @@ export interface NextAuthResult {
369369
export default function NextAuth(
370370
config:
371371
| NextAuthConfig
372-
| ((request: NextRequest | undefined) => NextAuthConfig)
372+
| ((request: NextRequest | undefined) => Awaitable<NextAuthConfig>)
373373
): NextAuthResult {
374374
if (typeof config === "function") {
375-
const httpHandler = (req: NextRequest) => {
376-
const _config = config(req)
375+
const httpHandler = async (req: NextRequest) => {
376+
const _config = await config(req)
377377
setEnvDefaults(_config)
378378
return Auth(reqWithEnvURL(req), _config)
379379
}
@@ -383,18 +383,18 @@ export default function NextAuth(
383383
// @ts-expect-error
384384
auth: initAuth(config, (c) => setEnvDefaults(c)),
385385

386-
signIn: (provider, options, authorizationParams) => {
387-
const _config = config(undefined)
386+
signIn: async (provider, options, authorizationParams) => {
387+
const _config = await config(undefined)
388388
setEnvDefaults(_config)
389389
return signIn(provider, options, authorizationParams, _config)
390390
},
391-
signOut: (options) => {
392-
const _config = config(undefined)
391+
signOut: async (options) => {
392+
const _config = await config(undefined)
393393
setEnvDefaults(_config)
394394
return signOut(options, _config)
395395
},
396-
unstable_update: (data) => {
397-
const _config = config(undefined)
396+
unstable_update: async (data) => {
397+
const _config = await config(undefined)
398398
setEnvDefaults(_config)
399399
return update(data, _config)
400400
},

packages/next-auth/src/lib/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ function isReqWrapper(arg: any): arg is NextAuthMiddleware | AppRouteHandlerFn {
121121
export function initAuth(
122122
config:
123123
| NextAuthConfig
124-
| ((request: NextRequest | undefined) => NextAuthConfig),
124+
| ((request: NextRequest | undefined) => Awaitable<NextAuthConfig>),
125125
onLazyLoad?: (config: NextAuthConfig) => void // To set the default env vars
126126
) {
127127
if (typeof config === "function") {
128-
return (...args: WithAuthArgs) => {
128+
return async (...args: WithAuthArgs) => {
129129
if (!args.length) {
130130
// React Server Components
131131
const _headers = headers()
132-
const _config = config(undefined) // Review: Should we pass headers() here instead?
132+
const _config = await config(undefined) // Review: Should we pass headers() here instead?
133133
onLazyLoad?.(_config)
134134

135135
return getSession(_headers, _config).then((r) => r.json())
@@ -140,7 +140,7 @@ export function initAuth(
140140
// export { auth as default } from "auth"
141141
const req = args[0]
142142
const ev = args[1]
143-
const _config = config(req)
143+
const _config = await config(req)
144144
onLazyLoad?.(_config)
145145

146146
// args[0] is supposed to be NextRequest but the instanceof check is failing.
@@ -155,7 +155,7 @@ export function initAuth(
155155
return async (
156156
...args: Parameters<NextAuthMiddleware | AppRouteHandlerFn>
157157
) => {
158-
const _config = config(args[0])
158+
const _config = await config(args[0])
159159
onLazyLoad?.(_config)
160160
return handleAuth(args, _config, userMiddlewareOrRoute)
161161
}
@@ -164,7 +164,7 @@ export function initAuth(
164164
const request = "req" in args[0] ? args[0].req : args[0]
165165
const response: any = "res" in args[0] ? args[0].res : args[1]
166166
// @ts-expect-error -- request is NextRequest
167-
const _config = config(request)
167+
const _config = await config(request)
168168
onLazyLoad?.(_config)
169169

170170
// @ts-expect-error -- request is NextRequest

0 commit comments

Comments
 (0)