|
|
@@ -22,15 +22,15 @@ app.all('/register', function(req, res) {
|
|
22
|
22
|
redis.get(uquery).then(function (result) {
|
|
23
|
23
|
if (result !== "" && result !== undefined && result !== null) {
|
|
24
|
24
|
res.send({"registered": 0,
|
|
25
|
|
- "error": 1});
|
|
|
25
|
+ "error": 1});
|
|
26
|
26
|
} else {
|
|
27
|
27
|
bcrypt.hash(tmp_password, null, null, function (err, hash) {
|
|
28
|
28
|
user_object["password"] = hash;
|
|
29
|
29
|
user_object["auth-key"] = bcrypt.hashSync(Date.now().toString() + tmp_username);
|
|
30
|
30
|
redis.set(uquery, JSON.stringify(user_object));
|
|
31
|
31
|
res.send({"registered": 1,
|
|
32
|
|
- "auth-key": user_object["auth-key"],
|
|
33
|
|
- "error": 0});
|
|
|
32
|
+ "auth-key": user_object["auth-key"],
|
|
|
33
|
+ "error": 0});
|
|
34
|
34
|
});
|
|
35
|
35
|
}
|
|
36
|
36
|
});
|
|
|
@@ -39,36 +39,52 @@ app.all('/register', function(req, res) {
|
|
39
|
39
|
app.all('/login', function(req, res) {
|
|
40
|
40
|
var username = req.body.user || req.query.user;
|
|
41
|
41
|
var password = req.body.password || req.query.password;
|
|
|
42
|
+ var auth_key = req.body.auth_key || req.query.auth_key;
|
|
42
|
43
|
var uquery = 'user:' + username;
|
|
43
|
44
|
|
|
44
|
45
|
redis.get(uquery).then(function (result) {
|
|
45
|
46
|
if (result !== "" && result !== undefined && result !== null) {
|
|
46
|
47
|
var user_object = JSON.parse(result);
|
|
47
|
|
- bcrypt.compare(password, user_object["password"], function (err, matched) {
|
|
48
|
|
- if (matched === true) {
|
|
49
|
|
- if (undefined === user_object["auth-key"]) {
|
|
50
|
|
- var timestamp_user = Date.now().toString() + username;
|
|
51
|
|
- user_object["auth-key"] = bcrypt.hashSync(timestamp_user);
|
|
52
|
|
- redis.set(uquery, JSON.stringify(user_object));
|
|
53
|
|
- res.send({"logged_in": 1,
|
|
54
|
|
- "auth-key": user_object["auth-key"],
|
|
55
|
|
- "error": 0});
|
|
56
|
|
- } else {
|
|
57
|
|
- res.send({"logged_in": 1,
|
|
58
|
|
- "auth-key": user_object["auth-key"],
|
|
59
|
|
- "error": 0});
|
|
60
|
|
- }
|
|
61
|
|
- return;
|
|
|
48
|
+ if (auth_key !== "" && auth_key !== undefined && auth_key !== null) {
|
|
|
49
|
+ if (auth_key === user_object["auth-key"]) {
|
|
|
50
|
+ var timestamp_user = Date.now().toString() + username;
|
|
|
51
|
+ user_object["auth-key"] = bcrypt.hashSync(timestamp_user);
|
|
|
52
|
+ redis.set(uquery, JSON.stringify(user_object));
|
|
|
53
|
+ res.send({"logged_in": 1,
|
|
|
54
|
+ "auth-key": user_object["auth-key"],
|
|
|
55
|
+ "error": 0});
|
|
62
|
56
|
} else {
|
|
63
|
57
|
res.send({"logged_in": 0,
|
|
64
|
|
- "error": 2});
|
|
65
|
|
- return;
|
|
|
58
|
+ "error": 3});
|
|
66
|
59
|
}
|
|
67
|
|
- });
|
|
|
60
|
+ } else {
|
|
|
61
|
+ bcrypt.compare(password, user_object["password"], function (err, matched) {
|
|
|
62
|
+ if (matched === true) {
|
|
|
63
|
+ if (undefined === user_object["auth-key"]) {
|
|
|
64
|
+ var timestamp_user = Date.now().toString() + username;
|
|
|
65
|
+ user_object["auth-key"] = bcrypt.hashSync(timestamp_user);
|
|
|
66
|
+ redis.set(uquery, JSON.stringify(user_object));
|
|
|
67
|
+ res.send({"logged_in": 1,
|
|
|
68
|
+ "auth-key": user_object["auth-key"],
|
|
|
69
|
+ "error": 0});
|
|
|
70
|
+ } else {
|
|
|
71
|
+ res.send({"logged_in": 1,
|
|
|
72
|
+ "auth-key": user_object["auth-key"],
|
|
|
73
|
+ "error": 0});
|
|
|
74
|
+ }
|
|
|
75
|
+ return;
|
|
|
76
|
+ } else {
|
|
|
77
|
+ res.send({"logged_in": 0,
|
|
|
78
|
+ "error": 2});
|
|
|
79
|
+ return;
|
|
|
80
|
+ }
|
|
|
81
|
+ });
|
|
|
82
|
+
|
|
|
83
|
+ }
|
|
68
|
84
|
} else {
|
|
69
|
85
|
res.send({"logged_in": 0,
|
|
70
|
|
- "error": 1});
|
|
71
|
|
- return;
|
|
|
86
|
+ "error": 1});
|
|
|
87
|
+ return;
|
|
72
|
88
|
}
|
|
73
|
89
|
});
|
|
74
|
90
|
});
|
|
|
@@ -78,6 +94,6 @@ app.listen(3000, function () {
|
|
78
|
94
|
});
|
|
79
|
95
|
|
|
80
|
96
|
process.on('SIGINT', function() {
|
|
81
|
|
- console.log( "\nRecieved Ctrl-C, shutting down." );
|
|
82
|
|
- process.exit(0);
|
|
|
97
|
+ console.log( "\nRecieved Ctrl-C, shutting down." );
|
|
|
98
|
+ process.exit(0);
|
|
83
|
99
|
})
|