Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
113 lines (102 loc) · 2.8 KB
/
Copy pathapp.js
File metadata and controls
113 lines (102 loc) · 2.8 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
const express = require("express");
const app = express();
var jwt = require("jsonwebtoken");
const jwtkey = require("./config/authconfig");
const static = express.static(__dirname + "/public");
const cors = require("cors");
const configRoutes = require("./routes");
let port = 8080;
var xss = require("xss");
const dataValidation = require("./data/dataValidation");
const whitelist = ["https://betclient.herokuapp.com","http://localhost:3000"]; //Refrence: https://www.codingdeft.com/posts/nodejs-react-cors-error/
const corsOptions = {
origin: function (origin, callback) {
if (!origin || whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error("Not allowed by CORS"));
}
},
credentials: true,
};
app.use(cors(corsOptions));
app.use(express.json());
app.use("/public", static);
app.use(express.urlencoded({ extended: true }));
app.use("/users", (req, res, next) => {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
// verify token
app.use("/user/data", (req, res, next) => {
let token = undefined;
let tokeninbody = undefined;
if(req.headers)token = xss(req.headers["x-access-token"]);
if(req.body.header) tokeninbody = xss(req.body.headers["x-access-token"])
try{
if (token || tokeninbody) {
if (token) {
jwt.verify(token, jwtkey.secret, (err, decoded) => {
if (err) {
throw {
code: 401,
message:"Unauthorized!",
};
}
if (decoded) {
let email = decoded.email;
email = dataValidation.checkEmail(email);
req.userId = email;
console.log("Access Token Verified");
}
next();
});
} else if (tokeninbody) {
jwt.verify(tokeninbody, jwtkey.secret, (err, decoded) => {
if (err) {
throw {
code: 401,
message:"Unauthorized!",
};
}
if (decoded) {
let email = decoded.email;
email = dataValidation.checkEmail(email);
req.userId = email;
console.log("Access Token Verified");
}
next();
});
}
}
else throw {
code: 403,
message:
'AccessToken Not Provided',
};
}
catch(e){
return res.status(e.code).send({ Message:e.message});
}
});
// logging middle ware
app.use(async (req, res, next) => {
let currentTime = new Date().toUTCString();
let method = req.method;
let route = req.originalUrl;
if (req.userId) authenticated = true;
else authenticated = false;
console.log(
`Time: ${currentTime}, Method: ${method}, Route: ${route}, userAuth: ${authenticated}`
);
next();
});
configRoutes(app);
app.listen(process.env.PORT || port, () => {
console.log(`Your routes will be running on port http://localhost/${port}`);
});
try{}
catch(e){}