Added redis check to verify.

This commit is contained in:
Mike Cao 2022-11-08 12:28:45 -08:00
parent 13fd3ccd16
commit 208fcb8418
4 changed files with 20 additions and 13 deletions

View file

@ -14,14 +14,15 @@ export default async (req, res) => {
const user = await getUser({ username });
if (user && checkPassword(password, user.password)) {
const { id: userId, username, isAdmin } = user;
if (redis.enabled) {
const token = `auth:${generateAuthToken()}`;
const token = generateAuthToken();
await redis.set(token, user);
return ok(res, { token, user });
}
const { id: userId, username, isAdmin } = user;
const token = createSecureToken({ userId, username, isAdmin }, secret());
return ok(res, { token, user });

View file

@ -1,11 +1,21 @@
import { useAuth } from 'lib/middleware';
import { ok, unauthorized } from 'next-basics';
import redis from 'lib/redis';
import { secret } from 'lib/crypto';
import { getAuthToken } from 'lib/auth';
export default async (req, res) => {
await useAuth(req, res);
if (redis.enabled) {
const token = await getAuthToken(req, secret());
const user = await redis.get(token);
if (req.auth) {
return ok(res, req.auth);
return ok(res, user);
} else {
await useAuth(req, res);
if (req.auth) {
return ok(res, req.auth);
}
}
return unauthorized(res);