mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
28 lines
733 B
TypeScript
28 lines
733 B
TypeScript
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)
|