(req, res)
| 344 | |
| 345 | // Login |
| 346 | const 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 |
| 373 | const searchUser = async (req, res) => { |
nothing calls this directly
no outgoing calls
no test coverage detected