Skip to content

Commit

Permalink
Update Nuxt getting started middleware (#1340)
Browse files Browse the repository at this point in the history
  • Loading branch information
frasza authored Jan 17, 2024
1 parent be8aada commit f5a8538
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 3 additions & 4 deletions docs/pages/getting-started/nuxt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
10 changes: 6 additions & 4 deletions docs/pages/guides/validate-session-cookies/nuxt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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;
}
Expand All @@ -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;
}
}
```
Expand Down

0 comments on commit f5a8538

Please sign in to comment.