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
5 changes: 5 additions & 0 deletions .changeset/brown-dancers-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"enspire": patch
---

Skip auth middleware on initial client load
19 changes: 15 additions & 4 deletions app/middleware/auth.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { until } from '@vueuse/core'
import { useClerk, useClerkProvider } from 'vue-clerk'
import { clerkClient } from 'h3-clerk'

export default defineNuxtRouteMiddleware(async () => {
const nuxtApp = useNuxtApp()
const clerk = useClerk()
const { isClerkLoaded } = useClerkProvider()

// On server, check if the user isn't authenticated
// On server, check if the user isn't authenticated and if the user is binded
// and redirect to /sign-in.
if (process.server && !nuxtApp.ssrContext?.event.context.auth?.userId)
return navigateTo('/sign-in')
if (import.meta.server) {
const userId = nuxtApp.ssrContext?.event.context.auth?.userId
if (!userId) {
return navigateTo('/sign-in')
}
if (!(await clerkClient.users.getUser(userId)).publicMetadata.binded) {
return navigateTo('/account/bind')
}
}

// On client, check if clerk is loaded and if user isn't authenticated and if user is binded to a TSIMS account
// and redirect to /sign-in.
if (process.client) {
if (import.meta.client) {
// Skip on initial client load because server must have loaded in this situation
if (nuxtApp.isHydrating && nuxtApp.payload.serverRendered) return

await until(isClerkLoaded).toBe(true)
if (clerk.loaded && !clerk.user?.id)
return navigateTo('/sign-in')
Expand Down
4 changes: 2 additions & 2 deletions app/middleware/authWithoutBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export default defineNuxtRouteMiddleware(() => {

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

// On client, check if clerk is loaded and if user isn't authenticated
// and redirect to /sign-in.
if (process.client && clerk.loaded && !clerk.user?.id)
if (import.meta.client && clerk.loaded && !clerk.user?.id)
return navigateTo('/sign-in')
})
4 changes: 2 additions & 2 deletions app/middleware/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ export default defineNuxtRouteMiddleware(() => {
// On server, check if the user is authenticated
// and redirect to /profile.
if (
process.server
import.meta.server
&& nuxtApp.ssrContext?.event.context.auth?.userId
) {
return navigateTo('/profile')
}

// On client, check if clerk is loaded and if user is authenticated
// and redirect to /profile.
if (process.client && clerk.loaded && clerk.user?.id)
if (import.meta.client && clerk.loaded && clerk.user?.id)
return navigateTo('/profile')
})
Loading