@@ -2,6 +2,7 @@ import bcrypt from "bcrypt";
22import { FastifyInstance , FastifyReply , FastifyRequest } from "fastify" ;
33
44import { track } from "../lib/hog" ;
5+ import { checkSession } from "../lib/session" ;
56import { prisma } from "../prisma" ;
67
78export 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