feat: add account reset endpoint

This commit is contained in:
2025-07-28 10:21:39 +05:30
parent e795c8b8f9
commit 0e1d0cd649
2 changed files with 37 additions and 4 deletions

View File

@@ -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;
}
}
);
}

View File

@@ -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: {