@@ -252,87 +252,94 @@ app.post("/temp", urlencodedParser, function (req, res) {
252252 }
253253 }
254254} ) ;
255+
256+ function setReturnToFromReferer ( req ) {
257+ var referer = req . get ( 'referer' ) ;
258+ if ( ! req . session ) req . session = { } ;
259+ req . session . returnTo = referer ;
260+ }
261+
255262//facebook auth
256263if ( config . facebook ) {
257- app . get ( '/auth/facebook' ,
258- passport . authenticate ( 'facebook' ) ) ;
264+ app . get ( '/auth/facebook' , function ( req , res , next ) {
265+ setReturnToFromReferer ( req ) ;
266+ passport . authenticate ( 'facebook' ) ( req , res , next ) ;
267+ } ) ;
259268 //facebook auth callback
260269 app . get ( '/auth/facebook/callback' ,
261270 passport . authenticate ( 'facebook' , {
271+ successReturnToOrRedirect : config . serverurl + '/' ,
262272 failureRedirect : config . serverurl + '/'
263- } ) ,
264- function ( req , res ) {
265- res . redirect ( config . serverurl + '/' ) ;
266- } ) ;
273+ } ) ) ;
267274}
268275//twitter auth
269276if ( config . twitter ) {
270- app . get ( '/auth/twitter' ,
271- passport . authenticate ( 'twitter' ) ) ;
277+ app . get ( '/auth/twitter' , function ( req , res , next ) {
278+ setReturnToFromReferer ( req ) ;
279+ passport . authenticate ( 'twitter' ) ( req , res , next ) ;
280+ } ) ;
272281 //twitter auth callback
273282 app . get ( '/auth/twitter/callback' ,
274283 passport . authenticate ( 'twitter' , {
284+ successReturnToOrRedirect : config . serverurl + '/' ,
275285 failureRedirect : config . serverurl + '/'
276- } ) ,
277- function ( req , res ) {
278- res . redirect ( config . serverurl + '/' ) ;
279- } ) ;
286+ } ) ) ;
280287}
281288//github auth
282289if ( config . github ) {
283- app . get ( '/auth/github' ,
284- passport . authenticate ( 'github' ) ) ;
290+ app . get ( '/auth/github' , function ( req , res , next ) {
291+ setReturnToFromReferer ( req ) ;
292+ passport . authenticate ( 'github' ) ( req , res , next ) ;
293+ } ) ;
285294 //github auth callback
286295 app . get ( '/auth/github/callback' ,
287296 passport . authenticate ( 'github' , {
297+ successReturnToOrRedirect : config . serverurl + '/' ,
288298 failureRedirect : config . serverurl + '/'
289- } ) ,
290- function ( req , res ) {
291- res . redirect ( config . serverurl + '/' ) ;
292- } ) ;
299+ } ) ) ;
293300 //github callback actions
294301 app . get ( '/auth/github/callback/:noteId/:action' , response . githubActions ) ;
295302}
296303//gitlab auth
297304if ( config . gitlab ) {
298- app . get ( '/auth/gitlab' ,
299- passport . authenticate ( 'gitlab' ) ) ;
305+ app . get ( '/auth/gitlab' , function ( req , res , next ) {
306+ setReturnToFromReferer ( req ) ;
307+ passport . authenticate ( 'gitlab' ) ( req , res , next ) ;
308+ } ) ;
300309 //gitlab auth callback
301310 app . get ( '/auth/gitlab/callback' ,
302311 passport . authenticate ( 'gitlab' , {
312+ successReturnToOrRedirect : config . serverurl + '/' ,
303313 failureRedirect : config . serverurl + '/'
304- } ) ,
305- function ( req , res ) {
306- res . redirect ( config . serverurl + '/' ) ;
307- } ) ;
314+ } ) ) ;
308315 //gitlab callback actions
309316 app . get ( '/auth/gitlab/callback/:noteId/:action' , response . gitlabActions ) ;
310317}
311318//dropbox auth
312319if ( config . dropbox ) {
313- app . get ( '/auth/dropbox' ,
314- passport . authenticate ( 'dropbox-oauth2' ) ) ;
320+ app . get ( '/auth/dropbox' , function ( req , res , next ) {
321+ setReturnToFromReferer ( req ) ;
322+ passport . authenticate ( 'dropbox-oauth2' ) ( req , res , next ) ;
323+ } ) ;
315324 //dropbox auth callback
316325 app . get ( '/auth/dropbox/callback' ,
317326 passport . authenticate ( 'dropbox-oauth2' , {
327+ successReturnToOrRedirect : config . serverurl + '/' ,
318328 failureRedirect : config . serverurl + '/'
319- } ) ,
320- function ( req , res ) {
321- res . redirect ( config . serverurl + '/' ) ;
322- } ) ;
329+ } ) ) ;
323330}
324331//google auth
325332if ( config . google ) {
326- app . get ( '/auth/google' ,
327- passport . authenticate ( 'google' , { scope : [ 'profile' ] } ) ) ;
333+ app . get ( '/auth/google' , function ( req , res , next ) {
334+ setReturnToFromReferer ( req ) ;
335+ passport . authenticate ( 'google' , { scope : [ 'profile' ] } ) ( req , res , next ) ;
336+ } ) ;
328337 //google auth callback
329338 app . get ( '/auth/google/callback' ,
330339 passport . authenticate ( 'google' , {
340+ successReturnToOrRedirect : config . serverurl + '/' ,
331341 failureRedirect : config . serverurl + '/'
332- } ) ,
333- function ( req , res ) {
334- res . redirect ( config . serverurl + '/' ) ;
335- } ) ;
342+ } ) ) ;
336343}
337344//logout
338345app . get ( '/logout' , function ( req , res ) {
0 commit comments