-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (47 loc) · 1.3 KB
/
index.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
var express = require("express");
var bodyParser = require("body-parser");
var jwt = require("jwt-simple");
var auth = require("./auth.js")();
var users = require("./users.js");
var cfg = require("./config.js");
var app = express();
app.use(bodyParser.json());
app.use(auth.initialize());
app.get("/", function(req, res) {
res.json({
status: "My API is alive!"
});
});
app.get("/user", auth.authenticate(), function(req, res) {
var user = users.find(function(u) {
return u.id === req.user.id
}) || null;
res.json(user);
});
app.post("/token", function(req, res) {
if (req.body.email && req.body.password) {
var email = req.body.email;
var password = req.body.password;
var user = users.find(function(u) {
return u.email === email && u.password === password;
});
if (user) {
var payload = {
id: user.id
};
console.log("uid" + user.id);
var token = jwt.encode(payload, cfg.jwtSecret);
res.json({
token: token
});
} else {
res.sendStatus(401);
}
} else {
res.sendStatus(401);
}
});
app.listen(3000, function() {
console.log("My API is running...");
});
module.exports = app;