Skip to content

Commit 1fb081c

Browse files
authored
fix(composable): use same context for clear and fetch (#278)
Resolves #273
1 parent 33686af commit 1fb081c

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
import { useState, computed, useRequestFetch } from '#imports'
22
import type { UserSession, UserSessionComposable } from '#auth-utils'
33

4-
const useSessionState = () => useState<UserSession>('nuxt-session', () => ({}))
5-
const useAuthReadyState = () => useState('nuxt-auth-ready', () => false)
6-
74
/**
85
* Composable to get back the user session and utils around it.
96
* @see https://github.com/atinux/nuxt-auth-utils
107
*/
118
export function useUserSession(): UserSessionComposable {
12-
const sessionState = useSessionState()
13-
const authReadyState = useAuthReadyState()
9+
const sessionState = useState<UserSession>('nuxt-session', () => ({}))
10+
const authReadyState = useState('nuxt-auth-ready', () => false)
11+
12+
const clear = async () => {
13+
await $fetch('/api/_auth/session', { method: 'DELETE' })
14+
sessionState.value = {}
15+
}
16+
17+
const fetch = async () => {
18+
sessionState.value = await useRequestFetch()('/api/_auth/session', {
19+
headers: {
20+
Accept: 'text/json',
21+
},
22+
retry: false,
23+
}).catch(() => ({}))
24+
if (!authReadyState.value) {
25+
authReadyState.value = true
26+
}
27+
}
28+
1429
return {
1530
ready: computed(() => authReadyState.value),
1631
loggedIn: computed(() => Boolean(sessionState.value.user)),
@@ -20,21 +35,3 @@ export function useUserSession(): UserSessionComposable {
2035
clear,
2136
}
2237
}
23-
24-
async function fetch() {
25-
const authReadyState = useAuthReadyState()
26-
useSessionState().value = await useRequestFetch()('/api/_auth/session', {
27-
headers: {
28-
Accept: 'text/json',
29-
},
30-
retry: false,
31-
}).catch(() => ({}))
32-
if (!authReadyState.value) {
33-
authReadyState.value = true
34-
}
35-
}
36-
37-
async function clear() {
38-
await $fetch('/api/_auth/session', { method: 'DELETE' })
39-
useSessionState().value = {}
40-
}

0 commit comments

Comments
 (0)