diff --git a/src/runtime/composables/local/useAuth.ts b/src/runtime/composables/local/useAuth.ts index fdbd59bd..385d59b5 100644 --- a/src/runtime/composables/local/useAuth.ts +++ b/src/runtime/composables/local/useAuth.ts @@ -43,13 +43,30 @@ const signIn: SignInFunc = async (credentials, signInOptions, } } + +const addHeaders = (token: string | null, config: ReturnType) => { + if (!(token && config.token.headerName)) { + return undefined + } + + if (config.token.type.length > 0) { + switch (config.token.type) { + case 'Cookie': + return { [config.token.headerName]: `${config.token.name}=${token}` } + case 'Bearer': + default: + return { [config.token.headerName]: `${config.token.type} ${token}`} + } + } +} + const signOut: SignOutFunc = async (signOutOptions) => { const nuxt = useNuxtApp() const runtimeConfig = await callWithNuxt(nuxt, useRuntimeConfig) const config = useTypedBackendConfig(runtimeConfig, 'local') const { data, rawToken, token } = await callWithNuxt(nuxt, useAuthState) - const headers = new Headers(config.token.headerName ? { [config.token.headerName]: token.value } as HeadersInit : undefined) + const headers = new Headers(addHeaders(token.value, config)) data.value = null rawToken.value = null @@ -80,7 +97,7 @@ const getSession: GetSessionFunc = async (getSessionO return } - const headers = new Headers(token.value && config.token.headerName ? { [config.token.headerName]: token.value } as HeadersInit : undefined) + const headers = new Headers(addHeaders(token.value, config)) loading.value = true try { diff --git a/src/runtime/composables/local/useAuthState.ts b/src/runtime/composables/local/useAuthState.ts index 528e618c..18e4ddb7 100644 --- a/src/runtime/composables/local/useAuthState.ts +++ b/src/runtime/composables/local/useAuthState.ts @@ -29,15 +29,6 @@ export const useAuthState = (): UseAuthStateReturn => { return null } - if (config.token.type.length > 0) { - switch (config.token.type) { - case 'Cookie': - return `${config.token.name}=${rawToken.value}` - case 'Bearer': - default: - return `${config.token.type} ${rawToken.value}` - } - } return rawToken.value })