Skip to content

Commit

Permalink
Merge pull request #7 from fga-eps-mds/feat_update
Browse files Browse the repository at this point in the history
user can change password
  • Loading branch information
guipeeix7 authored Dec 9, 2024
2 parents 2fcd726 + d8c5c66 commit af6428c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
11 changes: 11 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import pluginReact from "eslint-plugin-react";

/** @type {import('eslint').Linter.Config[]} */
export default [
{ files: ["**/*.{js,mjs,cjs,jsx}"] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
pluginReact.configs.flat.recommended,
];
66 changes: 55 additions & 11 deletions src/Controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,54 @@ const getUserById = async (req, res) => {
}
};

const getLoggedUserId = async (req, res) => {
const token = req.headers.authorization?.split(" ")[1];

if (!token) {
return res.status(401).json({ message: "Token não fornecido" });
}

try {
const decoded = jwt.verify(token, SECRET);

userId = decoded.id;
} catch (err) {
console.log(err);
return res.status(401).json({ message: "Token inválido ou expirado" });
}
return userId;
};

const getLoggedUser = async (req, res) => {
//let userId = await this.getLoggedUserId(req,res);

const token = req.headers.authorization?.split(" ")[1];

if (!token) {
return res.status(401).json({ message: "Token não fornecido" });
}

try {
const decoded = jwt.verify(token, SECRET);

userId = decoded.id;
} catch (err) {
console.log(err);

return res.status(401).json({ message: "Token inválido ou expirado" });
}

try {
const user = await User.findById(userId).populate("role");
if (!user) {
return res.status(404).send();
}
res.status(200).send(user);
} catch (error) {
res.status(500).send(error);
}
};

const patchUser = async (req, res) => {
const userId = req.params.id;

Expand Down Expand Up @@ -178,6 +226,7 @@ const deleteUser = async (req, res) => {

const update = async (req, res) => {
let userId;

const token = req.headers.authorization?.split(" ")[1];

if (!token) {
Expand All @@ -188,8 +237,9 @@ const update = async (req, res) => {
const decoded = jwt.verify(token, SECRET);

userId = decoded.id;
// eslint-disable-next-line no-unused-vars
} catch (err) {
console.log(err);

return res.status(401).json({ message: "Token inválido ou expirado" });
}

Expand Down Expand Up @@ -295,22 +345,15 @@ const changePassword = async (req, res) => {

const changePasswordInProfile = async (req, res) => {
const { old_password, new_password } = req.body;
const userId = req.params.id;

const userId = await getLoggedUserId(req, res);

try {
const user = await User.findById(userId);

if (!user) {
return res.status(404).send();
}

if (userId !== req.userId) {
return res.status(403).json({
mensagem:
"O token fornecido não tem permissão para finalizar a operação",
});
}

if (!bcrypt.compareSync(old_password, user.password)) {
return res.status(401).json({
mensagem: "Senha atual incorreta.",
Expand All @@ -324,7 +367,7 @@ const changePasswordInProfile = async (req, res) => {
mensagem: "senha alterada com sucesso.",
});
} catch (error) {
return res.status(500).send(error);
return res.status(500).send({ myerror: error });
}
};
const teste = async (req, res) => {
Expand All @@ -345,6 +388,7 @@ module.exports = {
getUsers,
getUserById,
update,
getLoggedUser,
deleteUser,
patchUser,
recoverPassword,
Expand Down
7 changes: 7 additions & 0 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const OrganController = require("./Controllers/organController");
// --user
routes.get("/users", tokenValidation, UserController.getUsers);
routes.get("/users/:id", tokenValidation, UserController.getUserById);
routes.get("/user", tokenValidation, UserController.getLoggedUser);
routes.patch("/users/patch/:id", tokenValidation, UserController.patchUser);
routes.delete("/users/delete/:id", tokenValidation, UserController.deleteUser);
routes.put("/user", tokenValidation, UserController.update);
Expand All @@ -38,6 +39,12 @@ routes.post("/login", UserController.login);
routes.post("/users/recover-password", UserController.recoverPassword);
routes.post("/verify-token", TokenController.getToken);
routes.patch("/users/change-password/:id", UserController.changePassword);
routes.patch(
"/users/renew-password",
tokenValidation,
UserController.changePasswordInProfile
);

//
routes.post("/membership/create", MembershipForm.createMembershipForm);
routes.get("/membership", MembershipForm.getMembershipForm);
Expand Down

0 comments on commit af6428c

Please sign in to comment.