-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (86 loc) · 3.22 KB
/
index.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import express from "express";
import cors from "cors";
import bodyParser from 'body-parser';
import multer from 'multer';
import path from 'path';
import db from "./config/database.js";
import noticeBoardRoutes from "./routes/noticeRoute.js";
import repairRoutes from "./routes/repairPartsRoute.js";
import machineRoutes from "./routes/machineRoute.js";
import issuesRoutes from "./routes/issueRoute.js";
import plannerRoutes from "./routes/plannerRoute.js";
import employeeRoutes from "./routes/employeeRoute.js";
import departmentRoutes from "./routes/departmentRoute.js";
import positionRoutes from "./routes/jobRoutes.js"
import bcrypt from "bcrypt"
const app = express();
app.use(express.json());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Images folder can access via IP or Localhost
app.use(express.static('Images'))
// Routes
app.use("/hr", noticeBoardRoutes);
app.use("/repairs",repairRoutes)
app.use("/planner",plannerRoutes)
app.use("/issues",issuesRoutes)
app.use("/employees",employeeRoutes)
app.use("/departments",departmentRoutes)
app.use("/machines",machineRoutes)
app.use("/positions",positionRoutes)
//this middleware deal with the auth of the application without hashing
/*
app.post('/login', (req, res) => {
let username = req.body.username;
let password = req.body.password;
const query = `SELECT userid FROM user WHERE username = ? AND password = ?`;
db.query(query, [username, password], (err, results) => {
if (err) {
console.error(err);
return res.status(500).json({ error: 'An error occurred' });
}
if (results.length === 0) {
return res.status(401).json({ error: 'Invalid username or password' });
}
// Login successful
const userId = results[0].userid; // Assuming the user ID column in your database table is named "id"
return res.status(200).json({ message: 'Login successful', userId: userId });
});
});*/
//const bcrypt = require('bcrypt');
app.post('/login', (req, res) => {
let username = req.body.username;
let password = req.body.password;
console.log(username)
console.log(password)
const query = `SELECT userid,phone,username,password FROM user WHERE username = ?`;
db.query(query, [username], async (err, results) => {
if (err) {
console.error(err);
return res.status(500).json({ error: 'An error occurred' });
}
if (results.length === 0) {
return res.status(401).json({ error: 'Invalid username or password' });
}
const hashedPasswordFromDB = results[0].password;
try {
const match = await bcrypt.compare(password, hashedPasswordFromDB);
if (!match) {
return res.status(401).json({ error: 'Invalid username or password' });
}
// Login successful
const userId = results[0].userid;
const phone = results[0].phone;
const username=results[0].username;
return res.status(200).json({ message: 'Login successful', userId: userId,phone:phone ,username:username});
} catch (err) {
console.error(err);
return res.status(500).json({ error: 'An error occurred' });
}
});
});
// Start the server on Port 8800
app.listen(8800, () => {
console.log("SystemCAERUS Backend is started on PORT : 8800");
});