mirror of
https://github.com/umami-software/umami.git
synced 2026-02-12 00:27:11 +01:00
Switch to json web tokens.
This commit is contained in:
parent
5219582803
commit
cb0c912c5b
10 changed files with 202 additions and 86 deletions
|
|
@ -1,18 +1,21 @@
|
|||
import { serialize } from 'cookie';
|
||||
import { hash, random, encrypt } from 'lib/crypto';
|
||||
import { checkPassword, createToken, secret } from 'lib/crypto';
|
||||
import { getAccount } from 'lib/db';
|
||||
|
||||
export default (req, res) => {
|
||||
const { password } = req.body;
|
||||
export default async (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
|
||||
if (password === process.env.PASSWORD) {
|
||||
const account = await getAccount(username);
|
||||
|
||||
if (account && (await checkPassword(password, account.password))) {
|
||||
const { user_id, username, is_admin } = account;
|
||||
const token = await createToken({ user_id, username, is_admin });
|
||||
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 });
|
||||
const cookie = serialize('umami.auth', token, { expires, httpOnly: true });
|
||||
|
||||
res.setHeader('Set-Cookie', [cookie]);
|
||||
res.status(200).send('');
|
||||
|
||||
res.status(200).send({ token });
|
||||
} else {
|
||||
res.status(401).send('');
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue