diff --git a/src/auth/auth.route.ts b/src/auth/auth.route.ts index 41a4da0..d3862da 100644 --- a/src/auth/auth.route.ts +++ b/src/auth/auth.route.ts @@ -1,5 +1,9 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify"; -import { getUserByEmail, getUserByToken } from "../user/user.service"; +import { + getUserByEmail, + getUserByToken, + resetUser, +} from "../user/user.service"; import { createSession, deleteSession } from "./auth.service"; import { hash, verify } from "argon2"; import { validatePassword } from "../utils/password"; @@ -103,4 +107,33 @@ export async function authRoutes(fastify: FastifyInstance) { return res.code(200).send(); } ); + + fastify.post( + "/reset", + { + schema: { + body: { + type: "object", + properties: { + email: { type: "string" }, + }, + }, + }, + }, + async (req: FastifyRequest, res: FastifyReply) => { + const { email } = req.body as { email: string }; + + try { + const userInDB = await getUserByEmail(email); + + if (userInDB) { + await resetUser(userInDB.pid); + } + + return res.code(200).send(); + } catch (err) { + return err; + } + } + ); } diff --git a/src/user/user.service.ts b/src/user/user.service.ts index ca2ff47..dc47355 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -72,14 +72,14 @@ export async function createUser( .populate({ path: "orgId", select: "pid name avatar" }); } -export async function resetUser(userId: string, user: AuthenticatedUser) { - if (user.role !== "superAdmin") { +export async function resetUser(userId: string, user?: AuthenticatedUser) { + if (user && user.role !== "superAdmin") { throw ErrOpNotValid; } const token = await generateToken(); const userInDb = await userModel.findOneAndUpdate( - { pid: userId, tenantId: user.tenantId }, + { pid: userId }, { $set: { token: {