-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
252 lines (199 loc) · 5.69 KB
/
app.js
File metadata and controls
252 lines (199 loc) · 5.69 KB
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
const express = require("express");
const app = express();
var bodyParser = require('body-parser');
const apirouter = express.Router();
const cookieParser = require('cookie-parser');
const jwt=require('jsonwebtoken');
const userData=require('./dummyData.js');
const token_key = process.env.TOKEN_KEY;
// for serving the frontend
app.use("/static",express.static('public'))
// for reading/parsing the form data.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.get("/",(req,res)=>{
const token = req.cookies['login_email'];
if (!token) {
return res.status(200).sendFile(__dirname+'/pages/index.html');
}
try {
const decoded = jwt.verify(token, token_key);
const user_email = decoded.user_email;
let found=false;
userData.forEach((dt)=>{
if(dt.email==user_email)
found=true;
})
if(found)
return res.status(200).sendFile(__dirname+'/pages/dashboard.html');
} catch (err) {
console.log(err);
return res.status(500).json("error on server");
}
return res.status(200).sendFile(__dirname+'/pages/index.html');
})
app.get("/getIdea",(req,res)=>{
return res.status(200).sendFile(__dirname+'/pages/addIdea.html');
})
app.get("/profile",(req,res)=>{
return res.status(200).sendFile(__dirname+'/pages/profile.html');
})
app.get("/docs",(req,res)=>{
return res.status(200).sendFile(__dirname+'/pages/docs.html');
})
apirouter
.get('/ideas',(req,res)=>{
let data=[];
userData.forEach((dt)=>{
dt.ideas.forEach((ide)=>{
data.push( {
name:ide.name,
domain:ide.domain,
idea:ide.idea,
owned:dt.name,
email:dt.email
})
})
})
// console.log(data);
return res.status(200).json(data);
})
.post('/idea',(req,res)=>{
const {name,domain,idea}=req.body;
if(!name || !domain || !idea)
return res.status(400).json("data missing");
const token = req.cookies['login_email'];
if(!token)
return res.status(400).json("not logged in");
try {
const decoded = jwt.verify(token, token_key);
const user_email = decoded.user_email;
let found=false;
userData.forEach((dt)=>{
if(dt.email==user_email)
{
dt.ideas.push({
name,
domain,
idea
})
found=true;
}
})
if(found)
{
// return res.status(200).json('idea added');
return res.status(200).redirect("/");
}
} catch (err) {
console.log(err);
return res.status(500).json("error on server");
}
return res.status(400).json("invalid user");
})
.get('/profile',(req,res)=>{
const token = req.cookies['login_email'];
if(!token)
return res.status(400).json("not logged in");
try {
const decoded = jwt.verify(token, token_key);
const user_email = decoded.user_email;
let data;
let found=false;
userData.forEach((dt)=>{
if(dt.email==user_email)
{
found=true;
data=dt;
}
})
if(found)
return res.status(200).json(data);
return res.status(400).json("invalid user");
} catch (err) {
console.log(err);
return res.status(500).json("error on server");
}
})
.post('/burstIdea',(req,res)=>{
const {index}=req.body;
// console.log(req.body);
if(!index)
return res.status(400).json("invalid operation")
const token = req.cookies['login_email'];
if(!token)
return res.status(400).json("not logged in");
try {
const decoded = jwt.verify(token, token_key);
const user_email = decoded.user_email;
let data;
let found=false;
userData.forEach((dt)=>{
if(dt.email==user_email)
{
if(index<dt.ideas.length)
{
dt.ideas.splice(index,1);
found=true;
}
}
})
if(found)
{
return res.status(200).json("idea trashed");
}
return res.status(400).json("invalid user");
} catch (err) {
console.log(err);
return res.status(500).json("error on server");
}
})
app.use('/api',apirouter);
app.post('/login',(req,res)=>{
const {email,password}=req.body;
if(!email || !password)
return res.status(400).json("email or password missing");
let found=false;
userData.forEach((dt)=>{
if(dt.email==email && dt.password==password)
found=true;
})
if(found)
{
const token = jwt.sign(
{ user_email: email },
process.env.TOKEN_KEY
);
return res.status(200).cookie('login_email',token).redirect('/');
}
return res.status(400).json('invalid credentials');
});
app.post('/register',(req,res)=>{
const {name,email,password,checkpassword}=req.body;
if(!name || !email || !password || !checkpassword)
return res.status(400).json("data missing");
if(password!=checkpassword)
return res.status(400).json("passwords do not match");
let found=false;
userData.forEach((dt)=>{
if(dt.email==email)
found=true;
})
if(found)
{
return res.status(400).json("a user exists with this email")
}
userData.push({
name,
email,
password,
ideas:[]
})
const token = jwt.sign(
{ user_email: email },
process.env.TOKEN_KEY
);
return res.status(200).cookie('login_email',token).redirect('/');
})
module.exports = app;