mirror of
https://github.com/SrIzan10/next-auth.git
synced 2026-05-01 10:55:20 +00:00
chore(dev): update session example endpoint
This commit is contained in:
@@ -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)
|
||||
}
|
||||
21
apps/dev/nextjs/pages/api/examples/session.ts
Normal file
21
apps/dev/nextjs/pages/api/examples/session.ts
Normal 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",
|
||||
}
|
||||
Reference in New Issue
Block a user