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
Add a ment  | 

4 Answers 4

Reset to default 6

You 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