-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
57 lines (45 loc) · 1.57 KB
/
app.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
const credentials = {secretUser:"user" , secretPassword:"password"}
const cors = require("cors")
const express = require("express")
const bodyParser = require('body-parser')
const jwt = require('jsonwebtoken');
const app = express()
const PORT = process.env.PORT || 3000
app.use(function (req, res, next) {
res.setHeader('Content-Security-Policy', "default-src 'self'; font-src 'self'; img-src 'self'; script-src 'self'; style-src 'self'; frame-src 'self'");
next();
});
app.use('/healthcheck', require('./routes/healthcheck.routes'));
app.use(express.urlencoded({ extended: true }));
app.use(cors())
app.get("/", (req ,res)=>{
headers={"http_status":200, "cache-control": "no-cache"}
body={"status": "available"}
res.status(200).send(body)
})
app.get("/health", (req ,res)=>{
headers={"http_status":200, "cache-control": "no-cache"}
body={"status": "available"}
res.status(200).send(body)
})
app.post('/authorize', (req, res) => {
// Insert Login Code Here
let user = req.body.user;
let password = req.body.password;
console.log(`User ${user}`)
console.log(`Password ${password}`)
if(user===credentials.secretUser && password===credentials.secretPassword){
console.log("Authorized")
const token = jwt.sign({
data: 'foobar'
}, 'your-secret-key-here', { expiresIn: 60 * 60 });
console.log(token)
res.status(200).send(token)
}else{
console.log("Not authorized")
res.status(200).send({"STATUS":"FAILURE"})
}
});
app.listen(PORT , ()=>{
console.log(`STARTED LISTENING ON PORT ${PORT}`)
});