Skip to content

Commit

Permalink
fix: admin only check
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Nov 14, 2024
1 parent 5e8816f commit 5291aec
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions apps/api/src/controllers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import bcrypt from "bcrypt";
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";

import { track } from "../lib/hog";
import { checkSession } from "../lib/session";
import { prisma } from "../prisma";

export function userRoutes(fastify: FastifyInstance) {
Expand Down Expand Up @@ -70,20 +71,25 @@ export function userRoutes(fastify: FastifyInstance) {
// (ADMIN) Reset password
fastify.put(
"/api/v1/user/reset-password",

async (request: FastifyRequest, reply: FastifyReply) => {
const { password, id }: any = request.body;

const hashedPass = await bcrypt.hash(password, 10);
await prisma.user.update({
where: { id: id },
data: {
password: hashedPass,
},
});
reply
.status(201)
.send({ message: "password updated success", failed: false });
const session = await checkSession(request);

if (session!.isAdmin) {
const hashedPass = await bcrypt.hash(password, 10);
await prisma.user.update({
where: { id: id },
data: {
password: hashedPass,
},
});
reply
.status(201)
.send({ message: "password updated success", failed: false });
} else {
reply.status(403).send({ message: "Unauthorized", failed: true });
}
}
);

Expand Down

0 comments on commit 5291aec

Please sign in to comment.