forked from ALuhning/Space-Gems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalserver.js
76 lines (64 loc) · 1.8 KB
/
localserver.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
require('dotenv').config({ path: './.env.local' })
const express = require('express')
const jwt = require('jsonwebtoken')
const bodyParser = require('body-parser')
const path = require('path')
const cors = require('cors')
const app = express()
app.use(cors({
origin: '*'
}))
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'dist')));
app.post('/appseed', cors(), verifyToken, async (req, res) => {
jwt.verify(req.token, process.env.SECRET_KEY, async (err, authData) => {
if(err) {
res.sendStatus(403);
} else {
const seed = (process.env.APP_SEED).slice(0, 32)
res.json({
seed: seed,
authData
});
}
})
});
app.post('/token', cors(), async (req, res) => {
const accountId = req.body.accountId
if(!accountId) res.sendStatus(403)
jwt.sign({ accountId: accountId }, process.env.SECRET_KEY, (err, token) => {
res.json({
token
})
});
});
app.get('/*', cors(), function (req, res) {
// res.setHeader(
// 'Content-Security-Policy-Report-Only',
// "default-src 'self'; font-src 'self'; img-src 'self'; script-src 'self'; style-src 'self'; frame-src 'self'"
// );
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
// FORMAT OF TOKEN
// Authorization: Bearer <access_token>
// Verify Token
function verifyToken(req, res, next){
// Get auth header value
const bearerHeader = req.headers['authorization'];
// Check if bearer is undefined
if(typeof bearerHeader !== 'undefined'){
// Split at the space
const bearerToken = bearerHeader.split(' ')[1];
// Set the token
req.token = bearerToken;
// Next middleware
next();
} else {
//Forbidden
res.sendStatus(403);
}
}
app.listen(3001, () => {
console.log('running')
});