请检查我的通过电子邮件和密码进行的验证?

huangapple go评论125阅读模式
英文:

please check my verification using email and password?

问题

I'll provide translations for the code you shared:

  1. 现在我正在尝试在我的类别控制器中检查用户是否已登录如果已登录则允许他发布类别否则显示您必须登录才能创建类别”,但不管用户是否已登录类别都会被发布这让我困扰了4个小时因为我还是初学者
  2. 这是问题所在的地方它绕过了
  3. 路由部分
  4. routeCategory.post("/category", (req, res) => {
  5. if (!req.body || !req.body.category_id) {
  6. return res.status(400).send("类别丢失");
  7. }
  8. const id = req.body.category_id;
  9. const one = category.categoryData.find(
  10. (Element) => Element.category_id === id
  11. );
  12. const email2 = req.params.email;
  13. const found = User.data.find((email) => email.email === email2);
  14. if (User.login(email2, req.body.password) == false) {
  15. return res.status(401).send("您未经授权");
  16. } else if (one) {
  17. return res.status(409).send("类别已存在");
  18. } else {
  19. const newCategory = {
  20. category_id: req.body.category_id,
  21. categoryName: req.body.categoryName,
  22. };
  23. category.createCategory(newCategory);
  24. return res.sendStatus(201);
  25. }
  26. });
  27. 登录部分
  28. static login = function (email, password) {
  29. // 检查数据数组中是否存在与输入的电子邮件和密码相同的用户
  30. // 如果是,则返回电子邮件
  31. const found = User.data.find((element) => {
  32. return element.email === email && element.password === password;
  33. });
  34. console.log(User.data);
  35. console.log(email, password);
  36. if (found) {
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. };
  42. }

只提供了代码的翻译部分,没有其他内容。

英文:

now im trying to check in my category controller if user logged in let him post category otherwise show him "You must be logged in to create a category" but the category is posted wether user is logged in or not logged in it caused me frustration for 4 hours still beginner

this where the problem it bypass

  1. routeCategory.post("/category", (req, res) => {
  2. if (!req.body || !req.body.category_id) {
  3. return res.status(400).send("category missing");
  4. }
  5. const id = req.body.category_id;
  6. const one = category.categoryData.find(
  7. (Element) => Element.category_id === id
  8. );
  9. const email2 = req.params.email;
  10. const found = User.data.find((email) => email.email === email2);
  11. if (User.login(email2, req.body.password) == false) {
  12. return res.status(401).send("You are not authorized");
  13. } else if (one) {
  14. return res.status(409).send("Category already exists");
  15. } else {
  16. const newCategory = {
  17. category_id: req.body.category_id,
  18. categoryName: req.body.categoryName,
  19. };
  20. category.createCategory(newCategory);
  21. return res.sendStatus(201);
  22. }
  23. });
  1. static login = function (email, password) {
  2. // checking if the email and password are in the data array
  3. // then the email and password must be the same as the user entered it
  4. // return email if it's right
  5. // const found = User.data.find(
  6. // (element) => element.email === email && element.password === password
  7. // );
  8. const found = User.data.find((element) => {
  9. return element.email === email && element.password === password;
  10. });
  11. console.log(User.data);
  12. console.log(email, password);
  13. if (found) {
  14. return true;
  15. } else {
  16. return false;
  17. }
  18. };
  19. }

答案1

得分: 0

控制访问应该通过单独的中间件来完成:

  1. const checkAccess = (req, res, next) => {
  2. // 获取用户并检查要求
  3. if (/* 用户可以访问路由 */) {
  4. next()
  5. } else {
  6. next(new Error("无访问权限"))
  7. }
  8. }
  9. routeCategory.post("/category", checkAccess, (req, res) => {
  10. if (!req.body || !req.body.category_id) {
  11. ....

此外,应该以一种无法使用 === 进行检查的方式存储密码,可以使用 bcryptargon2 进行密码哈希化。

英文:

Controlling access like this should be done via a separate middleware:

  1. const checkAccess = (req, res, next) => {
  2. // fetch user and check requirements
  3. if (/* user can access route*/) {
  4. next()
  5. } else {
  6. next(new Error("No access"))
  7. }
  8. }
  9. routeCategory.post("/category", checkAccess, (req, res) => {
  10. if (!req.body || !req.body.category_id) {
  11. ....

Also you should be storing the password in a way that checking it with === isn't possible, use bcrypt or argon2 for password hashing.

huangapple
  • 本文由 发表于 2023年6月26日 23:17:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76558036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定