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,28 +1,18 @@
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
export function authenticateAdmin({
username,
password,
}: {
username: string
password: string
}) {
if (username !== 'admin') {
return false
}
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' }
// FIXME: move at least to env variable
if (password !== 'secret') {
return false
}
res.setHeader('WWW-Authenticate', 'Basic')
res.statusCode = 401
res.end()
return null
return true
}