Skip to content

Commit ba98275

Browse files
authored
Merge pull request #251 from CS3219-AY2425S1/wheatgrassmc/more-fixes
Wheatgrassmc/more fixes
2 parents c862bed + c624ea0 commit ba98275

File tree

6 files changed

+359
-139
lines changed

6 files changed

+359
-139
lines changed

backend/user/controller/user-controller.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,47 @@ export async function resetPasswordUsingCode(req, res) {
380380
}
381381
}
382382

383+
export async function resetPasswordFromProfile(req, res) {
384+
try {
385+
const { password } = req.body;
386+
const userId = req.params.id;
387+
console.log(req.body);
388+
if (password) {
389+
const user = await _findUserById(userId);
390+
if (!user) {
391+
console.log("ERROR");
392+
return res
393+
.status(404)
394+
.json({ message: `User not found with user ID ${code}` });
395+
}
396+
397+
const salt = bcrypt.genSaltSync(10);
398+
const hashedPassword = bcrypt.hashSync(password, salt);
399+
400+
const updatedUser = await _updateUserById(
401+
user.id,
402+
user.username,
403+
user.email,
404+
hashedPassword,
405+
user.bio,
406+
user.linkedin,
407+
user.github
408+
);
409+
return res.status(200).json({
410+
message: `Reset password for user ${user.username}`,
411+
data: formatUserResponse(updatedUser),
412+
});
413+
} else {
414+
return res.status(400).json({ message: "password is missing!" });
415+
}
416+
} catch (err) {
417+
console.error(err);
418+
return res
419+
.status(500)
420+
.json({ message: "Unknown error when resetting password!" });
421+
}
422+
}
423+
383424
export function formatUserResponse(user) {
384425
return {
385426
id: user.id,

backend/user/routes/user-routes.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
updateUserPrivilege,
1313
getFileUrl,
1414
verifyUser,
15+
resetPasswordFromProfile,
1516
} from "../controller/user-controller.js";
1617
import {
1718
verifyAccessToken,
@@ -32,6 +33,8 @@ router.patch(
3233

3334
router.get("/verify", verifyUser);
3435

36+
router.post("/:id/reset-password-from-profile", resetPasswordFromProfile);
37+
3538
router.post("/request-password-reset", requestPasswordReset);
3639

3740
router.post("/check-password-reset-code", checkPasswordResetCode);

frontend/src/api/user.tsx

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AuthStatus, UploadProfilePictureResponse } from "@/types/user";
44
import Cookie from "js-cookie";
55
import Swal from "sweetalert2";
66

7-
const toast = Swal.mixin({
7+
export const ToastComponent = Swal.mixin({
88
toast: true,
99
position: "top-end",
1010
showConfirmButton: false,
@@ -88,7 +88,7 @@ export const verifyToken = async (token: string) => {
8888
};
8989

9090
export const login = async (email: string, password: string) => {
91-
toast.fire({
91+
ToastComponent.fire({
9292
icon: "info",
9393
title: "Logging in...",
9494
});
@@ -105,7 +105,7 @@ export const login = async (email: string, password: string) => {
105105
const data = await response.json();
106106

107107
if (response.status !== 200) {
108-
toast.fire({
108+
ToastComponent.fire({
109109
icon: "error",
110110
title: data.message,
111111
});
@@ -134,7 +134,7 @@ export const register = async (
134134
password: string,
135135
username: string
136136
) => {
137-
toast.fire({
137+
ToastComponent.fire({
138138
icon: "info",
139139
title: "Registering...",
140140
});
@@ -152,7 +152,7 @@ export const register = async (
152152
const data = await response.json();
153153

154154
if (response.status !== 201) {
155-
toast.fire({
155+
ToastComponent.fire({
156156
icon: "error",
157157
title: data.message,
158158
});
@@ -175,7 +175,7 @@ export const getUser = async (userId = "") => {
175175
const data = await response.json();
176176

177177
if (response.status !== 200) {
178-
toast.fire({
178+
ToastComponent.fire({
179179
icon: "error",
180180
title: data.message,
181181
});
@@ -185,10 +185,38 @@ export const getUser = async (userId = "") => {
185185
return data;
186186
};
187187

188+
export const resetPasswordFromProfile = async (newPassword: string) => {
189+
const userId = getUserId();
190+
const token = getToken();
191+
const url = `${NEXT_PUBLIC_IAM_USER_SERVICE}/${userId}/reset-password-from-profile`;
192+
const response = await fetch(url, {
193+
method: "POST",
194+
headers: {
195+
"Content-Type": "application/json",
196+
Authorization: `Bearer ${token}`,
197+
},
198+
body: JSON.stringify({ password: newPassword }),
199+
});
200+
201+
const data = await response.json();
202+
if (response.status === 400) {
203+
ToastComponent.fire({
204+
icon: "error",
205+
title: data.message,
206+
});
207+
return false;
208+
} else if (response.status === 200) {
209+
ToastComponent.fire({
210+
icon: "success",
211+
title: data.message,
212+
});
213+
return true;
214+
}
215+
};
216+
188217
export const updateUser = async (userData: {
189218
username?: string;
190219
email?: string;
191-
password?: string;
192220
bio?: string;
193221
linkedin?: string;
194222
github?: string;
@@ -208,14 +236,14 @@ export const updateUser = async (userData: {
208236
console.log(data);
209237

210238
if (response.status !== 200) {
211-
toast.fire({
239+
ToastComponent.fire({
212240
icon: "error",
213241
title: data.message,
214242
});
215243
return false;
216244
}
217245

218-
toast.fire({
246+
ToastComponent.fire({
219247
icon: "success",
220248
title: "Profile updated successfully",
221249
});
@@ -241,7 +269,7 @@ export const getFileUrl = async (
241269
const data: UploadProfilePictureResponse = await res.json();
242270

243271
if (res.status !== 200) {
244-
toast.fire({
272+
ToastComponent.fire({
245273
icon: "error",
246274
title: res.statusText,
247275
});
@@ -251,7 +279,7 @@ export const getFileUrl = async (
251279
};
252280

253281
export const requestPasswordReset = async (email: string) => {
254-
toast.fire({
282+
ToastComponent.fire({
255283
icon: "info",
256284
title: "Requesting password reset...",
257285
});
@@ -269,14 +297,14 @@ export const requestPasswordReset = async (email: string) => {
269297
console.log(data);
270298

271299
if (response.status !== 200) {
272-
toast.fire({
300+
ToastComponent.fire({
273301
icon: "error",
274302
title: data.message,
275303
});
276304
return false;
277305
}
278306

279-
toast.fire({
307+
ToastComponent.fire({
280308
icon: "success",
281309
title: "Password reset requested. Please check your email.",
282310
});
@@ -298,7 +326,7 @@ export const checkPasswordResetCode = async (code: string) => {
298326
const data = await response.json();
299327

300328
if (response.status !== 200) {
301-
toast.fire({
329+
ToastComponent.fire({
302330
icon: "error",
303331
title: data.message,
304332
});
@@ -322,7 +350,7 @@ export const resetPasswordWithCode = async (code: string, password: string) => {
322350
const data = await response.json();
323351

324352
if (response.status !== 200) {
325-
toast.fire({
353+
ToastComponent.fire({
326354
icon: "error",
327355
title: data.message,
328356
});

0 commit comments

Comments
 (0)