Added rev_id column. Updated redis calls.

This commit is contained in:
Mike Cao 2022-11-07 16:22:49 -08:00
parent a9112f39ec
commit 3485b6268b
18 changed files with 133 additions and 79 deletions

View file

@ -1,6 +1,8 @@
import { ok, unauthorized, badRequest, checkPassword, createSecureToken } from 'next-basics';
import { getUser } from 'queries';
import { secret } from 'lib/crypto';
import redis from 'lib/redis';
import { generateAuthToken } from 'lib/auth';
export default async (req, res) => {
const { username, password } = req.body;
@ -13,6 +15,13 @@ export default async (req, res) => {
if (user && checkPassword(password, user.password)) {
const { id: userId, username, isAdmin } = user;
if (redis.enabled) {
const token = `auth:${generateAuthToken()}`;
return ok(res, { token, user });
}
const token = createSecureToken({ userId, username, isAdmin }, secret());
return ok(res, { token, user });

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

@ -0,0 +1,18 @@
import { methodNotAllowed, ok } from 'next-basics';
import { useAuth } from 'lib/middleware';
import redis from 'lib/redis';
import { getAuthToken } from 'lib/auth';
export default async (req, res) => {
await useAuth(req, res);
if (req.method === 'POST') {
if (redis.enabled) {
await redis.del(`auth:${getAuthToken(req)}`);
}
return ok(res);
}
return methodNotAllowed(res);
};

View file

@ -1,14 +1,22 @@
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { removeItem } from 'next-basics';
import { removeItem, useApi } from 'next-basics';
import { AUTH_TOKEN } from 'lib/constants';
import { setUser } from 'store/app';
export default function LogoutPage() {
const router = useRouter();
const { post } = useApi();
useEffect(() => {
async function logout() {
await post('/logout');
}
removeItem(AUTH_TOKEN);
logout();
router.push('/login');
return () => setUser(null);