Protect routes via middleware in Nuxt #89

Closed
opened 2026-03-13 07:32:44 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @jonmh85 on GitHub (Oct 17, 2024).

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). 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
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")
  }
})
@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

@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#89