护照无法正确认证

时间:2016-11-15 04:51:38

标签: node.js mongodb express mongoose passport.js

这是我第一次进行用户身份验证,我觉得我差不多完成了。我正在使用Express与Passport.js和MongoDB / Mongoose。当我运行我的gulp文件时,所有内容都正确加载但当我尝试使用我的凭据登录时,我会不断重定向到故障页面(主屏幕)。这是我的代码:

{ "_id" : "/url2.php", "diff" : 17 }
{ "_id" : "/url3.php", "diff" : 75 }
{ "_id" : "/url1.php", "diff" : 4 }

和userData模块中的代码:

const mongoose = require('mongoose')

// Initialize
const bodyParser = require('body-parser')
const path = require('path')
const bcrypt = require('bcryptjs')
const express = require('express')
const session = require('express-session')
const cookieParser = require('cookie-parser')
const passport = require('passport'),
      LocalStrategy = require('passport-local').Strategy
let userData = require('./models/user.js')
const app = express()

// Configuration
app.use(cookieParser())
app.use(session({ secret: 'ThisIsTheSessionSecret', resave: true, saveUninitialized: true }))
app.use(passport.initialize())
app.use(passport.session())
app.use(express.static(path.join(__dirname, '../../docs')))
app.use(express.static(path.join(__dirname, '../../src')))

app.set('port', process.env.PORT || 8080);
app.set('view engine', 'pug')
app.set('views', 'src/pages')

let urlencodedParser = bodyParser.urlencoded({ extended: false })

// Configure authentication and sessions
passport.use(new LocalStrategy(
  function(username, password, done) {
    user.findOne({ username: username }, function(err, user) {
      if (err) { console.log(err) }
      if (!user) {
        return done(null, false, { message: 'Incorrect username.' })
      }
      if (!user.validPassword(password)) {
        return done(null, false, { message: 'Incorrect password.' })
      }
      return done(null, user)
    })
  }
));

// Session
passport.serializeUser(function(user, done) {
  done(null, user.id)
});

passport.deserializeUser(function(id, done) {
  user.findById(id, function(err, user) {
    done(err, user)
  })
})

app.listen(app.get('port'))
console.log('Listening on port: ' + app.get('port'))

// Initialize database (local or remote?)
const MongoClient = require('mongodb').MongoClient,
      mongoose = require('mongoose'),
      assert = require('assert')

const database = 'mongodb://localhost:27017/database'
mongoose.connect(database)

let db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error'))

db.once('open', function() {
  // let testUser = new userData.UserModel({ username: 'PodcatchUser', password: 'RIPHarambe' })
  // console.log(testUser.username)
})

// Routing
app.get('/', function(req, res) {
    console.log('Welcome!')
    res.render('index')
})

// Registration
app.post('/signup', urlencodedParser, function(req, res) {
    let username = req.body.username,
        password = req.body.password,
        salt = bcrypt.genSaltSync(10),
        hash = bcrypt.hashSync(password, salt)

    let user = new userData.UserModel({
        username: username,
        password: hash
    })

    console.log(user.username)
    console.log(user.password)

    user.save(function(err) {
        if (err) {
            return console.log(err, user)
        } else {
            console.log('User has been saved to the database successfully')
        }
    })
    res.end()
})

// Login
app.post('/login', passport.authenticate('local', { successRedirect: '/success',
        failureRedirect: '/' }), function(req, res) {
        res.end()
})

我真的很困惑发生了什么。 Mongo将凭据和散列密码保存到数据库中,我创建了一个会话并拥有一个cookie解析器,但它没有进行身份验证。非常感谢帮助:))

0 个答案:

没有答案