File tree Expand file tree Collapse file tree 3 files changed +23
-8
lines changed Expand file tree Collapse file tree 3 files changed +23
-8
lines changed Original file line number Diff line number Diff line change @@ -67,11 +67,17 @@ function initialize(app, options) {
6767
6868 app . post ( '/login' ,
6969 csrf ( ) ,
70- passport . authenticate ( 'local' , {
71- successRedirect : `${ self . mountPath } apps` ,
72- failureRedirect : `${ self . mountPath } login` ,
73- failureFlash : true
74- } )
70+ ( req , res , next ) => {
71+ let redirect = 'apps' ;
72+ if ( req . body . redirect ) {
73+ redirect = req . body . redirect . charAt ( 0 ) === '/' ? req . body . redirect . substring ( 1 ) : req . body . redirect
74+ }
75+ return passport . authenticate ( 'local' , {
76+ successRedirect : `${ self . mountPath } ${ redirect } ` ,
77+ failureRedirect : `${ self . mountPath } login${ req . body . redirect ? `?redirect=${ req . body . redirect } ` : '' } ` ,
78+ failureFlash : true
79+ } ) ( req , res , next )
80+ } ,
7581 ) ;
7682
7783 app . get ( '/logout' , function ( req , res ) {
Original file line number Diff line number Diff line change @@ -173,8 +173,9 @@ module.exports = function(config, options) {
173173 }
174174
175175 app . get ( '/login' , csrf ( ) , function ( req , res ) {
176+ const redirectURL = req . url . includes ( '?redirect=' ) && req . url . split ( '?redirect=' ) [ 1 ] ;
176177 if ( ! users || ( req . user && req . user . isAuthenticated ) ) {
177- return res . redirect ( `${ mountPath } apps` ) ;
178+ return res . redirect ( `${ mountPath } ${ redirectURL || ' apps' } ` ) ;
178179 }
179180
180181 let errors = req . flash ( 'error' ) ;
@@ -206,7 +207,7 @@ module.exports = function(config, options) {
206207 // For every other request, go to index.html. Let client-side handle the rest.
207208 app . get ( '/*' , function ( req , res ) {
208209 if ( users && ( ! req . user || ! req . user . isAuthenticated ) ) {
209- return res . redirect ( `${ mountPath } login` ) ;
210+ return res . redirect ( `${ mountPath } login?redirect= ${ req . url . replace ( '/login' , '' ) } ` ) ;
210211 }
211212 if ( users && req . user && req . user . matchingUsername ) {
212213 res . append ( 'username' , req . user . matchingUsername ) ;
Original file line number Diff line number Diff line change @@ -28,10 +28,13 @@ export default class Login extends React.Component {
2828 }
2929 }
3030
31+ const url = new URL ( window . location ) ;
32+ const redirect = url . searchParams . get ( 'redirect' ) ;
3133 this . state = {
3234 forgot : false ,
3335 username : sessionStorage . getItem ( 'username' ) || '' ,
34- password : sessionStorage . getItem ( 'password' ) || ''
36+ password : sessionStorage . getItem ( 'password' ) || '' ,
37+ redirect
3538 } ;
3639 sessionStorage . clear ( ) ;
3740 setBasePath ( props . path ) ;
@@ -106,6 +109,11 @@ export default class Login extends React.Component {
106109 ref = { this . inputRefPass }
107110 />
108111 } />
112+ { this . state . redirect && < input
113+ name = 'redirect'
114+ type = 'hidden'
115+ value = { this . state . redirect }
116+ /> }
109117 {
110118 this . errors && this . errors . includes ( 'one-time' ) ?
111119 < LoginRow
You can’t perform that action at this time.
0 commit comments