MCPcopy Create free account
hub / github.com/Muhammadsiddiq-code/backend-group-lessons / postLogin

Function postLogin

controller/userController.js:346–370  ·  view source on GitHub ↗
(req, res)

Source from the content-addressed store, hash-verified

344
345// Login
346const postLogin = async (req, res) => {
347 try {
348 const { username, password } = req.body;
349
350 const user = await User.findOne({ username });
351 if (!user) {
352 return res
353 .status(401)
354 .json({ success: false, message: "Invalid credentials" });
355 }
356
357 const passwordMatch = await bcrypt.compare(password, user.password);
358 if (!passwordMatch) {
359 return res
360 .status(401)
361 .json({ success: false, message: "Username or password is invalid" });
362 }
363
364 const token = jwt.sign({ username: user.username }, "Secret");
365 return res.json({ success: true, token: token });
366 } catch (error) {
367 console.error(error, "error");
368 return res.status(500).json({ success: false, message: "Server Error" });
369 }
370};
371
372// Search user
373const searchUser = async (req, res) => {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected