'admin pages in Next js that only authorized users
I have admin pages in Next js that only authorized users should have access to. I have a cookie verification service:
import { NextApiRequest } from 'next'
import { unsign } from './signature'
import db from '../../prisma'
export default async function checkCookie (req: NextApiRequest) {
const token = req.cookies['sid']
if (token) {
const sessionToken = unsign(token, process.env.SECRET!)
if (sessionToken && typeof sessionToken === 'string') {
const session = await db.session.findUnique({ where: { sessionToken },
include: { admin: true } })
if (session) {
return { admin: session.admin }
}
}
}
}
where should this service be located ?
import { NextPage } from "next"
import TableService from "../../src/component/TableService"
import AdminLayout from "../../src/component/admin/AdminLayout"
import TableTradeIn from "../../src/component/TableTradeIn"
import checkCookie from "../../src/services/checkCookie"
const AdminTable: NextPage = (checkCookie) => {
return (
<>
<AdminLayout title="Admin">
<TableService />
<TableTradeIn />
</AdminLayout>
</>
)
}
export default AdminTable
But in this place ('checkCookie' is declared but its value is never read.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|