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?
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
add a comment |
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
add a comment |
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
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
node.js express passport.js
asked Mar 6 at 20:00
sfarzososfarzoso
184112
184112
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)=>...);
seems the best solution, thanks
– sfarzoso
Mar 6 at 21:15
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)=>...);
seems the best solution, thanks
– sfarzoso
Mar 6 at 21:15
add a comment |
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)=>...);
seems the best solution, thanks
– sfarzoso
Mar 6 at 21:15
add a comment |
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)=>...);
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)=>...);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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