我正在调整this local authentication implementation(使用Sequelize而不是Mongoose,但这应该是DB不可知的),这是我的以下目录层次结构:
app.js
package.json
node_modules
config
-----env
-----config.js
-----passport.js
-----sequelize.js
views
-----index.ejs
-----login.ejs
-----signup.ejs
我有app.js中定义的路由和sequelize.js中定义的模型,如下所述:
const express = require('express');
const http = require('http');
const https = require('https');
const sequelize = require('sequelize');
const db = require('./config/sequelize');
const config = require('./config/config');
const passport = require('passport');
const strategies = require('./config/passport')(passport);
const env = 'development';
const app = express();
const port = 3000;
app.set('view engine', 'ejs');
app.use(passport.initialize());
app.use(passport.session());
app.listen(port);
function isLoggedIn(req, res, next) {
if (req.isAuthenticated()){
return next();
}
else{
res.redirect('/');
}
}
app.post('/signup', function(req, res){
console.log("nothing");
passport.authenticate('local-signup', {
successRedirect : '/profile',
failureRedirect : '/signup',
failureFlash : false // allow flash messages
}
}));
const LocalStrategy = require('passport-local').Strategy;
const passport = require('passport');
const User = require('../config/sequelize');
module.exports = function(passport) {
passport.serializeUser(function(user, done) {
console.log("Serializing user");
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
console.log("Deserializing user");
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local-signup', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
console.log("Inside local strategy");
process.nextTick(function() {
User.findOne({ 'local.email' : email }, function(err, user){
if (err){
console.log("nothing");
return done(err);
}
if (user) {
console.log("There is already a user attached to this email.");
return done(null);
}
else {
const newUser = new User();
newUser.local.email = email;
newUser.local.password = password;
newUser.save(function(err) {
if (err)
throw err;
return done(null, newUser);
});
}
});
});
}));
};
<!doctype html>
<html>
<head>
<title>Node Authentication</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"> <!-- load fontawesome -->
<style> body { padding-top:80px; }</style>
</head>
<body>
<div class="container">
<div class="col-sm-6 col-sm-offset-3">
<h1><span class="fa fa-sign-in"></span> Signup</h1>
<% if (message.length > 0) { %>
<div class="alert alert-danger"><%= message %></div>
<% } %>
<form action="/signup" method="post">
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password">
</div>
<button type="submit" class="btn btn-warning btn-lg">Signup</button>
</form>
<hr>
<p>Already have an account? <a href="/login">Login</a></p>
<p>Or go <a href="/">home</a>.</p>
</div>
</div>
</body>
</html>
好的衡量标准:
const path = require('path');
const Sequelize = require('sequelize');
const _ = require('lodash');
const config = require('./config');
const db = {};
const sequelize = new Sequelize(config.db.name, config.db.username, config.db.password, {
host: config.db.host,
port: config.db.port,
dialect: 'mysql',
storage: config.db.storage
});
sequelize
.authenticate()
.then(function(err) {
console.log('Connection has been established successfully.');
}, function (err) {
console.log('Unable to connect to the database:', err);
});
const User = sequelize.define('User', {
username: Sequelize.STRING,
email: Sequelize.STRING,
password: Sequelize.STRING
});
sequelize.sync();
module.exports = User;
运行应用程序并按下注册按钮
时会发生什么是config / passport.js中的“nothing”console.log语句,但是当我尝试通过在'err'上添加console.log语句来诊断错误源时,似乎没有任何内容出现在cmd上。