Implement redux.

This commit is contained in:
Mike Cao 2020-08-04 22:45:05 -07:00
parent 9d8a2406e1
commit 5d4ff5cfa4
31 changed files with 341 additions and 85 deletions

View file

@ -1,6 +1,7 @@
import { serialize } from 'cookie';
import { checkPassword, createSecureToken } from 'lib/crypto';
import { getAccount } from 'lib/db';
import { AUTH_COOKIE_NAME } from 'lib/constants';
export default async (req, res) => {
const { username, password } = req.body;
@ -10,7 +11,7 @@ export default async (req, res) => {
if (account && (await checkPassword(password, account.password))) {
const { user_id, username, is_admin } = account;
const token = await createSecureToken({ user_id, username, is_admin });
const cookie = serialize('umami.auth', token, {
const cookie = serialize(AUTH_COOKIE_NAME, token, {
path: '/',
httpOnly: true,
maxAge: 60 * 60 * 24 * 365,

16
pages/api/auth/logout.js Normal file
View file

@ -0,0 +1,16 @@
import { serialize } from 'cookie';
import { AUTH_COOKIE_NAME } from 'lib/constants';
export default async (req, res) => {
const cookie = serialize(AUTH_COOKIE_NAME, '', {
path: '/',
httpOnly: true,
maxAge: 0,
});
res.statusCode = 303;
res.setHeader('Set-Cookie', [cookie]);
res.setHeader('Location', '/login');
return res.end();
};

11
pages/api/auth/verify.js Normal file
View file

@ -0,0 +1,11 @@
import { useAuth } from 'lib/middleware';
export default async (req, res) => {
await useAuth(req, res);
if (req.auth) {
return res.status(200).json(req.auth);
}
return res.status(401).end();
};