-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathauthenticate.js
42 lines (35 loc) · 984 Bytes
/
authenticate.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
exports.verifyUser = (req, res, next) => {
console.log(req.signedCookies);
if(!req.signedCookies.user) {
var authHeader = req.headers.authorization;
if(!authHeader) {
var err = new Error('You are not authosrised');
res.setHeader('WWW-Authenticate', 'Basic');
err.status = 401;
return next(err);
}
var auth = new Buffer.from(authHeader.split(' ')[1], 'base64').toString().split(":");
var username = auth[0];
var password = auth[1];
if (username === 'admin' && password === 'password') {
res.cookie('user', 'admin', {signed: true})
next();
}
else {
var err = new Error('password is incorrect');
res.setHeader('WWW-Authenticate', 'Basic');
err.status = 401;
return next(err);
}
}
else {
if(req.signedCookies.user === 'admin') {
next();
}
else {
var err = new Error('cookie is invalid');
err.status = 401;
return next(err);
}
}
}