feat: add account reset endpoint
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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: {
|
||||
|
||||
Reference in New Issue
Block a user