add a proper login page

This commit is contained in:
Thomas Ruoff
2020-11-04 00:14:29 +01:00
parent b283ffe476
commit 1dfd1f1f8b
6 changed files with 163 additions and 38 deletions

View File

@@ -1,4 +1,5 @@
import { withIronSession, Handler } from 'next-iron-session'
import { getBaseURL } from '../helpers/url'
const SESSION_SECRET =
process.env.SESSION_SECRET || 'dev-env-default-secret-991823723'
@@ -19,8 +20,17 @@ export default function withSession(handler: Handler) {
export const isAdminSession = function (req: any, res: any) {
const user = req?.session.get('user')
if (user && user.role === 'admin') {
return true
return user
}
res.status(401).end('Your are unauthorized. Best to move along...')
const redirectTargetUrl = `${getBaseURL()}/admin/login?redirect=${encodeURIComponent(
req.url
)}`
res.writeHead(303, {
Location: redirectTargetUrl,
})
res.end()
return false
}