Skip to content

Commit

Permalink
Merge branch 'main' into docs/migrate-to-vitepress
Browse files Browse the repository at this point in the history
  • Loading branch information
zoey-kaiser authored Jul 4, 2024
2 parents 508f40c + b2ad71a commit d34fd2d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sidebase/nuxt-auth",
"version": "0.8.0-rc.1",
"version": "0.8.0",
"license": "MIT",
"type": "module",
"exports": {
Expand Down
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

0 comments on commit d34fd2d

Please sign in to comment.