last
last
TTCTheo's Typesafe Cult
Created by gnarley_farley. on 11/13/2024 in #questions
How do you get the session in middleware?
maybe like this?
import { decode } from "next-auth/jwt";

const cookieName = "__Secure-next-auth.session-token";
const token = request.cookies.get(cookieName)?.value;
const currentUrl = request.nextUrl.pathname;

if (!token) return NextResponse.redirect(new URL("/login", request.url));

const decryptedToken = (await decode({
token,
secret: env.NEXTAUTH_SECRET!,
})) as unknown as Session["user"];
import { decode } from "next-auth/jwt";

const cookieName = "__Secure-next-auth.session-token";
const token = request.cookies.get(cookieName)?.value;
const currentUrl = request.nextUrl.pathname;

if (!token) return NextResponse.redirect(new URL("/login", request.url));

const decryptedToken = (await decode({
token,
secret: env.NEXTAUTH_SECRET!,
})) as unknown as Session["user"];
3 replies