-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.js
50 lines (43 loc) · 1.2 KB
/
users.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var express = require("express");
var router = express.Router();
const databaseClient = require("/workspace/backend/database");
const jwt = require("jsonwebtoken");
/* GET users listing. */
router.get("/", function (req, res, next) {
res.send("test123");
});
router.post("/login", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
try {
console.log(`awaiting db...`)
const data = await databaseClient.query(
`SELECT * FROM users WHERE username = $1 AND password = $2`,
[username, password]
); //Verifying if the user exists in the database
console.log(`finished awaiting db`)
const userData = data.rows;
if (userData.length === 0) {
res.status(400).json({
error: "User is not registered",
});
return
}
const token = jwt.sign(
{
username: username,
},
process.env.ACCESS_TOKEN
);
res.status(200).json({
message: "User signed in!",
token: token,
});
} catch (error) {
console.log(error);
res.status(500).json({
error: "Database error occurred while signing in!", //Database connection error
});
}
});
module.exports = router;