diff --git a/package.json b/package.json index 04bfa10..4166d94 100644 --- a/package.json +++ b/package.json @@ -23,11 +23,13 @@ "@types/jsonwebtoken": "^8.5.5", "@types/node": "^16.10.3", "@types/nodemailer": "^6.4.4", + "@types/redis": "^2.8.32", "argon2": "^0.28.2", "dotenv": "^10.0.0", "express": "^4.17.1", "jsonwebtoken": "^8.5.1", - "nodemailer": "^6.7.0" + "nodemailer": "^6.7.0", + "redis": "^3.1.2" }, "devDependencies": { "@types/chai": "^4.2.22", diff --git a/src/Controllers/admin_auth.controller.ts b/src/Controllers/admin_auth.controller.ts index f4672ea..5fb01fa 100644 --- a/src/Controllers/admin_auth.controller.ts +++ b/src/Controllers/admin_auth.controller.ts @@ -1,5 +1,6 @@ import { Request, Response } from "express"; import { Admin, AdminLevel, Group } from "@prisma/client"; +import { authClient } from "../lib/redis"; import prisma from "../lib/prisma"; import argon2 from "argon2"; import jwt from "jsonwebtoken"; @@ -55,6 +56,9 @@ export const authenticateUser = async (req: Request<{}, {}, AuthenticateUserBody const param_password_hash = await argon2.hash(password, { type: argon2.argon2id }); if (param_password_hash === user.password) { + // Set password revision ID in redis + await authClient.set(user.pid, user.revision); + return { type: "success", payload: { diff --git a/src/lib/redis.ts b/src/lib/redis.ts new file mode 100644 index 0000000..afe01f5 --- /dev/null +++ b/src/lib/redis.ts @@ -0,0 +1,27 @@ +import redis from "redis"; +import { promisify } from "util"; + +const REDIS_INDICES = { + auth: 0, +}; + +const REDIS_HOST = process.env.REDIS_HOST || "redis"; +const REDIS_PORT = process.env.REDIS_PORT ? parseInt(process.env.REDIS_PORT) : 6379; + +// TODO: Extend +function createAsyncClient(client: redis.RedisClient) { + return { + get: promisify(client.get).bind(client), + set: promisify(client.set).bind(client), + }; +} + +// ----------------- // +// Redis auth client // +// ----------------- // + +const auth = redis.createClient({ host: REDIS_HOST, port: REDIS_PORT }); + +auth.select(REDIS_INDICES.auth); + +export const authClient = createAsyncClient(auth);