Skip to content

Commit

Permalink
feat: Update User and CheckPassword End-Point
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcarvalhoj committed Mar 10, 2024
1 parent 7215786 commit ed531aa
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ const bcrypt = require("bcrypt");

const USER_TABLE = "vts-portal-users";

interface Resume {
socialMedia: string[];
articles: string[];
diploma: string[];
skills: string[];
microResume: string;
}

interface User {
id: string;
name: string;
email: string;
password: string;
phone: string;
role: string;
createdAt: string;
avatar: string;
}

// get all
export const getUsers = async () => {
const params = {
Expand Down Expand Up @@ -79,6 +98,61 @@ export const createUser = async (user: {
}
};

// check password
export const checkPassword = async (id: string, password: string) => {
try {
const params = {
TableName: USER_TABLE,
Key: {
id: id,
},
};
const data = await client.get(params).promise();
console.log(data.Item);
if (!data.Item) {
return null;
}
const match: boolean = await bcrypt.compare(password, data.Item.password);
if (match) {
return match;
}
return false;
} catch (error) {
console.error("Error checking user password:", error);
throw error;
}
};

// update all user fields
export const updateUser = async (userId: string, user: User) => {
const params = {
TableName: USER_TABLE,
Key: {
id: userId,
},
UpdateExpression:
"SET #name = :name, email = :email, phone = :phone, avatar = :avatar",
ExpressionAttributeNames: {
"#name": "name",
},
ExpressionAttributeValues: {
":name": user.name,
":email": user.email,
":phone": user.phone,
":avatar": user.avatar,
},
ReturnValues: "ALL_NEW",
};

try {
await client.update(params).promise();
return user;
} catch (error) {
console.error("Error updating user:", error);
throw error;
}
};

// update user email
export const updateUserEmail = async (user: { id: string; email: string }) => {
const params = {
Expand Down
36 changes: 36 additions & 0 deletions src/routes/user.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@ userRouter.get("/", async (req: Request, res: Response) => {
}
});

userRouter.get("/password/:id", async (req: Request, res: Response) => {
try {
const data = await userController.checkPassword(
req.params.id,
req.body.password
);
if (data === null) {
res.status(404).send({ message: "Usuário não encontrado" });
return;
}

if (!data) {
res.status(401).send({ message: "Senha inválida" });
return;
}
res.status(200).send(data);
} catch (error) {
console.error("Error getting user password:", error);
res.status(500).send(error);
}
});

userRouter.put("/update/:id", async (req: Request, res: Response) => {
try {
const data = await userController.updateUser(req.params.id, req.body);
if (!data) {
res.status(404).send({ message: "Usuário não encontrado" });
return;
}
res.status(200).send(data);
} catch (error) {
console.error("Error updating user:", error);
res.status(500).send(error);
}
});

userRouter.post("/login", async (req: Request, res: Response) => {
const { email, password } = req.body;
try {
Expand Down

0 comments on commit ed531aa

Please sign in to comment.