diff --git a/arc3-api/src/app.js b/arc3-api/src/app.js index 865079b..80cbc98 100644 --- a/arc3-api/src/app.js +++ b/arc3-api/src/app.js @@ -56,7 +56,7 @@ app.get('/:guildid/notes/*', authenticated, whitelist, (req, res) => { }) // Authenticate the rest of the client. -app.get('/*', authenticated, (req, res) => { +app.get('/*', (req, res, next) => { const file = req.path.split('/')[1]; if (STATIC_FILES.includes(file)) { @@ -64,7 +64,9 @@ app.get('/*', authenticated, (req, res) => { return; } - res.sendFile('index.html', { root: process.env.BUILD_PATH?? "./build" }); + authenticated(req, res, () => { + res.sendFile('index.html', { root: process.env.BUILD_PATH?? "./build" }); + }); }); diff --git a/arc3-api/src/auth/controllers/authentication.js b/arc3-api/src/auth/controllers/authentication.js index 95c4d3e..53d2133 100644 --- a/arc3-api/src/auth/controllers/authentication.js +++ b/arc3-api/src/auth/controllers/authentication.js @@ -3,13 +3,19 @@ const { Sign, Verify } = require('../../lib/jwt.js'); // Send the login page function LoginRoute(req, res) { - res.redirect("/login"); + const {src} = req.query + if (src) { + res.cookie('src', src) + } + + res.redirect(`/login`); + } function RedirectRoute(req, res) { - // Get the direct url and send them to it - const url = process.env.DIRECT_URL; + let url = process.env.DIRECT_URL; + res.redirect(url); } @@ -23,6 +29,10 @@ async function CallbackRoute(req, res) { return; } + // If there is an src available from this identified user, get it. + + const { src } = req.cookies; + // Extract the code from the query parameters const { code } = req.query; @@ -55,7 +65,10 @@ async function CallbackRoute(req, res) { res.cookie('session', jwt, { maxAge: response.data.expires_in }) - res.redirect('/') + if (src) + res.redirect(src) + else + res.redirect('/') } diff --git a/arc3-api/src/auth/middlewares/authenticated.js b/arc3-api/src/auth/middlewares/authenticated.js index 6bf9029..c410887 100644 --- a/arc3-api/src/auth/middlewares/authenticated.js +++ b/arc3-api/src/auth/middlewares/authenticated.js @@ -18,7 +18,7 @@ async function authenticated(req, res, next) { // If we got no cookies, we can redirect to the login page. if (!token) { - res.redirect('/auth/login'); + res.redirect(`/auth/login?src=${req.originalUrl}`); return; }