Files
archived-next-auth/apps/playgrounds/nuxt/plugins/auth.ts
Thang Vu 4aec142525 chore: split apps to dev, examples, playgrounds (#6228)
* chore: dev -> dev/nextjs

* chore: move to /examples

* chore: move to playgrounds, add dev/sveltekit

* Update sync.yml

* chore: dev scripts
2022-12-31 07:17:42 +00:00

20 lines
452 B
TypeScript

import { Session } from "@auth/core"
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
}
})