File tree 3 files changed +23
-8
lines changed
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) {
67
67
68
68
app . post ( '/login' ,
69
69
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
+ } ,
75
81
) ;
76
82
77
83
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) {
173
173
}
174
174
175
175
app . get ( '/login' , csrf ( ) , function ( req , res ) {
176
+ const redirectURL = req . url . includes ( '?redirect=' ) && req . url . split ( '?redirect=' ) [ 1 ] ;
176
177
if ( ! users || ( req . user && req . user . isAuthenticated ) ) {
177
- return res . redirect ( `${ mountPath } apps` ) ;
178
+ return res . redirect ( `${ mountPath } ${ redirectURL || ' apps' } ` ) ;
178
179
}
179
180
180
181
let errors = req . flash ( 'error' ) ;
@@ -206,7 +207,7 @@ module.exports = function(config, options) {
206
207
// For every other request, go to index.html. Let client-side handle the rest.
207
208
app . get ( '/*' , function ( req , res ) {
208
209
if ( users && ( ! req . user || ! req . user . isAuthenticated ) ) {
209
- return res . redirect ( `${ mountPath } login` ) ;
210
+ return res . redirect ( `${ mountPath } login?redirect= ${ req . url . replace ( '/login' , '' ) } ` ) ;
210
211
}
211
212
if ( users && req . user && req . user . matchingUsername ) {
212
213
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 {
28
28
}
29
29
}
30
30
31
+ const url = new URL ( window . location ) ;
32
+ const redirect = url . searchParams . get ( 'redirect' ) ;
31
33
this . state = {
32
34
forgot : false ,
33
35
username : sessionStorage . getItem ( 'username' ) || '' ,
34
- password : sessionStorage . getItem ( 'password' ) || ''
36
+ password : sessionStorage . getItem ( 'password' ) || '' ,
37
+ redirect
35
38
} ;
36
39
sessionStorage . clear ( ) ;
37
40
setBasePath ( props . path ) ;
@@ -106,6 +109,11 @@ export default class Login extends React.Component {
106
109
ref = { this . inputRefPass }
107
110
/>
108
111
} />
112
+ { this . state . redirect && < input
113
+ name = 'redirect'
114
+ type = 'hidden'
115
+ value = { this . state . redirect }
116
+ /> }
109
117
{
110
118
this . errors && this . errors . includes ( 'one-time' ) ?
111
119
< LoginRow
You can’t perform that action at this time.
0 commit comments