-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
65 lines (56 loc) · 1.34 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
55
56
57
58
59
60
61
62
63
64
65
const express = require('express');
const cors = require('cors');
const expressJwt = require('express-jwt');
const bodyParser = require('body-parser');
const uuidv1 = require('uuid/v1');
const jwt = require('jsonwebtoken');
const app = express();
const jwtMiddleWare = expressJwt({ secret: process.env.JWT_SECRET })
app.use(cors());
app.use(bodyParser.json())
let feeds = [
{
id: uuidv1(),
description: 'express-jwt can be handy',
author: 'Ab'
},
{
id: uuidv1(),
description: 'another way to get jwt is using passport-jwt',
author: 'Xy'
}
]
app.post('/login', (req, res) => {
const loginInfo = req.body
if (!(loginInfo.userName !== '' && loginInfo.password === 'pass')) {
throw new Error('not valid user id or password!');
}
const user = {
name: loginInfo.userName
};
const token = jwt.sign(user, process.env.JWT_SECRET, {
expiresIn: 60 * 2
});
res.send({
token
});
});
app.get('/isloggedin', jwtMiddleWare, (req, res) => {
res.sendStatus(200);
})
// NOTE - usage of jwtMiddleWare for the secured route
app.post('/feed', jwtMiddleWare, (req, res) => {
const feed = req.body
feeds.push({
...feed,
id: uuidv1()
})
res.sendStatus(200)
});
app.get('/feeds', jwtMiddleWare, (req, res) => {
console.log(' in get feeds ')
res.send(feeds);
});
app.listen(8080, function () {
console.log('server listening on port 8080.....')
});