From 5f17fa67cbe13442fd5f5244b37b9cf07c7138b7 Mon Sep 17 00:00:00 2001 From: dblythy Date: Wed, 25 Jan 2023 15:08:19 +1100 Subject: [PATCH] feat: add url redirecting when authentication is needed --- Parse-Dashboard/Authentication.js | 16 +++++++++++----- Parse-Dashboard/app.js | 5 +++-- src/login/Login.js | 10 +++++++++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Parse-Dashboard/Authentication.js b/Parse-Dashboard/Authentication.js index 0a6beee442..bfa857cae8 100644 --- a/Parse-Dashboard/Authentication.js +++ b/Parse-Dashboard/Authentication.js @@ -68,11 +68,17 @@ function initialize(app, options) { app.post('/login', csrf(), - passport.authenticate('local', { - successRedirect: `${self.mountPath}apps`, - failureRedirect: `${self.mountPath}login`, - failureFlash : true - }) + (req,res,next) => { + let redirect = 'apps'; + if (req.body.redirect) { + redirect = req.body.redirect.charAt(0) === '/' ? req.body.redirect.substring(1) : req.body.redirect + } + return passport.authenticate('local', { + successRedirect: `${self.mountPath}${redirect}`, + failureRedirect: `${self.mountPath}login${req.body.redirect ? `?redirect=${req.body.redirect}` : ''}`, + failureFlash : true + })(req, res, next) + }, ); app.get('/logout', function(req, res){ diff --git a/Parse-Dashboard/app.js b/Parse-Dashboard/app.js index 0149b8c634..952b55821c 100644 --- a/Parse-Dashboard/app.js +++ b/Parse-Dashboard/app.js @@ -173,8 +173,9 @@ module.exports = function(config, options) { } app.get('/login', csrf(), function(req, res) { + const redirectURL = req.url.includes('?redirect=') && req.url.split('?redirect=')[1]; if (!users || (req.user && req.user.isAuthenticated)) { - return res.redirect(`${mountPath}apps`); + return res.redirect(`${mountPath}${redirectURL || 'apps'}`); } let errors = req.flash('error'); @@ -206,7 +207,7 @@ module.exports = function(config, options) { // For every other request, go to index.html. Let client-side handle the rest. app.get('/*', function(req, res) { if (users && (!req.user || !req.user.isAuthenticated)) { - return res.redirect(`${mountPath}login`); + return res.redirect(`${mountPath}login?redirect=${req.url.replace('/login', '')}`); } if (users && req.user && req.user.matchingUsername ) { res.append('username', req.user.matchingUsername); diff --git a/src/login/Login.js b/src/login/Login.js index 79f4de1b23..967da8b57e 100644 --- a/src/login/Login.js +++ b/src/login/Login.js @@ -28,10 +28,13 @@ export default class Login extends React.Component { } } + const url = new URL(window.location); + const redirect = url.searchParams.get('redirect'); this.state = { forgot: false, username: sessionStorage.getItem('username') || '', - password: sessionStorage.getItem('password') || '' + password: sessionStorage.getItem('password') || '', + redirect }; sessionStorage.clear(); setBasePath(props.path); @@ -106,6 +109,11 @@ export default class Login extends React.Component { ref={this.inputRefPass} /> } /> + {this.state.redirect && } { this.errors && this.errors.includes('one-time') ?