Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 17 additions & 20 deletions src/runtime/composables/local/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const getSession: GetSessionFunc<SessionData | null | void> = async (getSessionO
const nuxt = useNuxtApp()

const config = useTypedBackendConfig(useRuntimeConfig(), 'local')
const getSessionConfig = config.endpoints.getSession
const { path, method } = config.endpoints.getSession
const { data, loading, lastRefreshedAt, rawToken, token: tokenState, _internal } = useAuthState()

let token = tokenState.value
Expand All @@ -88,27 +88,24 @@ const getSession: GetSessionFunc<SessionData | null | void> = async (getSessionO
return
}

if (getSessionConfig) {
const headers = new Headers(token ? { [config.token.headerName]: token } as HeadersInit : undefined)
const { path, method } = getSessionConfig

loading.value = true
try {
const result = await _fetch<any>(nuxt, path, { method, headers })
const { dataResponsePointer: sessionDataResponsePointer } = config.session
data.value = jsonPointerGet<SessionData>(result, sessionDataResponsePointer)
} catch (err) {
if (!data.value && err instanceof Error) {
console.error(`Session: unable to extract session, ${err.message}`)
}

// Clear all data: Request failed so we must not be authenticated
data.value = null
rawToken.value = null
const headers = new Headers(token ? { [config.token.headerName]: token } as HeadersInit : undefined)

loading.value = true
try {
const result = await _fetch<any>(nuxt, path, { method, headers })
const { dataResponsePointer: sessionDataResponsePointer } = config.session
data.value = jsonPointerGet<SessionData>(result, sessionDataResponsePointer)
} catch (err) {
if (!data.value && err instanceof Error) {
console.error(`Session: unable to extract session, ${err.message}`)
}
loading.value = false
lastRefreshedAt.value = new Date()

// Clear all data: Request failed so we must not be authenticated
data.value = null
rawToken.value = null
}
loading.value = false
lastRefreshedAt.value = new Date()

const { required = false, callbackUrl, onUnauthenticated, external } = getSessionOptions ?? {}
if (required && data.value === null) {
Expand Down
4 changes: 1 addition & 3 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@ export type ProviderLocal = {
signUp?: { path?: string; method?: RouterMethod };
/**
* What method and path to call to fetch user / session data from. `nuxt-auth` will send the token received upon sign-in as a header along this request to authenticate.
* Set to false to disable.
*
* Refer to the `token` configuration to configure how `nuxt-auth` uses the token in this request. By default it will be send as a bearer-authentication header like so: `Authentication: Bearer eyNDSNJDASNMDSA....`
*
* @default { path: '/session', method: 'get' }
* @example { path: '/user', method: 'get' }
* @example false
*/
getSession?: { path?: string; method?: RouterMethod } | false;
getSession?: { path?: string; method?: RouterMethod };
};
/**
* Pages that `nuxt-auth` needs to know the location off for redirects.
Expand Down