Skip to content

Commit

Permalink
feat(user): change password endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 14, 2024
1 parent 797fb13 commit a887abd
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller } from "@/types";
import { logger } from "@middleware/logging.middleware";
import { updateProfileSchema } from "@schemas/user.schema";
import { changePasswordSchema } from "@schemas/auth.schema";
import * as userService from "@services/user.service";
import { RequestHandler } from "express";
import { z } from "zod";
Expand Down Expand Up @@ -51,3 +52,25 @@ export const updateProfilePicture: RequestHandler = async (req, res) => {
});
}
};

type ChangePasswordSchema = z.infer<typeof changePasswordSchema>;
export const changePassword: Controller<ChangePasswordSchema> = async (
req,
res,
) => {
try {
await userService.changePassword(req.user.id, req.body.newPassword);

res.json({
status: "success",
message: "Password changed successfully",
});
} catch (error) {
logger.error(`Failed to change password: ${error}`);

res.status(500).json({
status: "error",
message: "Failed to change password",
});
}
};
4 changes: 2 additions & 2 deletions src/middleware/validation.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { RequestHandler } from "express";
import { AnyZodObject, z } from "zod";
import { z, ZodTypeAny } from "zod";
import { logger } from "./logging.middleware";
import { imageUpload } from "./multer.middleware";

export const validator =
(schema: AnyZodObject): RequestHandler =>
(schema: ZodTypeAny): RequestHandler =>
async (req, res, next) => {
try {
const result = await schema.parseAsync({
Expand Down
9 changes: 9 additions & 0 deletions src/routes/v1/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
validateProfilePictureUpload,
validator,
} from "@middleware/validation.middleware";
import { changePasswordSchema } from "@schemas/auth.schema";
import { updateProfileSchema } from "@schemas/user.schema";
import { Router } from "express";

Expand All @@ -27,4 +28,12 @@ userRouter.put(
userController.updateProfilePicture,
);

// TODO: harus validasi dulu password lamanya. Di firebase-admin ga ada kayak compare password
// jadi harus di handle di client atau generate password reset link yang dikirim di email
userRouter.put(
"/password",
validator(changePasswordSchema),
userController.changePassword,
);

export default userRouter;
13 changes: 13 additions & 0 deletions src/schemas/auth.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ export const registerSchema = z.object({
password: z.string().min(8, "Password must be at least 8 characters"),
}),
});

export const changePasswordSchema = z
.object({
body: z.object({
currentPassword: z.string(),
newPassword: z.string().min(8, "Password must be at least 8 characters"),
confirmPassword: z.string(),
}),
})
.refine((data) => data.body.newPassword === data.body.confirmPassword, {
message: "Passwords do not match",
path: ["confirmPassword"],
});
1 change: 0 additions & 1 deletion src/schemas/tutor.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const tutorSchema = z.object({
message:
"Gender must be one of the following: male, female, or prefer not to say",
})
.default("prefer not to say")
.optional(),
services: z
.array(z.string())
Expand Down
1 change: 0 additions & 1 deletion src/schemas/user.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const userSchema = z.object({
message:
"Gender must be one of the following: male, female, or prefer not to say",
})
.default("prefer not to say")
.optional(),
interests: z
.array(z.string())
Expand Down
12 changes: 11 additions & 1 deletion src/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { firestore, GCS_BUCKET_NAME } from "@/config";
import { auth, firestore, GCS_BUCKET_NAME } from "@/config";
import { downscaleImage } from "@/helpers/image.helper";
import { Storage } from "@google-cloud/storage";
import { logger } from "@middleware/logging.middleware";
Expand Down Expand Up @@ -57,3 +57,13 @@ export const updateUserProfilePicture = async (
return undefined;
}
};

export const changePassword = async (userId: string, newPassword: string) => {
try {
await auth.updateUser(userId, {
password: newPassword,
});
} catch (error) {
throw new Error(`Failed to change password: ${error}`);
}
};

0 comments on commit a887abd

Please sign in to comment.