safak / youtube

5.04k stars 5.47k forks source link

wrong password login issue #148

Open Muhammed-Rashid-07 opened 1 year ago

Muhammed-Rashid-07 commented 1 year ago

when a user login with wrong password the server is crashing. Can you assign me to fix this issue.

adarsh2901 commented 1 year ago

the same is happening with me as well!! Did you get how to fix it?

migdalius commented 1 year ago

In api > routes > auth.js

Change login request:

`router.post("/login", async (req, res) => { try { const user = await User.findOne({ username: req.body.username });

const hashedPassword = CryptoJS.AES.decrypt(
  user.password,
  process.env.PASS_SEC
);
const OriginalPassword = hashedPassword.toString(CryptoJS.enc.Utf8);

if (!user || OriginalPassword !== req.body.password) {
  res.status(401).json("Błędny login lub hasło użytkownika");
} else {
  const accessToken = jwt.sign(
    {
      id: user._id,
      isAdmin: user.isAdmin,
    },
    process.env.JWT_SEC,
    { expiresIn: "3d" }
  );

  const { password, ...others } = user._doc;

  res.status(200).json({ ...others, accessToken });
}

} catch (err) { res.status(500).json(err); } }); `

Problem is solve here: https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client

manoj515876 commented 1 year ago

Sanj your assign me this possible. I will fix it.