Files
archived-next-auth/apps/playgrounds/nuxt/plugins/auth.ts
Robert Soriano 8b3f0696a5 chore(playgrounds): Nuxt 3.5.1 (#7626)
* bump Nuxt to 3.5.1

* follow playground package names

* chore: update nuxt playground scripts

* fix: imports and types

* fix: more nuxt type imports

* fix: nuxt auth options types

* fix: nuxt client fetch types
2023-06-01 14:49:45 +02:00

20 lines
458 B
TypeScript

import { Session } from "@auth/core/types"
export default defineNuxtPlugin(async () => {
const session = useSession()
addRouteMiddleware("auth", () => {
if (!session.value) return navigateTo("/")
})
if (process.server) {
const data = await $fetch<Session>("/api/auth/session", {
headers: useRequestHeaders() as any,
})
const hasSession = data && Object.keys(data).length
session.value = hasSession ? data : null
}
})