From f5a853869e30d66be9a2e67962256b9906926380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDan=20Fras?= Date: Wed, 17 Jan 2024 23:54:41 +0100 Subject: [PATCH] Update Nuxt getting started middleware (#1340) --- docs/pages/getting-started/nuxt.md | 7 +++---- docs/pages/guides/validate-session-cookies/nuxt.md | 10 ++++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/pages/getting-started/nuxt.md b/docs/pages/getting-started/nuxt.md index 4f77ab97c..ca84e7d3e 100644 --- a/docs/pages/getting-started/nuxt.md +++ b/docs/pages/getting-started/nuxt.md @@ -67,11 +67,10 @@ It's a bit verbose, but it just reads the session cookie, validates it, and sets // server/middleware/auth.ts import { verifyRequestOrigin } from "lucia"; -import type { H3Event } from "h3"; -import type { User } from "lucia"; +import type { Session, User } from "lucia"; -export default defineEventHandler((event) => { - if (context.request.method !== "GET") { +export default defineEventHandler(async (event) => { + if (event.method !== "GET") { const originHeader = getHeader(event, "Origin") ?? null; const hostHeader = getHeader(event, "Host") ?? null; if (!originHeader || !hostHeader || !verifyRequestOrigin(originHeader, [hostHeader])) { diff --git a/docs/pages/guides/validate-session-cookies/nuxt.md b/docs/pages/guides/validate-session-cookies/nuxt.md index db4c1a5f6..fa1901f91 100644 --- a/docs/pages/guides/validate-session-cookies/nuxt.md +++ b/docs/pages/guides/validate-session-cookies/nuxt.md @@ -12,11 +12,10 @@ We recommend creating a middleware to validate requests and store the current us // server/middleware/auth.ts import { verifyRequestOrigin } from "lucia"; -import type { H3Event } from "h3"; -import type { User } from "lucia"; +import type { Session, User } from "lucia"; -export default defineEventHandler((event) => { - if (context.request.method !== "GET") { +export default defineEventHandler(async (event) => { + if (event.method !== "GET") { const originHeader = getHeader(event, "Origin") ?? null; const hostHeader = getHeader(event, "Host") ?? null; if (!originHeader || !hostHeader || !verifyRequestOrigin(originHeader, [hostHeader])) { @@ -26,6 +25,7 @@ export default defineEventHandler((event) => { const sessionId = getCookie(event, lucia.sessionCookieName) ?? null; if (!sessionId) { + event.context.session = null; event.context.user = null; return; } @@ -37,12 +37,14 @@ export default defineEventHandler((event) => { if (!session) { appendResponseHeader(event, "Set-Cookie", lucia.createBlankSessionCookie().serialize()); } + event.context.session = session; event.context.user = user; }); declare module "h3" { interface H3EventContext { user: User | null; + session: Session | null; } } ```