I am making panel with NextAuth, and I wonder how can I use it in API routes?
I need to get users email or anything to work with database.
Documentation has only in NextJS “pages” API, but I am on app router.
The API routes code:
import { authOptions } from "pages/api/auth/[...nextauth]"
import { getServerSession } from "next-auth/next"
export default async function handler(req, res) {
const session = await getServerSession(req, res, authOptions)
if (!session) {
res.status(401).json({ message: "You must be logged in." })
return
}
return res.json({
message: "Success",
})
}
Fungy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1