-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
258 lines (225 loc) · 6.24 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
let express = require("express");
let app = express();
require("colors");
require("dotenv").config();
var bodyParser = require("body-parser");
var jwt = require("jsonwebtoken");
var bcrypt = require("bcryptjs");
let DbService = require("./services/db.service");
let Observable = require("rxjs/Observable").Observable;
require("rxjs/add/operator/map");
require("rxjs/add/observable/fromPromise");
let dbService = new DbService();
const PORT = process.env.PORT || 8889;
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Credentials", "true");
res.setHeader(
"Access-Control-Allow-Methods",
"GET,HEAD,OPTIONS,POST,PUT,DELETE"
);
res.setHeader(
"Access-Control-Allow-Headers",
"Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization"
);
next();
});
function authorization(req) {
if (req.headers && req.headers.authorization) {
var authorization = req.headers.authorization.split(" ")[1];
var decoded = jwt.verify(authorization, "secret");
var userId = decoded.id;
return userId;
} else {
return null;
}
}
const auth = (req, res, next) => {
const userId = authorization(req);
if (userId !== null) {
req.userId = userId;
next();
} else {
res
.status(400)
.json({ message: "Invalid auth token provided." })
.end();
}
};
app.get("/api/verify-token", (req, res) => {
const userId = authorization(req);
let inputParams = {
p: userId
};
if (!userId) res.send({});
dbService.execute("selectUsers", inputParams).subscribe(q => {
if (q && q.recordsets && q.recordsets[0] && q.recordsets[0][0]) {
res.send({ user: q.recordsets[0][0] });
} else {
res.send({});
}
});
});
app.post("/api/posts", auth, (req, res) => {
let userId = req.userId;
if (!userId) {
res.send({ status: null });
} else {
let { title, content } = req.body;
let inputParams = {
pTitle: title,
pContent: content,
pUserId: userId
};
let outputParams = {
status: null
};
dbService
.execute("createPost", inputParams, outputParams)
.subscribe(query => {
if (Number(query.output.status) === 1) {
dbService
.query(`select top 1 * from POSTS order by id desc`)
.subscribe(query => {
res.send({ post: query[0][0] });
});
} else {
res.send(query);
}
});
}
});
app.put("/api/posts", (req, res) => {
let userId = authorization(req);
if (!userId) {
res.send({ status: null });
} else {
let { title, content, id } = req.body;
dbService
.query(
`Update POSTS SET title = '${title}', content = '${content}', created_at = GETDATE() where userId = '${userId}' and Id = '${id}'`
)
.subscribe(query => {
res.send({ status: 1 });
});
}
});
app.get("/api/getallusers", (req, res) => {
dbService.query(`Select login from Users`).subscribe(query => {
res.send({ users: query });
});
});
app.get("/api/posts", (req, res) => {
let { userId } = req.query;
dbService
.query(
`Select id, title, content, created_at from POSTS where userId = '${userId}'`
)
.subscribe(query => {
res.send(query);
});
});
app.get("/api/getallposts", (req, res) => {
dbService.query(`select * from postParams()`).subscribe(query => {
res.send({ posts: query });
});
});
app.delete("/api/posts", (req, res) => {
let userId = authorization(req);
let { postId } = req.query;
if (!userId) {
res.send({ status: null });
} else {
dbService
.query(
`Delete from POSTS where Id = '${postId}' and userId = '${userId}'`
)
.subscribe(query => {
res.send({ status: 1 });
});
}
});
app.get("/api/users", (req, res) => {
dbService.query(`SELECT * FROM UsersParams()`).subscribe(query => {
res.send(query);
});
});
app.get("/api/search-users", (req, res) => {
let { fullMatch, login } = req.query;
if (fullMatch === "true") {
dbService
.query(`SELECT * FROM findFullMatchUser('${login}')`)
.subscribe(query => {
res.send(query);
});
} else {
let inputParams = {
pLogin: login
};
dbService.execute("getAllMatchingUser", inputParams).subscribe(query => {
res.send(query.recordsets);
});
}
});
app.post("/api/users", (req, res) => {
let { login, password } = req.body;
let inputParams = {
pLogin: login,
pPassword: password
};
let outputParams = {
status: null
};
dbService
.execute("createUser", inputParams, outputParams)
.subscribe(query => {
if (Number(query.output.status) === 1) {
dbService
.query(`SELECT * FROM findFullMatchUser('${login}')`)
.subscribe(q => {
const user = q[0][0];
var token = jwt.sign({ id: user.id, login: user.login }, "secret", {
expiresIn: 86400 // expires in 24 hours
});
res.send({ output: { status: 1 }, user: { ...user, token } });
});
} else {
res.send(query);
}
});
});
app.post("/api/authenticate", (req, res) => {
let { login, password } = req.body;
let inputParams = {
pLogin: login,
pPassword: password
};
dbService.execute("loginUser", inputParams).subscribe(query => {
let userId = query && query.returnValue;
if (!userId) {
res.send({ currentUser: null });
} else {
dbService
.query(`SELECT id, login FROM USERS where Id = '${userId}'`)
.subscribe(query => {
let user = query && query[0] && query[0][0];
if (!user) {
res.send({ currentUser: null });
}
var token = jwt.sign({ id: user.id, login: user.login }, "secret", {
expiresIn: 86400 // expires in 24 hours
});
res.send({
currentUser: { login: user.login, id: user.id, token: token }
});
});
}
});
});
app.listen(PORT, () => {
dbService.init().subscribe(() => {
console.log("successful connecting to database...".magenta);
});
});