How do you get the session in middleware?

getServerAuthSession throws an error.
1 Reply
last
last2mo ago
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"];
Want results from more Discord servers?
Add your server