Cookie authentication.

This commit is contained in:
Mike Cao 2020-07-22 15:46:05 -07:00
parent 0edf87941a
commit 5219582803
6 changed files with 337 additions and 29 deletions

19
pages/api/auth.js Normal file
View file

@ -0,0 +1,19 @@
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('');
}
};