admin管理员组文章数量:1366984
I have the following code that checks to see if a user exists in a database...
router.post('/', function (req, res) {
User.findOne({
username: req.body.log_username,
password: req.body.log_password
}, function (err, docs) {
if (docs.length !== 0) {
console.log("user exists");
}
else {
console.log("no exist");
}
});
});
I have a home page that I want to send the user to if the login was a success. What should I put in the if
statement to send the use to another page, in this case, home.js
. home.js
has the following code in it...
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('home', { title: 'Express' });
});
module.exports = router;
I have the following code that checks to see if a user exists in a database...
router.post('/', function (req, res) {
User.findOne({
username: req.body.log_username,
password: req.body.log_password
}, function (err, docs) {
if (docs.length !== 0) {
console.log("user exists");
}
else {
console.log("no exist");
}
});
});
I have a home page that I want to send the user to if the login was a success. What should I put in the if
statement to send the use to another page, in this case, home.js
. home.js
has the following code in it...
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('home', { title: 'Express' });
});
module.exports = router;
Share
Improve this question
asked Jun 26, 2016 at 19:56
buydadipbuydadip
9,45722 gold badges93 silver badges160 bronze badges
3
- If it worked , mark it resolve – Zohaib Ijaz Commented Jun 26, 2016 at 20:14
- @ZohaibIjaz patience my friend, I always do – buydadip Commented Jun 26, 2016 at 20:44
- hahahaha.... wait is too bad thing :) – Zohaib Ijaz Commented Jun 26, 2016 at 20:45
4 Answers
Reset to default 6You need to redirect the user to main page. res.redirect('path')
Reference : http://expressjs./en/4x/api.html#res.redirect
router.post('/', function (req, res) {
User.findOne({
username: req.body.log_username,
password: req.body.log_password
}, function (err, docs) {
if (docs.length !== 0) {
console.log("user exists");
res.redirect('/'); // main page url
}
else {
console.log("no exist");
res.redirect('/login');
}
});
});
Mongoose findOne will return 1 object as result
else null
.
No need to check for docs.length > 0
manual: http://mongoosejs./docs/api.html#query_Query-findOne)
Also added req.session.user = result
maybe You'll need it to check if user authenticated (inside middleware function to prevent direct access to internal routes.
router.post('/', function (req, res) {
User
.findOne({
username: req.body.log_username,
password: req.body.log_password
})
.exec(function (err, result) {
if(result) { // auth was successful
req.session.user = result; // so writing user document to session
return res.redirect('/'); // redirecting user to interface
}
// auth not successful, because result is null
res.redirect('/login'); // redirect to login page
});
});
Somewhere in middleware store originalUrl to session:
req.session.returnTo = req.originalUrl;
And change successRedirect
to successReturnToOrRedirect
param:
app.post('/login', passport.authenticate('local-login', {
successReturnToOrRedirect: '/dashboard', // redirect to the secure profile section
failureRedirect: '/login?Failed', // redirect back to the signup page if there is an error
failureFlash: true // allow flash messages
}));
You'd want something like
res.redirect('/');
in your POST to "/".
Example:
if (docs.length !== 0) {
console.log("user exists");
res.redirect('/');
}
本文标签: javascriptHow to send user to another page after login (node js)Stack Overflow
版权声明:本文标题:javascript - How to send user to another page after login (node js) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743764839a2535046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论