200
200
/// <reference types="@sveltejs/kit" />
201
201
import type { Handle , RequestEvent } from "@sveltejs/kit"
202
202
203
- import { dev } from "$app/environment"
203
+ import { dev , building } from "$app/environment"
204
204
import { base } from "$app/paths"
205
205
import { env } from "$env/dynamic/private"
206
206
@@ -219,7 +219,7 @@ export async function getSession(
219
219
req : Request ,
220
220
config : SvelteKitAuthConfig
221
221
) : ReturnType < App . Locals [ "getSession" ] > {
222
- config . secret ??= env . AUTH_SECRET
222
+ setEnvDefaults ( env , config )
223
223
config . trustHost ??= true
224
224
225
225
const prefix = config . prefix ?? `${ base } /auth`
@@ -258,31 +258,23 @@ const actions: AuthAction[] = [
258
258
"error" ,
259
259
]
260
260
261
- type DynamicSvelteKitAuthConfig = (
262
- event : RequestEvent
263
- ) => PromiseLike < SvelteKitAuthConfig >
264
-
265
261
/**
266
262
* The main entry point to `@auth/sveltekit`
267
263
* @see https://sveltekit.authjs.dev
268
264
*/
269
265
export function SvelteKitAuth (
270
- svelteKitAuthOptions : SvelteKitAuthConfig | DynamicSvelteKitAuthConfig
266
+ config :
267
+ | SvelteKitAuthConfig
268
+ | ( ( event : RequestEvent ) => PromiseLike < SvelteKitAuthConfig > )
271
269
) : Handle {
272
270
return async function ( { event, resolve } ) {
273
- const authOptions =
274
- typeof svelteKitAuthOptions === "object"
275
- ? svelteKitAuthOptions
276
- : await svelteKitAuthOptions ( event )
277
- const { prefix = `${ base } /auth` } = authOptions
278
-
279
- authOptions . secret ??= env . AUTH_SECRET
280
- authOptions . redirectProxyUrl ??= env . AUTH_REDIRECT_PROXY_URL
281
- authOptions . trustHost ??= ! ! ( env . AUTH_TRUST_HOST ?? env . VERCEL ?? dev )
271
+ const _config = typeof config === "object" ? config : await config ( event )
272
+ setEnvDefaults ( env , _config )
273
+ const { prefix = `${ base } /auth` } = _config
282
274
283
275
const { url, request } = event
284
276
285
- event . locals . getSession ??= ( ) => getSession ( request , authOptions )
277
+ event . locals . getSession ??= ( ) => getSession ( request , _config )
286
278
287
279
const action = url . pathname
288
280
. slice ( prefix . length + 1 )
@@ -292,7 +284,7 @@ export function SvelteKitAuth(
292
284
return resolve ( event )
293
285
}
294
286
295
- return Auth ( request , authOptions )
287
+ return Auth ( request , _config )
296
288
}
297
289
}
298
290
@@ -313,3 +305,30 @@ declare module "$env/dynamic/private" {
313
305
export const AUTH_TRUST_HOST : string
314
306
export const VERCEL : string
315
307
}
308
+
309
+ export function setEnvDefaults ( envObject : any , config : SvelteKitAuthConfig ) {
310
+ if ( building ) return
311
+
312
+ config . redirectProxyUrl ??= env . AUTH_REDIRECT_PROXY_URL
313
+ config . secret ??= env . AUTH_SECRET
314
+ config . trustHost ??= ! ! (
315
+ env . AUTH_URL ??
316
+ env . NEXTAUTH_URL ??
317
+ env . AUTH_TRUST_HOST ??
318
+ env . VERCEL ??
319
+ env . NODE_ENV !== "production" ??
320
+ dev
321
+ )
322
+ config . providers = config . providers . map ( ( p ) => {
323
+ const finalProvider = typeof p === "function" ? p ( { } ) : p
324
+ if ( finalProvider . type === "oauth" || finalProvider . type === "oidc" ) {
325
+ const ID = finalProvider . id . toUpperCase ( )
326
+ finalProvider . clientId ??= envObject [ `AUTH_${ ID } _ID` ]
327
+ finalProvider . clientSecret ??= envObject [ `AUTH_${ ID } _SECRET` ]
328
+ if ( finalProvider . type === "oidc" ) {
329
+ finalProvider . issuer ??= envObject [ `AUTH_${ ID } _ISSUER` ]
330
+ }
331
+ }
332
+ return finalProvider
333
+ } )
334
+ }
0 commit comments