forked from totaljs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorization.js
More file actions
37 lines (31 loc) · 798 Bytes
/
authorization.js
File metadata and controls
37 lines (31 loc) · 798 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
29
30
31
32
33
34
35
36
37
// ================================================
// AUTHORIZATION
// ================================================
F.onAuthorize = function(req, res, flags, callback) {
var cookie = req.cookie(F.config.cookie);
if (!cookie || cookie.length < 10) {
callback(false);
return;
}
var obj = F.decrypt(cookie, 'user');
if (!obj || obj.ip !== req.ip) {
callback(false);
return;
}
var user = F.cache.read('user_' + obj.id);
if (user) {
req.user = user;
callback(true);
return;
}
NOSQL('users').find().make(function(builder) {
builder.where('id', obj.id);
builder.first();
builder.callback(function(err, response) {
if (!response)
return callback(false);
F.cache.add('user_' + response.id, response, '5 minutes');
callback(true, response);
});
});
};