Skip to content

Commit fc40f32

Browse files
authored
fix(auth): skip auth middleware on initial client load (#679)
* fix(auth): skip auth middleware on initial client load * changeset
1 parent 7c64ea0 commit fc40f32

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

.changeset/brown-dancers-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"enspire": patch
3+
---
4+
5+
Skip auth middleware on initial client load

app/middleware/auth.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import { until } from '@vueuse/core'
22
import { useClerk, useClerkProvider } from 'vue-clerk'
3+
import { clerkClient } from 'h3-clerk'
34

45
export default defineNuxtRouteMiddleware(async () => {
56
const nuxtApp = useNuxtApp()
67
const clerk = useClerk()
78
const { isClerkLoaded } = useClerkProvider()
89

9-
// On server, check if the user isn't authenticated
10+
// On server, check if the user isn't authenticated and if the user is binded
1011
// and redirect to /sign-in.
11-
if (process.server && !nuxtApp.ssrContext?.event.context.auth?.userId)
12-
return navigateTo('/sign-in')
12+
if (import.meta.server) {
13+
const userId = nuxtApp.ssrContext?.event.context.auth?.userId
14+
if (!userId) {
15+
return navigateTo('/sign-in')
16+
}
17+
if (!(await clerkClient.users.getUser(userId)).publicMetadata.binded) {
18+
return navigateTo('/account/bind')
19+
}
20+
}
1321

1422
// On client, check if clerk is loaded and if user isn't authenticated and if user is binded to a TSIMS account
1523
// and redirect to /sign-in.
16-
if (process.client) {
24+
if (import.meta.client) {
25+
// Skip on initial client load because server must have loaded in this situation
26+
if (nuxtApp.isHydrating && nuxtApp.payload.serverRendered) return
27+
1728
await until(isClerkLoaded).toBe(true)
1829
if (clerk.loaded && !clerk.user?.id)
1930
return navigateTo('/sign-in')

app/middleware/authWithoutBind.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export default defineNuxtRouteMiddleware(() => {
66

77
// On server, check if the user isn't authenticated
88
// and redirect to /sign-in.
9-
if (process.server && !nuxtApp.ssrContext?.event.context.auth?.userId)
9+
if (import.meta.server && !nuxtApp.ssrContext?.event.context.auth?.userId)
1010
return navigateTo('/sign-in')
1111

1212
// On client, check if clerk is loaded and if user isn't authenticated
1313
// and redirect to /sign-in.
14-
if (process.client && clerk.loaded && !clerk.user?.id)
14+
if (import.meta.client && clerk.loaded && !clerk.user?.id)
1515
return navigateTo('/sign-in')
1616
})

app/middleware/public.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ export default defineNuxtRouteMiddleware(() => {
77
// On server, check if the user is authenticated
88
// and redirect to /profile.
99
if (
10-
process.server
10+
import.meta.server
1111
&& nuxtApp.ssrContext?.event.context.auth?.userId
1212
) {
1313
return navigateTo('/profile')
1414
}
1515

1616
// On client, check if clerk is loaded and if user is authenticated
1717
// and redirect to /profile.
18-
if (process.client && clerk.loaded && clerk.user?.id)
18+
if (import.meta.client && clerk.loaded && clerk.user?.id)
1919
return navigateTo('/profile')
2020
})

0 commit comments

Comments
 (0)