-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
157 lines (136 loc) · 3.97 KB
/
server.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import express from 'express'
import fetch from 'node-fetch'
import jwt from 'jwt-simple'
import store from 'store'
const app = express()
const clientsAPI = 'http://www.mocky.io/v2/5808862710000087232b75ac'
const policesAPI = 'http://www.mocky.io/v2/580891a4100000e8242b75c5'
const tokenSecret = 'mysecrettoken'
function createToken (user) {
return jwt.encode(user, tokenSecret)
}
function isAuthenticated (req, res, next) {
let tokenstored = store.get('token')
if (tokenstored) {
return next()
} else {
return res.json({'error': 'You have not permission access to this resource, please login in http://localhost:3001/login/<username>'})
}
}
app.get('/login/:username', (req, res) => {
let username = req.params.username
fetch(clientsAPI)
.then(res => res.json())
.then(data => {
let user = data.clients.find(function (user) {
if (user.name === username && user.role === 'admin') {
var token = createToken(user)
store.set('token', token)
return res.json({'message': 'You have logged in, your user token has been created'})
}
})
if (!user) {
res.json({'error': `This user: <${username}> is not valid`})
}
})
.catch(err => console.error(err))
})
// Get all the list of clients
app.get('/clients', isAuthenticated, (req, res) => {
fetch(clientsAPI)
.then(res => res.json())
.then(data => res.json(data))
})
// Get a single client by cliend id
app.get('/clients/:id', isAuthenticated, (req, res) => {
let id = req.params.id
fetch(clientsAPI)
.then(res => res.json())
.then(data => {
let client = data.clients.find(function (client) {
if (client.id === id) {
return true
}
})
res.json(client)
})
.catch(err => console.error(err))
})
// Get a client by username
app.get('/client/:username', isAuthenticated, (req, res) => {
let username = req.params.username
fetch(clientsAPI)
.then(res => res.json())
.then(data => {
let user = data.clients.find(function (user) {
if (user.name === username) {
return res.json(user)
}
})
if (!user) {
res.json({'error': `It coudn't find <${username}> in our database`})
}
})
.catch(err => console.error(err))
})
// Get all policies related to a username
app.get('/policies/:username', isAuthenticated, (req, res) => {
let username = req.params.username
async function getPolices () {
var users = await fetch(clientsAPI)
.then(res => res.json())
.catch(err => console.error(err))
var data = await fetch(policesAPI)
.then(res => res.json())
.catch(err => console.error(err))
let user = users.clients.find(user => {
if (user.name === username) {
return user
}
})
if (!user) {
return res.json({'error': 'This user does not exists'})
}
let policies = data.policies.filter((policy) => {
if (user.id === policy.clientId) {
return policy
}
})
if (policies.length === 0) {
return res.json({'error': 'It could\'nt found policies for this user'})
} else {
return res.json(policies)
}
}
getPolices()
})
// Get a user related to a policy id
app.get('/policy/:id/user', (req, res) => {
let policyId = req.params.id
async function getUser () {
var data = await fetch(policesAPI)
.then(res => res.json())
.catch(err => console.error(err))
var users = await fetch(clientsAPI)
.then(res => res.json())
.catch(err => console.error(err))
let policy = data.policies.find((policy) => {
if (policy.id === policyId) {
return policy
}
})
if (!policy) {
return res.json({'error': 'This policy does not exist'})
}
users.clients.find((user) => {
if (policy.clientId === user.id) {
return res.json(user)
}
})
}
getUser()
})
const PORT = 3001
app.listen(PORT,
err => err ? new Error(err) : console.info('Server running on port', PORT)
)