200200/// <reference types="@sveltejs/kit" />
201201import type { Handle , RequestEvent } from "@sveltejs/kit"
202202
203- import { dev } from "$app/environment"
203+ import { dev , building } from "$app/environment"
204204import { base } from "$app/paths"
205205import { env } from "$env/dynamic/private"
206206
@@ -219,7 +219,7 @@ export async function getSession(
219219 req : Request ,
220220 config : SvelteKitAuthConfig
221221) : ReturnType < App . Locals [ "getSession" ] > {
222- config . secret ??= env . AUTH_SECRET
222+ setEnvDefaults ( env , config )
223223 config . trustHost ??= true
224224
225225 const prefix = config . prefix ?? `${ base } /auth`
@@ -258,31 +258,23 @@ const actions: AuthAction[] = [
258258 "error" ,
259259]
260260
261- type DynamicSvelteKitAuthConfig = (
262- event : RequestEvent
263- ) => PromiseLike < SvelteKitAuthConfig >
264-
265261/**
266262 * The main entry point to `@auth/sveltekit`
267263 * @see https://sveltekit.authjs.dev
268264 */
269265export function SvelteKitAuth (
270- svelteKitAuthOptions : SvelteKitAuthConfig | DynamicSvelteKitAuthConfig
266+ config :
267+ | SvelteKitAuthConfig
268+ | ( ( event : RequestEvent ) => PromiseLike < SvelteKitAuthConfig > )
271269) : Handle {
272270 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
282274
283275 const { url, request } = event
284276
285- event . locals . getSession ??= ( ) => getSession ( request , authOptions )
277+ event . locals . getSession ??= ( ) => getSession ( request , _config )
286278
287279 const action = url . pathname
288280 . slice ( prefix . length + 1 )
@@ -292,7 +284,7 @@ export function SvelteKitAuth(
292284 return resolve ( event )
293285 }
294286
295- return Auth ( request , authOptions )
287+ return Auth ( request , _config )
296288 }
297289}
298290
@@ -313,3 +305,30 @@ declare module "$env/dynamic/private" {
313305 export const AUTH_TRUST_HOST : string
314306 export const VERCEL : string
315307}
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