mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 14:37:13 +01:00
27 lines
804 B
TypeScript
27 lines
804 B
TypeScript
import { withIronSession, Handler } from 'next-iron-session'
|
|
|
|
const SESSION_SECRET =
|
|
process.env.SESSION_SECRET || 'dev-env-default-secret-991823723'
|
|
|
|
export default function withSession(handler: Handler) {
|
|
return withIronSession(handler, {
|
|
password: SESSION_SECRET,
|
|
cookieName: 'pfadi-bussle-cookie',
|
|
cookieOptions: {
|
|
// the next line allows to use the session in non-https environements like
|
|
// Next.js dev mode (http://localhost:3000)
|
|
secure: process.env.NODE_ENV === 'production',
|
|
path: '/admin',
|
|
},
|
|
})
|
|
}
|
|
|
|
export const isAdminSession = function (req: any, res: any) {
|
|
const user = req?.session.get('user')
|
|
if (user && user.role === 'admin') {
|
|
return true
|
|
}
|
|
res.status(401).end('Your are unauthorized. Best to move along...')
|
|
return false
|
|
}
|