70 lines
1.9 KiB
JavaScript
70 lines
1.9 KiB
JavaScript
config = require("./config.js")
|
|
register = require("./register.js")
|
|
login = require("./login.js")
|
|
txHandler = require("./txHandler.js")
|
|
require("./peroidicActions/checkPayment.js")
|
|
require("./peroidicActions/pruneUnpaidAccounts.js")
|
|
|
|
express = require('express')
|
|
jwt = require('jsonwebtoken')
|
|
bodyParser = require('body-parser')
|
|
javalon = require("javalon")
|
|
|
|
javalon.init({api: config.avalonNode.slice(0, -1)})
|
|
|
|
app = express()
|
|
app.use(bodyParser.json(),function (req, res, next) {
|
|
res.setHeader('Access-Control-Allow-Origin', '*')
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST')
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization')
|
|
|
|
res.setHeader('Content-Type', 'application/json')
|
|
res.setHeader('Access-Control-Allow-Credentials', true)
|
|
next()
|
|
})
|
|
|
|
const authenticateJWT = (req, res, next) => {
|
|
const authHeader = req.headers.authorization;
|
|
|
|
if (authHeader) {
|
|
const token = authHeader.split(' ')[1];
|
|
jwt.verify(token, config.jwtSecret, (err, user) => {
|
|
if (err) {
|
|
console.log(err)
|
|
return res.send({"Error": "Auth Failure"})
|
|
}
|
|
|
|
req.user = user
|
|
next();
|
|
});
|
|
} else {
|
|
res.redirect(401, "/");
|
|
}
|
|
}
|
|
|
|
app.post('/register', function (req, res) {
|
|
register(req.body.username, req.body.password).then(x => {
|
|
res.send({"Message": x})
|
|
}).catch( e => {
|
|
res.send({"Error": e})
|
|
})
|
|
})
|
|
|
|
app.post('/login', function (req, res) {
|
|
login(req.body.username, req.body.password).then(x => {
|
|
res.send({"accessToken":x})
|
|
}).catch( e => {
|
|
res.send({"Message": e})
|
|
})
|
|
})
|
|
|
|
app.post('/submit',authenticateJWT, function (req, res) {
|
|
txHandler(req.body.tx, req.user).then(x => {
|
|
res.send({"Message": x})
|
|
}).catch( e => {
|
|
res.send({"Message": e})
|
|
})
|
|
})
|
|
|
|
console.log("Up on: "+config.expressPort)
|
|
app.listen(config.expressPort) |