Skip to content

Commit 7c5bee6

Browse files
committed
fix(auth): try to fix updateIdentity
1 parent c951440 commit 7c5bee6

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

src/runtime/plugins/edgedb-auth.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,37 @@ export default defineNuxtPlugin(async (nuxtApp) => {
88

99
const cookie = useCookie('edgedb-auth-token')
1010

11-
const updateIdentity = async (event?: H3Event) => {
12-
if (!process.server) {
13-
identity.value = await $fetch('/api/auth/identity')
14-
return
15-
}
16-
17-
if (!cookie.value)
18-
return
11+
const isLoggedIn = computed(() => !!((identity as Ref<User>)?.value))
1912

20-
const req = getRequestURL(event)
21-
const url = `${req.protocol}//${req.host}/api/auth/identity`
22-
identity.value = await fetchWithEvent(event, url).then(r => r.json())
13+
async function updateIdentity(event?: H3Event) {
14+
try {
15+
if (!process.server) {
16+
identity.value = await $fetch('/api/auth/identity')
17+
return
18+
}
19+
20+
const req = getRequestURL(event)
21+
const url = `${req.protocol}//${req.host}/api/auth/identity`
22+
23+
const idRequest = await fetchWithEvent(event, url).then(r => r.json())
24+
25+
if (identity) {
26+
identity.value = idRequest
27+
}
28+
else {
29+
identity.value = undefined
30+
await logout()
31+
}
32+
}
33+
catch (_) {
34+
//
35+
}
2336
}
2437

25-
const isLoggedIn = computed(() => !!((identity as Ref<User>)?.value))
26-
27-
const logout = async (redirectTo: string) => {
38+
async function logout(redirectTo: string) {
2839
await $fetch('/api/auth/logout')
2940
identity.value = undefined
41+
cookie.value = ''
3042
if (redirectTo)
3143
await navigateTo(redirectTo)
3244
}

0 commit comments

Comments
 (0)