feat(frontend): Use betterAuth implementation from nuxthub-better-auth project

This commit is contained in:
2025-04-20 09:54:16 +02:00
parent 4e7a962a06
commit eec15dd7ef
17 changed files with 209 additions and 66 deletions

View File

@@ -0,0 +1,63 @@
// Copied from https://github.com/atinux/nuxthub-better-auth
import { defu } from 'defu'
type MiddlewareOptions =
| false
| {
/**
* Only apply auth middleware to guest or user
*/
only?: 'guest' | 'user'
/**
* Redirect authenticated user to this route
*/
redirectUserTo?: string
/**
* Redirect guest to this route
*/
redirectGuestTo?: string
}
declare module '#app' {
interface PageMeta {
auth?: MiddlewareOptions
}
}
declare module 'vue-router' {
interface RouteMeta {
auth?: MiddlewareOptions
}
}
export default defineNuxtRouteMiddleware(async (to) => {
// If auth is disabled, skip middleware
if (to.meta?.auth === false) {
return
}
const { loggedIn, options, fetchSession } = useAuth()
const { only, redirectUserTo, redirectGuestTo } = defu(to.meta?.auth, options)
// If guest mode, redirect if authenticated
if (only === 'guest' && loggedIn.value) {
// Avoid infinite redirect
if (to.path === redirectUserTo) {
return
}
return navigateTo(redirectUserTo)
}
// If client-side, fetch session between each navigation
if (import.meta.client) {
await fetchSession()
}
// If not authenticated, redirect to home
if (!loggedIn.value) {
// Avoid infinite redirect
if (to.path === redirectGuestTo) {
return
}
return navigateTo(redirectGuestTo)
}
})

View File

@@ -1,7 +0,0 @@
export default defineNuxtRouteMiddleware(async (_to, _from) => {
const { data: session } = await useSession(useFetch)
if (!session.value) {
return navigateTo('/login')
}
})