[GH-ISSUE #211] Protect routes via middleware in Nuxt #8173

Closed
opened 2026-04-13 03:16:22 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @jonmh85 on GitHub (Oct 17, 2024).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/211

How can I use better-auth to protect routes with middleware in Nuxt?
I can see that middleware is in the Nextjs example but I cant see the equivalent for Nuxt.

Thanks

Originally created by @jonmh85 on GitHub (Oct 17, 2024). Original GitHub issue: https://github.com/better-auth/better-auth/issues/211 How can I use better-auth to protect routes with middleware in Nuxt? I can see that middleware is in the Nextjs example but I cant see the equivalent for Nuxt. Thanks
GiteaMirror added the locked label 2026-04-13 03:16:22 -05:00
Author
Owner

@kikiimdev commented on GitHub (Oct 18, 2024):

Something like this

export default defineNuxtRouteMiddleware(async () => {
  const { data: session } = await authClient.useSession(useFetch)

  if (!session.value) {
    return navigateTo("/auth")
  }
})
<!-- gh-comment-id:2421543715 --> @kikiimdev commented on GitHub (Oct 18, 2024): Something like this ``` ts export default defineNuxtRouteMiddleware(async () => { const { data: session } = await authClient.useSession(useFetch) if (!session.value) { return navigateTo("/auth") } }) ```
Author
Owner

@Bekacru commented on GitHub (Oct 18, 2024):

just added section on the docs as well
https://www.better-auth.com/docs/integrations/nuxt#middleware

<!-- gh-comment-id:2421627981 --> @Bekacru commented on GitHub (Oct 18, 2024): just added section on the docs as well https://www.better-auth.com/docs/integrations/nuxt#middleware
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#8173