chore(dev): update session example endpoint

This commit is contained in:
Balázs Orbán
2023-01-02 11:15:58 +01:00
parent 5064f20e61
commit cb2da4dd9c
2 changed files with 21 additions and 8 deletions

View File

@@ -1,8 +0,0 @@
// This is an example of how to access a session from an API route
import { unstable_getServerSession } from "next-auth/next"
import { authOptions } from "../auth/[...nextauth]"
export default async (req, res) => {
const session = await unstable_getServerSession(req, res, authOptions)
res.json(session)
}

View File

@@ -0,0 +1,21 @@
import { Auth, SessionRequest } from "@auth/core"
import { authConfig } from "../auth/[...nextauth]"
export default async function handle(req: Request) {
authConfig.secret = process.env.AUTH_SECRET
const response = await Auth(new SessionRequest(req), authConfig)
const session = await response.session()
if (!session) {
return new Response("Not authenticated", { status: 401 })
}
console.log(session.user) // Do something with the session
// Pass the original headers to set cookies (eg.: updating the session expiry)
response.headers.set("content-type", "text/plain")
return new Response("Authenticated", { headers: response.headers })
}
export const config = {
runtime: "experimental-edge",
}