-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
79 lines (70 loc) · 2.12 KB
/
auth.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
var firebase = require('./firebase').database();
var bcrypt = require("bcryptjs");
var isAuth = function (req, res, next) {
console.log("Checking auth");
console.log(req.body);
if(req.body.username === null || req.body.username === '' ||
req.body.password === null || req.body.password === '') {
return next({
code: 1,
msg: 'Missing credentials'
});
}
var query = {
username: req.body.username,
password: req.body.password
};
firebase.ref('usernames/' + query.username).once('value', function (snap) {
var data = snap.val();
if(data === undefined || data === null) {
return next({
code: 2,
msg: 'Username not found'
});
}
if(!bcrypt.compareSync(query.password, data.password)) {
return next({
code: 2,
msg: 'Incorrect password'
});
}
firebase.ref('users/' + query.username).once('value', function(snap) {
var name = snap.val().name;
var rooms = snap.val().rooms;
var picture = snap.val().picture;
if(picture === undefined) {
picture = '';
}
var roomInfo = [];
if(rooms !== undefined) {
for(var roomid in rooms) {
if(rooms.hasOwnProperty(roomid)) {
roomInfo.push({
id: roomid,
name: rooms[roomid].name,
balance: rooms[roomid].balance
})
}
}
}
req.user = {
username: query.username,
name: name,
rooms: roomInfo,
picture: picture
};
next();
}, function(error) {
next({
code: 0,
msg: error
});
});
}, function (error) {
next({
code: 0,
msg: error
});
});
};
module.exports = isAuth;