Skip to content

Optional getSessionHeaders parameter to getSession function of useAuth composable #972

@root5427

Description

@root5427

Describe the feature

Feature

It would be great if there will be an optional parameter getSessionHeaders (and maybe getSessionParams) in getSession funciton of useAuth composable for local provider.

Why?

In some cases a developer needs to pass a parameter (or multiple) to the custom authentication backend to get authenticated session data.

It is done properly in signIn function of useAuth composable in local provider:

const signIn: SignInFunc<Credentials, any> = async (credentials, signInOptions, signInParams, signInHeaders) => {
const nuxt = useNuxtApp()
const runtimeConfig = useRuntimeConfig()
const config = useTypedBackendConfig(runtimeConfig, 'local')
const { path, method } = config.endpoints.signIn
const response = await _fetch<Record<string, any>>(nuxt, path, {
method,
body: credentials,
params: signInParams ?? {},
headers: signInHeaders ?? {}
})
const { rawToken, rawRefreshToken } = useAuthState()
// Extract the access token
const extractedToken = jsonPointerGet(response, config.token.signInResponseTokenPointer)
if (typeof extractedToken !== 'string') {
console.error(
`Auth: string token expected, received instead: ${JSON.stringify(extractedToken)}. `
+ `Tried to find token at ${config.token.signInResponseTokenPointer} in ${JSON.stringify(response)}`
)
return
}
rawToken.value = extractedToken
// Extract the refresh token if enabled
if (config.refresh.isEnabled) {
const refreshTokenPointer = config.refresh.token.signInResponseRefreshTokenPointer
const extractedRefreshToken = jsonPointerGet(response, refreshTokenPointer)
if (typeof extractedRefreshToken !== 'string') {
console.error(
`Auth: string token expected, received instead: ${JSON.stringify(extractedRefreshToken)}. `
+ `Tried to find refresh token at ${refreshTokenPointer} in ${JSON.stringify(response)}`
)
return
}
rawRefreshToken.value = extractedRefreshToken
}
const { redirect = true, external, callGetSession = true } = signInOptions ?? {}
if (callGetSession) {
await nextTick(getSession)
}
let { callbackUrl } = signInOptions ?? {}
if (typeof callbackUrl === 'undefined') {
const redirectQueryParam = useRoute()?.query?.redirect
if (redirectQueryParam) {
callbackUrl = redirectQueryParam.toString()
}
else {
callbackUrl = await determineCallbackUrl(runtimeConfig.public.auth, () => getRequestURLWN(nuxt))
}
}
if (redirect) {
return navigateTo(callbackUrl, { external })
}
}

Example case when it is needed

I am currently using supabase as an auth backend, and for all the API requests it is required to pass apikey header.

How would you implement this?

No response

Additional information

  • Would you be willing to help implement this feature?

Provider

  • AuthJS
  • Local
  • Refresh
  • New Provider

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementAn improvement that needs to be addedprovider-localAn issue with the local provider

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions