mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
Admin page for bill wit iron-session (#13)
This commit is contained in:
28
lib/authenticate.ts
Normal file
28
lib/authenticate.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { IncomingMessage, ServerResponse } from 'http'
|
||||
|
||||
export default function authenticate(
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse
|
||||
) {
|
||||
const authHeader = req.headers.authorization
|
||||
|
||||
if (!authHeader) {
|
||||
res.setHeader('WWW-Authenticate', 'Basic')
|
||||
res.statusCode = 401
|
||||
return null
|
||||
}
|
||||
|
||||
const [username, password] = Buffer.from(authHeader.split(' ')[1], 'base64')
|
||||
.toString()
|
||||
.split(':')
|
||||
|
||||
// FIXME: pull admin password from env
|
||||
if (username === 'admin' || password === 'secret') {
|
||||
return { username: 'admin', role: 'admin' }
|
||||
}
|
||||
|
||||
res.setHeader('WWW-Authenticate', 'Basic')
|
||||
res.statusCode = 401
|
||||
res.end()
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user