umami/pages/api/auth.js

20 lines
529 B
JavaScript
Raw Normal View History

2020-07-23 00:46:05 +02:00
import { serialize } from 'cookie';
import { hash, random, encrypt } from 'lib/crypto';
export default (req, res) => {
const { password } = req.body;
if (password === process.env.PASSWORD) {
const expires = new Date(Date.now() + 31536000000);
const id = random();
const value = encrypt(`${id}:${hash(id)}`);
const cookie = serialize('umami.auth', value, { expires, httpOnly: true });
res.setHeader('Set-Cookie', [cookie]);
res.status(200).send('');
} else {
res.status(401).send('');
}
};