1
|
|
|
import bcrypt from "bcryptjs"; |
2
|
|
|
import jwt from "jsonwebtoken"; |
3
|
|
|
import { db } from "./db.js" |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
// separata modeller för emp och user eftersom |
7
|
|
|
// inloggningen och registereringen fungerar annorlunda |
8
|
|
|
// och checkToken funktionen kommer också skilja sig, |
9
|
|
|
// dels ingen check för roll och dets lyfta ut idt och lägga till på req.body |
10
|
|
|
const emp = { |
11
|
|
|
checkToken: function (req, res, next, acceptableRoles=["admin"]) { |
12
|
|
|
let token = req.headers["x-access-token"]; |
13
|
|
|
|
14
|
|
|
jwt.verify(token, process.env.JWT_SECRET, function (err, decoded) { |
15
|
|
|
if (err) { |
16
|
|
|
return res.status(500).json({ |
17
|
|
|
errors: { |
18
|
|
|
status: 500, |
19
|
|
|
source: "/login", |
20
|
|
|
title: "Failed authentication", |
21
|
|
|
detail: err.message |
22
|
|
|
} |
23
|
|
|
}); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// om inget token är med kommer det att kastas |
27
|
|
|
// ett fel här eftersom decoded inte kommer ha attributet role som alla token |
28
|
|
|
// som tillhör anställda ska ha |
29
|
|
|
if (!acceptableRoles.includes[decoded.role]) { |
30
|
|
|
// if unauthorized request it is safer |
31
|
|
|
// to make it look like the page does not |
32
|
|
|
// exist |
33
|
|
|
return res.status(404).json({ |
34
|
|
|
errors: { |
35
|
|
|
status: 404, |
36
|
|
|
source: req.originalUrl, |
37
|
|
|
title: "Not found", |
38
|
|
|
detail: "Page not found" |
39
|
|
|
} |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
req.emp = { |
44
|
|
|
id: decoded.id, |
45
|
|
|
role: decoded.role |
46
|
|
|
}; |
47
|
|
|
|
48
|
|
|
console.log(req.emp); |
|
|
|
|
49
|
|
|
return next(); |
50
|
|
|
}); |
51
|
|
|
}, |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @description Function that handles admin login |
55
|
|
|
* |
56
|
|
|
* @param {Request} req Request object |
57
|
|
|
* @param {Response} res Response object |
58
|
|
|
* @param {Function} next Next function |
59
|
|
|
* |
60
|
|
|
* @returns {Object} JSON object |
61
|
|
|
*/ |
62
|
|
|
login: async function login(req, res) { |
63
|
|
|
const username = req.body.username; |
64
|
|
|
const password = req.body.password; |
65
|
|
|
|
66
|
|
|
const result = await db.queryWithArgs(`CALL emp_login(?);`, [username]); |
67
|
|
|
const emp = result[0]; |
68
|
|
|
|
69
|
|
|
// om användarnamn saknas kommer |
70
|
|
|
// databasen lyfta ett error |
71
|
|
|
// om lösenord saknas kommer det fångas i bcrypt compare |
72
|
|
|
|
73
|
|
|
return this.comparePasswords(res, password, emp); |
74
|
|
|
}, |
75
|
|
|
/** |
76
|
|
|
* @description Function that compares passwords |
77
|
|
|
* |
78
|
|
|
* @param {Request} req Request object |
79
|
|
|
* @param {String} password Password |
80
|
|
|
* @param {Object} user User |
81
|
|
|
* |
82
|
|
|
* @returns {Object} JSON object |
83
|
|
|
*/ |
84
|
|
|
comparePasswords: function comparePasswords(res, password, emp) { |
85
|
|
|
bcrypt.compare(password, emp.hash, (err, result) => { |
86
|
|
|
if (err) { |
87
|
|
|
return res.status(500).json({ |
88
|
|
|
errors: { |
89
|
|
|
status: 500, |
90
|
|
|
source: "/login", |
91
|
|
|
title: "bcrypt error", |
92
|
|
|
detail: "bcrypt error" |
93
|
|
|
} |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (result) { |
98
|
|
|
const payload = { |
99
|
|
|
id: emp.id, |
100
|
|
|
role: emp.role |
101
|
|
|
}; |
102
|
|
|
const jwtToken = jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: "24h" }); |
103
|
|
|
|
104
|
|
|
return res.json({ |
105
|
|
|
data: { |
106
|
|
|
type: "success", |
107
|
|
|
message: "User logged in", |
108
|
|
|
user: payload, |
109
|
|
|
token: jwtToken |
110
|
|
|
} |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return res.status(401).json({ |
115
|
|
|
errors: { |
116
|
|
|
status: 401, |
117
|
|
|
source: "/login", |
118
|
|
|
title: "Wrong password", |
119
|
|
|
detail: "Password is incorrect." |
120
|
|
|
} |
121
|
|
|
}); |
122
|
|
|
}); |
123
|
|
|
} |
124
|
|
|
}; |
125
|
|
|
|
126
|
|
|
export default emp; |