简体   繁体   中英

Passport Authentication in Nodejs

Below is the Code that i'm working on but i'm getting errors while executing. Please help me in creating routes for both login and register forms which are on the same page. When I'm submitting the registration form with this code, I'm getting an error which says "Bad request"

var express = require ('express');
var app = express();
var path= require('path');
var bodyParser = require('body-parser');
var mongoose = require("mongoose");
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport();
var smtpTransport = require('nodemailer-smtp-transport');
var passport = require('passport');
var LocalStrategy = require('passport-local');
var passportLocalMongoose = require('passport-local-mongoose');
var User = require('./models/user');


app.set('port',  process.env.PORT || 3000);
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

app.use(express.static(path.join(__dirname, 'public')));
app.set("view engine", "ejs");
app.use(require('express-session')({
  secret: 'Working on enroll Medicare',
  resave: false,
  saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());

passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

mongoose.connect("mongodb://localhost/registration");
app.post('/login-register.html', function(req,res)
{
            var username = req.body.usernameregister;
            var email = req.body.emailregister;
            var password1 = req.body.password1register;
            var password2 = req.body.password2register;
        password1, password2: password2}
        User.register(new User({username: username, email: email}), password1, function(err, user){
          if(err){
            console.log(err);
            return res.render('error');
          }
          passport.authenticate('local')(req, res, function(){
              res.redirect("/secret");
          });
        });
});

login page

 app.post("/login-register.html", passport.authenticate('local', {
  succesRedirect: "/secret",
   failureRedirect: "/error"
 }), function(req, res){ });

I think you may have multiple problems here. Here are a few things to check:

  • You have passport.use(new LocalStrategy(User.authenticate())); . I don't know what User.authenticate() returns, but it should return a function with the arguments function(username, password, done) {...}
  • It looks like you're using email as the username. You need to add usernameField to the strategy options: new LocalStrategy({usernameField: 'email'}, function(email, password, done){...})
  • I've also found it really helpful to use a custom callback in the passport.authenticate function to see if the returned error provides any more information about the problem: passport.authenticate('local', function(err, user, info) {console.log(err, user, info)})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM