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

27
pages/api/admin/login.ts Normal file
View File

@@ -0,0 +1,27 @@
import { authenticateAdmin } from '../../../lib/authenticate'
import withSession from '../../../lib/session'
async function loginHandler(req: any, res: any) {
const { method } = req
switch (method) {
case 'POST':
const { username, password } = req.body
if (!authenticateAdmin({ username, password })) {
res.status(401).end()
return
}
const userData = { username, role: 'admin' }
req.session.set('user', userData)
await req.session.save()
res.json({ message: 'Authenticated', user: userData })
break
default:
res.setHeader('Allow', ['POST'])
res.status(405).end(`Method ${method} Not Allowed`)
}
}
export default withSession(loginHandler)