尝试登录时的错误请求 - 护照,快递

时间:2016-02-03 19:13:32

标签: node.js express passport.js

这是app.js

var express = require('express');
var session = require('express-session');
var mongoose = require('mongoose');
var multer = require('multer');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var passportLocalMongoose = require('passport-local-mongoose');
var app = express();
var uploadDir = __dirname + '/uploads';

mongoose.connect('mongodb://orion:orion@127.0.0.1:27017/test');

var Schema = mongoose.Schema;
var userSchema = new Schema({
    name: String,
    email: String,
    password: String,
    pp: String
});
var users = mongoose.model('users', userSchema);
var port = process.env.PORT || 3000;

app.set('views', './views');
app.set('view engine', 'jade');
app.use('/assets', express.static(__dirname + '/public'));
app.use(session({secret: 'supernova', saveUninitialized: true, resave: true}));
app.use(passport.initialize());
app.use(passport.session());

userSchema.plugin(passportLocalMongoose);
passport.serializeUser(function (user, done) {
    done(null, user.id);
});

passport.deserializeUser(function (id, done) {
    users.findById(id, function (err, user) {
        done(err, user);
    });
});
passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
}, function (username, password, done) {

    users.findOne({email: username}, function (err, user) {
        if (err) {
            return done(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);
    });
}));


app.all('/', function (req, res) {
    res.render('login');

})

app.all('/register', function (req, res) {
    res.render('register');
})

app.all('/login', function (req, res) {
    res.render('login');
})

var pp = Date.now() + '.jpg';

var storage = multer.diskStorage({
    destination: function (request, file, callback) {
        callback(null, uploadDir);
    },
    filename: function (request, file, callback) {
        console.log(file);
        callback(null, pp);
    }
});
var upload = multer({storage: storage}).single('proPic');

app.all('/doReg', upload, function (request, response) {
    var user = users({
        'name': request.body.name,
        'email': request.body.email,
        'password': request.body.password,
        'pp': pp
    })
    user.save(function (err) {
        if (err) throw err;
    });

    var url = request.protocol + '://' + request.get('host') + '/login'
    response.writeHead(301,
        {Location: url}
    );
    //response.redirect('/login');
    response.end();
});

app.all('/doLogin', passport.authenticate('local'), function (req, res) {
    console.log('abc');

    res.redirect('/');
});

app.listen(port);

这是login.jade

doctype html
html
    head
        title.
            Fonebook
        link(rel='stylesheet', href='/assets/bootstrap/css/bootstrap.min.css')
        script(src='https://code.jquery.com/jquery-2.2.0.min.js')
        script(src='/assets/bootstrap/js/bootstrap.min.js')
    body
        div.container
            h3.
                The Fonebook
            form(method="post",action="/doLogin",class="center",id="loginForm")
                div.panel.panel-info
                    div.panel-heading.
                        Login
                    div.panel-body
                        div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
                            label.
                                Email:
                            input.form-control(id="email", type="email", name="email", placeholder="Enter email ")
                        div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
                            label.
                                Password:
                            input.form-control(id="password", type="password", name="password")
                        div.col-md-offset-3.col-md-6.col-md-offset-3.form-group
                            a(href='register').
                                Don't have an account?
                    div.panel-footer
                        button.btn.btn-info(type="submit").
                            Login

可能不需要了解我的问题,但这里是package.json

{
  "name": "fonebook",
  "version": "1.0.0",
  "description": "a simple phonebook",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "phonebook"
  ],
  "author": "Orion",
  "license": "ISC",
  "dependencies": {
    "express": "^4.13.4",
    "express-session": "^1.13.0",
    "jade": "^1.11.0",
    "mongoose": "^4.3.6",
    "multer": "^1.1.0",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^4.0.0"
  }
}

现在问题是当我尝试登录我的浏览器时显示“Bad request”

我在本地安装了mongodb,其中有一个名为“test”的数据库,其中包含“用户”集合。

这里似乎有什么问题?是的,我是一个菜鸟。

0 个答案:

没有答案