How to redirect on a custom page after login?2019 Community Moderator ElectionHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsHow to get GET (query string) variables in Express.js on Node.js?Redirecting to previous page after authentication in node.js using passport.jsCalling and Saving User Account Custom DataRedirecting to original url after authentication in PassportJSOnce authenticated always go to index page and not loginPassportJs isAuthenticated not Authenticatingwhy req.isAuthenticated() return false even when session saved the user id?

Are ETF trackers fundamentally better than individual stocks?

Are there verbs that are neither telic, or atelic?

How to make healing in an exploration game interesting

Unexpected result from ArcLength

Happy pi day, everyone!

Time travel from stationary position?

In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf

Does Mathematica reuse previous computations?

Error in Twin Prime Conjecture

Do I need to be arrogant to get ahead?

How to deal with a cynical class?

Is a party consisting of only a bard, a cleric, and a warlock functional long-term?

Why doesn't using two cd commands in bash script execute the second command?

Professor being mistaken for a grad student

How to deal with taxi scam when on vacation?

How do I hide Chekhov's Gun?

What do Xenomorphs eat in the Alien series?

Interplanetary conflict, some disease destroys the ability to understand or appreciate music

Combining an idiom with a metonymy

Min function accepting varying number of arguments in C++17

Gantt Chart like rectangles with log scale

How to simplify this time periods definition interface?

How to explain that I do not want to visit a country due to personal safety concern?

Is this a real picture of Jordan Peterson in New Zealand with a fan wearing a shirt that says "I'm a Proud Islamaphobe"?



How to redirect on a custom page after login?



2019 Community Moderator ElectionHow do I pass command line arguments to a Node.js program?How to decide when to use Node.js?How to exit in Node.jsHow to get GET (query string) variables in Express.js on Node.js?Redirecting to previous page after authentication in node.js using passport.jsCalling and Saving User Account Custom DataRedirecting to original url after authentication in PassportJSOnce authenticated always go to index page and not loginPassportJs isAuthenticated not Authenticatingwhy req.isAuthenticated() return false even when session saved the user id?










0















I'm using PassportJS for handle the access to my application, suppose that the user has logged in, and suppose that these urls:



  • Login

  • Register

  • Welcome

need to redirect the user on dashboard url, only if the user has logged in, how can I do?



This is my authentication middleware:



module.exports = 
ensureAuthenticated: function(req, res, next)
if(req.isAuthenticated())
return next();


res.render('index/forbidden');

;


Example:



  • User go to login

  • No session

  • Stay on the page

(already works)



  • User go to login

  • Already logged in

  • Redirect to dashboard









share|improve this question


























    0















    I'm using PassportJS for handle the access to my application, suppose that the user has logged in, and suppose that these urls:



    • Login

    • Register

    • Welcome

    need to redirect the user on dashboard url, only if the user has logged in, how can I do?



    This is my authentication middleware:



    module.exports = 
    ensureAuthenticated: function(req, res, next)
    if(req.isAuthenticated())
    return next();


    res.render('index/forbidden');

    ;


    Example:



    • User go to login

    • No session

    • Stay on the page

    (already works)



    • User go to login

    • Already logged in

    • Redirect to dashboard









    share|improve this question
























      0












      0








      0








      I'm using PassportJS for handle the access to my application, suppose that the user has logged in, and suppose that these urls:



      • Login

      • Register

      • Welcome

      need to redirect the user on dashboard url, only if the user has logged in, how can I do?



      This is my authentication middleware:



      module.exports = 
      ensureAuthenticated: function(req, res, next)
      if(req.isAuthenticated())
      return next();


      res.render('index/forbidden');

      ;


      Example:



      • User go to login

      • No session

      • Stay on the page

      (already works)



      • User go to login

      • Already logged in

      • Redirect to dashboard









      share|improve this question














      I'm using PassportJS for handle the access to my application, suppose that the user has logged in, and suppose that these urls:



      • Login

      • Register

      • Welcome

      need to redirect the user on dashboard url, only if the user has logged in, how can I do?



      This is my authentication middleware:



      module.exports = 
      ensureAuthenticated: function(req, res, next)
      if(req.isAuthenticated())
      return next();


      res.render('index/forbidden');

      ;


      Example:



      • User go to login

      • No session

      • Stay on the page

      (already works)



      • User go to login

      • Already logged in

      • Redirect to dashboard






      node.js express passport.js






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 6 at 20:00









      sfarzososfarzoso

      184112




      184112






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You can create another middleware to redirect if logged in and add it to the routes that don't need login. You are basically doing the opposite.



          module.exports = 
          ensureAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return next();

          res.render('index/forbidden');
          ,
          ensureNOTAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return res.redirect('/dashboard')

          return next();

          ;


          For routes that need auth:



          app.get('/dashboard', ensureAuthenticated, (req,res)=>...);


          For routes that dob't need auth:



          app.get('/login', ensureNOTAuthenticated, (req,res)=>...);





          share|improve this answer

























          • seems the best solution, thanks

            – sfarzoso
            Mar 6 at 21:15










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031288%2fhow-to-redirect-on-a-custom-page-after-login%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can create another middleware to redirect if logged in and add it to the routes that don't need login. You are basically doing the opposite.



          module.exports = 
          ensureAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return next();

          res.render('index/forbidden');
          ,
          ensureNOTAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return res.redirect('/dashboard')

          return next();

          ;


          For routes that need auth:



          app.get('/dashboard', ensureAuthenticated, (req,res)=>...);


          For routes that dob't need auth:



          app.get('/login', ensureNOTAuthenticated, (req,res)=>...);





          share|improve this answer

























          • seems the best solution, thanks

            – sfarzoso
            Mar 6 at 21:15















          1














          You can create another middleware to redirect if logged in and add it to the routes that don't need login. You are basically doing the opposite.



          module.exports = 
          ensureAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return next();

          res.render('index/forbidden');
          ,
          ensureNOTAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return res.redirect('/dashboard')

          return next();

          ;


          For routes that need auth:



          app.get('/dashboard', ensureAuthenticated, (req,res)=>...);


          For routes that dob't need auth:



          app.get('/login', ensureNOTAuthenticated, (req,res)=>...);





          share|improve this answer

























          • seems the best solution, thanks

            – sfarzoso
            Mar 6 at 21:15













          1












          1








          1







          You can create another middleware to redirect if logged in and add it to the routes that don't need login. You are basically doing the opposite.



          module.exports = 
          ensureAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return next();

          res.render('index/forbidden');
          ,
          ensureNOTAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return res.redirect('/dashboard')

          return next();

          ;


          For routes that need auth:



          app.get('/dashboard', ensureAuthenticated, (req,res)=>...);


          For routes that dob't need auth:



          app.get('/login', ensureNOTAuthenticated, (req,res)=>...);





          share|improve this answer















          You can create another middleware to redirect if logged in and add it to the routes that don't need login. You are basically doing the opposite.



          module.exports = 
          ensureAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return next();

          res.render('index/forbidden');
          ,
          ensureNOTAuthenticated: function(req, res, next)
          if(req.isAuthenticated())
          return res.redirect('/dashboard')

          return next();

          ;


          For routes that need auth:



          app.get('/dashboard', ensureAuthenticated, (req,res)=>...);


          For routes that dob't need auth:



          app.get('/login', ensureNOTAuthenticated, (req,res)=>...);






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 6 at 21:31









          sfarzoso

          184112




          184112










          answered Mar 6 at 20:18









          Aritra ChakrabortyAritra Chakraborty

          2,41311015




          2,41311015












          • seems the best solution, thanks

            – sfarzoso
            Mar 6 at 21:15

















          • seems the best solution, thanks

            – sfarzoso
            Mar 6 at 21:15
















          seems the best solution, thanks

          – sfarzoso
          Mar 6 at 21:15





          seems the best solution, thanks

          – sfarzoso
          Mar 6 at 21:15



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55031288%2fhow-to-redirect-on-a-custom-page-after-login%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved