diff --git a/bin/web/routes/api.js b/bin/web/routes/api.js index 8256f7c..5eb99a4 100644 --- a/bin/web/routes/api.js +++ b/bin/web/routes/api.js @@ -162,8 +162,8 @@ getRoutes = async () => { }); // database error - if(authCode.err) { - global['logs'].debug(authCode[1]); + if(typeof authCode.err !== "undefined") { + global['logs'].debug(authCode.err); return res.type('json').status(500).end(JSON.stringify({ status: 500, message: [ @@ -171,11 +171,11 @@ getRoutes = async () => { ] })); } - else if(rep) { + else if(typeof authCode.reply !== "undefined") { // retrieve apps apps = await db.getApps(); // database error - if(apps.reply) { + if(typeof apps.err !== "undefined") { global['logs'].debug(apps.err); return res.type('json').status(500).end(JSON.stringify({ status: 500, @@ -189,7 +189,7 @@ getRoutes = async () => { // if app.id is equal to queried app if(app.id == req.query.id) { // redirect to app - return res.redirect(app.access+"?uid="+req.session.user.id+"&token="+rep.token); + return res.redirect(app.access+"?uid="+req.session.user.id+"&token="+authCode.reply.token); } }); } else { diff --git a/bin/web/routes/static.js b/bin/web/routes/static.js index 0e4de69..e59090e 100644 --- a/bin/web/routes/static.js +++ b/bin/web/routes/static.js @@ -11,7 +11,7 @@ asyncer = require('express-async-handler'); fs = require('fs'); path = require('path'); - +var cfg = require(global['__dirname']+'/bin/config'); // reduce IO file checks - save file state in cache var fileCheck = (file) => { @@ -48,7 +48,8 @@ let getRoutes = async () => { apps = await db.getApps(); res.render('index', { session: req.session, - apps: apps.reply + apps: apps.reply, + cfg: cfg }); })); @@ -85,7 +86,8 @@ let getRoutes = async () => { res.render('request', { session: req.session, appRequest: req.session.appRequest, - apps: apps.reply + apps: apps.reply, + cfg: cfg }); // if user isnt logged in, show login page } else { @@ -110,7 +112,7 @@ let getRoutes = async () => { * @method all * @TODO comments */ - route.all('/*', (req, res, next) => { + route.all('/*', asyncer(async (req, res, next) => { // passthrough to next route if(req.path.startsWith('/api')) return next(); @@ -135,22 +137,26 @@ let getRoutes = async () => { return res.status(404).render('error/404', { error_code: 404, error_msg: 'msg.request.file.not_found', - session: req.session + session: req.session, + cfg: cfg }); } else if(rule.type == "missing_permission") { return res.status(401).render('error/permission', { error_code: 401, - session: req.session + session: req.session, + cfg: cfg }); } else if(rule.type == "login") { return res.status(401).render('error/login', { error_code: 401, - session: req.session + session: req.session, + cfg: cfg }); } else { return res.status(401).render('error/error', { error_code: 401, - session: req.session + session: req.session, + cfg: cfg }); } } @@ -159,23 +165,28 @@ let getRoutes = async () => { }); if(fileCheck(req.path)) { + // query apps + apps = await db.getApps(); + return res.render(req.path.replace(/^\//, ''), { session: req.session, - cfg: global['gds'].cfg + apps: apps.reply, + cfg: cfg }); } else { global['logs'].info("[web] (404) path not found: "+req.path); return res.status(404).render('error/404', { error_code: 404, error_msg: 'msg.request.file.not_found', - session: req.session + session: req.session, + cfg: cfg }); } // TODO: try to login // TODO: role-based authorization // TODO: show login page or page - }); + })); return route; };