1
這裏沒有太多解釋。我已經在某種程度上,我認爲應該工作,當我用正確的信息登錄這些都是在控制檯輸出設置快遞和護照:成功登錄後POST請求失敗
Login attempt: [email protected]
Login success: [email protected]
Serialize:
{ __v: 0,
_id: 5320883fbb3e62fc14fda44a,
email: '[email protected]',
password: '$2a$10$VpTLJgwALxcTeNO1YH3k7eBXoI589fOZLzqtkSdpwL2N9sxPgO1bS',
verified: true }
POST /login 404 714ms
現在,這是所有相關代碼:
import os = require("os");
import express = require("express");
import paths = require("./paths");
import https = require("https");
import http = require("http");
import fs = require("fs");
var stylus = require("stylus"),
nib = require("nib");
var app = express();
var mongoose = require("mongoose");
var bcrypt = require("bcrypt-nodejs");
var db = require("./db");
var passport = require("passport");
var LocalStrategy = require("passport-local").Strategy;
passport.use(new LocalStrategy({
usernameField: "email",
passwordField: "password"
},
function (email, password, done) {
db.User.findOne({ email: email }, function (err, user) {
console.log("Login attempt: " + email);
if (err) { return done(err); };
if (!user) {
return done(null, false, { message: "Incorrect email" });
}
if (!bcrypt.compareSync(password, user.password)) {
return done(null, false, { message: "Incorrect password" });
}
console.log("Login success: " + email);
return done(null, user);
});
}
));
passport.serializeUser(function (user, done) {
console.log("Serialize: ");
console.log(user);
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
db.User.findById(id, function(err, user) {
console.log("Deserialize: " + err + ", " + user);
done(err, user);
});
});
var httpPort = 8442;
var httpsPort = 8443;
app.locals.pathNoProtocol = (os.hostname() == "domain.me" ? "domain.me" : "localhost") + ":" + httpsPort + app.path();
app.locals.path = "https://" + app.locals.pathNoProtocol;
app.locals.capitalize = function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};
//Redirect all HTTP traffic to HTTPS
app.use(function (req, res, next) {
if (!req.secure) { //HTTP request
console.log("HTTP request for " + req.url + ". Redirecting to " + app.locals.path + req.url);
return res.redirect(app.locals.path + req.url);
}
next();
});
app.enable("strict routing");
var compile = function (str, path) {
return stylus(str)
.set('filename', path)
.use(nib());
};
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(stylus.middleware({
src: __dirname + '/public',
compile: compile
}));
app.configure(function() {
app.use(express.static('public'));
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.session({ secret: "supersecret" }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
paths(app); //Process paths
mongoose.connect("mongodb://localhost/test");
mongoose.connection.on("connected", function() { console.log("Connected to db"); });
https.createServer({
key: fs.readFileSync("server-key.pem"),
cert: fs.readFileSync("server-cert.pem")
}, app).listen(httpsPort);
http.createServer(app).listen(httpPort);
我已經留在了一些重定向的東西,因爲我不知道,這可能是這裏的問題。
這也是我如何處理paths.js
app.get("/login", function (req, res) {
res.render("pages/login");
});
app.post("/login", passport.authenticate("local", {
succesRedirect: "/",
failureRedirect: "/loginFailed"
}
));
我會很感激,如果有人花時間看在什麼我可以做什麼錯誤的路徑。