feat: add account reset endpoint
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
|
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 { createSession, deleteSession } from "./auth.service";
|
||||||
import { hash, verify } from "argon2";
|
import { hash, verify } from "argon2";
|
||||||
import { validatePassword } from "../utils/password";
|
import { validatePassword } from "../utils/password";
|
||||||
@@ -103,4 +107,33 @@ export async function authRoutes(fastify: FastifyInstance) {
|
|||||||
return res.code(200).send();
|
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" });
|
.populate({ path: "orgId", select: "pid name avatar" });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function resetUser(userId: string, user: AuthenticatedUser) {
|
export async function resetUser(userId: string, user?: AuthenticatedUser) {
|
||||||
if (user.role !== "superAdmin") {
|
if (user && user.role !== "superAdmin") {
|
||||||
throw ErrOpNotValid;
|
throw ErrOpNotValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
const token = await generateToken();
|
const token = await generateToken();
|
||||||
const userInDb = await userModel.findOneAndUpdate(
|
const userInDb = await userModel.findOneAndUpdate(
|
||||||
{ pid: userId, tenantId: user.tenantId },
|
{ pid: userId },
|
||||||
{
|
{
|
||||||
$set: {
|
$set: {
|
||||||
token: {
|
token: {
|
||||||
|
|||||||
Reference in New Issue
Block a user