-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.js
43 lines (41 loc) · 1.13 KB
/
user.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
var meta = require('./meta'),
uuid = require('node-uuid');
exports.get = function(req, res) {
var userId = req.cookies[meta.userId];
req.store.getUser(userId, function(user) {
if(user) {
res.json(200, user);
}
else {
res.clearCookie(meta.userId);
res.json(404, {
error: "user not found"
});
}
});
};
exports.post = function(req, res) {
var userId = req.cookies[meta.userId];
req.store.getUser(userId, function(name) {
if(name) {
res.json(400, {
error: "user already exists, clear out the current user"
});
}
else {
var name = req.body.name;
if(name) {
var userId = uuid.v4();
res.cookie(meta.userId, userId);
req.store.setUser(userId, name, function() {
res.json(200, {});
});
}
else {
res.json(400, {
error: "please send a name in the post body"
});
}
}
});
};