-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticate.js
More file actions
28 lines (25 loc) · 781 Bytes
/
authenticate.js
File metadata and controls
28 lines (25 loc) · 781 Bytes
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
//var config = require('./config.js');
var JWT = require('./JWT.js');
module.exports = function(req,res, next){
var authHeader = req.headers.authorization;
if (!authHeader){
err = new Error('You are not authenticated.');
err.status = 401;
res.status(401).end(err.message);
}
else{
var auth = new Buffer(authHeader.split(' ')[1], 'base64').toString().split(':');
var user = auth[0];
var password = auth[1];
if (user == 'admin' && password == 'root'){
var user = { id : 'admin', password: 'root'};
var token = JWT.getToken(user);
res.status(200).json({ status : 'Login successful', token : token});
}
else{
err = new Error('Wrong User name or password');
err.status = 401;
res.status(401).end(err.message);
}
}
}