mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
add a proper login page
This commit is contained in:
27
pages/api/admin/login.ts
Normal file
27
pages/api/admin/login.ts
Normal 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)
|
||||
Reference in New Issue
Block a user