Skip to content

Commit 5291aec

Browse files
committed
fix: admin only check
1 parent 5e8816f commit 5291aec

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

apps/api/src/controllers/users.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import bcrypt from "bcrypt";
22
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
33

44
import { track } from "../lib/hog";
5+
import { checkSession } from "../lib/session";
56
import { prisma } from "../prisma";
67

78
export function userRoutes(fastify: FastifyInstance) {
@@ -70,20 +71,25 @@ export function userRoutes(fastify: FastifyInstance) {
7071
// (ADMIN) Reset password
7172
fastify.put(
7273
"/api/v1/user/reset-password",
73-
7474
async (request: FastifyRequest, reply: FastifyReply) => {
7575
const { password, id }: any = request.body;
7676

77-
const hashedPass = await bcrypt.hash(password, 10);
78-
await prisma.user.update({
79-
where: { id: id },
80-
data: {
81-
password: hashedPass,
82-
},
83-
});
84-
reply
85-
.status(201)
86-
.send({ message: "password updated success", failed: false });
77+
const session = await checkSession(request);
78+
79+
if (session!.isAdmin) {
80+
const hashedPass = await bcrypt.hash(password, 10);
81+
await prisma.user.update({
82+
where: { id: id },
83+
data: {
84+
password: hashedPass,
85+
},
86+
});
87+
reply
88+
.status(201)
89+
.send({ message: "password updated success", failed: false });
90+
} else {
91+
reply.status(403).send({ message: "Unauthorized", failed: true });
92+
}
8793
}
8894
);
8995

0 commit comments

Comments
 (0)