make isAdminSession usable by api routes

This commit is contained in:
Thomas Ruoff
2020-11-04 00:33:35 +01:00
parent 1dfd1f1f8b
commit 7ee9803adc
5 changed files with 19 additions and 7 deletions

View File

@@ -12,17 +12,20 @@ export default function withSession(handler: Handler) {
// 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) {
export function isAdminSession(req: any, res: any) {
const user = req?.session.get('user')
if (user && user.role === 'admin') {
return user
}
return false
}
export function redirectToLogin(req: any, res: any) {
const redirectTargetUrl = `${getBaseURL()}/admin/login?redirect=${encodeURIComponent(
req.url
)}`
@@ -31,6 +34,4 @@ export const isAdminSession = function (req: any, res: any) {
Location: redirectTargetUrl,
})
res.end()
return false
}