Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/atlp-rwanda/eagles-ec-be int…
Browse files Browse the repository at this point in the history
…o ft-google-auth-#187419170
  • Loading branch information
MugemaneBertin2001 committed Apr 29, 2024
2 parents 1c2ac98 + 5d9a0bf commit c54ecd5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
17 changes: 7 additions & 10 deletions src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { generateToken } from "../utils/jsonwebtoken";
import * as mailService from "../services/mail.service";
import { IUser, STATUS, SUBJECTS } from "../types";
import { comparePasswords } from "../utils/comparePassword";
import { loggedInUser } from "../services/user.service";
import { createUserService, getUserByEmail, updateUserPassword } from "../services/user.service";
import { createUserService, loggedInUser, updateUserPassword } from "../services/user.service";
import User, { UserAttributes } from "../sequelize/models/users";
import { hashedPassword } from "../utils/hashPassword";
import Token, { TokenAttributes } from "../sequelize/models/Token";
Expand Down Expand Up @@ -84,8 +83,7 @@ export const createUserController = async (req: Request, res: Response) => {
}
return res.status(201).json({
status: 201,
message: "User successfully created.",
user,
message: "User successfully created."
});
} catch (err: any) {
if (err.name === "UnauthorizedError" && err.message === "User already exists") {
Expand All @@ -99,10 +97,7 @@ export const updatePassword = async (req: Request, res: Response) => {
const { oldPassword, newPassword, confirmPassword } = req.body;
try {
// @ts-ignore
const user = await getUserByEmail(req.user.email);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const { user }:IUser = req;
const isPasswordValid = await comparePasswords(oldPassword, user.password);
if (!isPasswordValid) {
return res.status(400).json({ message: "Old password is incorrect" });
Expand All @@ -117,8 +112,10 @@ export const updatePassword = async (req: Request, res: Response) => {
}

const password = await hashedPassword(newPassword);
await updateUserPassword(user, password);
return res.status(200).json({ message: "Password updated successfully" });
const update = await updateUserPassword(user, password);
if(update){
return res.status(200).json({ message: "Password updated successfully" });
}
} catch (err: any) {
return res.status(500).json({
message: err.message,
Expand Down
5 changes: 2 additions & 3 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export const findUserById = async (id: string) => {
}
};
export const updateUserPassword = async (user: User, password: string) => {
user.password = password;
const update = await user.save;
return update;
const update = await User.update({ password: password}, { where: { id: user.id}})
return update
};

0 comments on commit c54ecd5

Please sign in to comment.