使用 passportjs(facebook) 进行身份验证时出现 Sequelizejs 错误
Sequelizejs error when authenticating with passportjs(facebook)
我正在尝试通过 passport.js 使用 facebook OAuth 进行身份验证,但 sequelizejs 抛出以下错误:
server-0(错误):未处理的拒绝 SequelizeDatabaseError:列 "value" 不存在
我的用户模型如下所示:
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
username: {
type: DataTypes.STRING,
allowNull: true,
validate: {
}
},
displayName: {
type: DataTypes.STRING,
allowNull: true,
validate: {
}
},
email: {
type: DataTypes.STRING(256),
allowNull: false,
validate: {
isEmail: true,
len: [5, 256]
}
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
}
}
}, {
// lowercase tableName in Posrgres, if you need.
tableName: 'users',
classMethods: {
associate: function(models) {
User.hasMany(models.Candidate);
User.hasMany(models.Employer);
User.hasMany(models.Topic);
User.hasMany(models.Reply);
User.hasMany(models.Notification);
}
}
});
return User;
};
我的 passport.js 文件如下所示:
'use strict';
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
module.exports = function(app, User) {
app.use(passport.initialize());
// Enable sessions
app.use(passport.session());
passport.use(new FacebookStrategy({
clientID: 652109234923404,
clientSecret: "f126af0ec55ca0c2bc8c7cb914b7cb6b",
callbackURL: "http://localhost:5000/auth/facebook/callback"
},
function(accesstoken, tokenSecret, profile, done) {
// Could be an existing user or a new user
// profile.username is used as the username
User.findOrCreate({
where: {
email: profile.emails,
displayName: profile.displayName
}
}).spread(function(user) {
return done(null, user);
});
}));
// This just stores the username is an encrypted browser cookie
passport.serializeUser(function(user, done) {
done(null, user.username);
});
// This fetches the user by username retrieved from the
// cookie that was set during serializeUser
passport.deserializeUser(function(uname, done) {
User.find({
where: {
username: uname
}
}).then(function(user) {
if (!user) return done(new Error('Invalid user'));
return done(null, user);
});
});
// Redirect the user to facebook for authentication. When complete, Facebook
// will redirect the user back to the application at /auth/facebook/callback
//app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook',
passport.authenticate('facebook', { scope: ['email']}),
function(req, res){
});
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If access
// was granted, the user will be logged in. Otherwise, authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
failureRedirect: '/login'
}),
function(req, res) {
res.cookie('signIn', 'true');
res.redirect('/');
}
);
// This is the middleware that needs to be used for
// protecting APIs that require authorization
return function(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the login page /auth/twitter
res.redirect('/auth/facebook');
};
};
知道为什么 sequelize 会抛出这个错误吗? "value" 列是什么?
配置文件是 { value, type }
个对象的数组 http://passportjs.org/docs/profile
所以你应该通过 profile.emails[0].value
或 profile.emails.map(p => p.value)
我正在尝试通过 passport.js 使用 facebook OAuth 进行身份验证,但 sequelizejs 抛出以下错误:
server-0(错误):未处理的拒绝 SequelizeDatabaseError:列 "value" 不存在
我的用户模型如下所示:
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
username: {
type: DataTypes.STRING,
allowNull: true,
validate: {
}
},
displayName: {
type: DataTypes.STRING,
allowNull: true,
validate: {
}
},
email: {
type: DataTypes.STRING(256),
allowNull: false,
validate: {
isEmail: true,
len: [5, 256]
}
},
password: {
type: DataTypes.STRING,
allowNull: false,
validate: {
}
}
}, {
// lowercase tableName in Posrgres, if you need.
tableName: 'users',
classMethods: {
associate: function(models) {
User.hasMany(models.Candidate);
User.hasMany(models.Employer);
User.hasMany(models.Topic);
User.hasMany(models.Reply);
User.hasMany(models.Notification);
}
}
});
return User;
};
我的 passport.js 文件如下所示:
'use strict';
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
module.exports = function(app, User) {
app.use(passport.initialize());
// Enable sessions
app.use(passport.session());
passport.use(new FacebookStrategy({
clientID: 652109234923404,
clientSecret: "f126af0ec55ca0c2bc8c7cb914b7cb6b",
callbackURL: "http://localhost:5000/auth/facebook/callback"
},
function(accesstoken, tokenSecret, profile, done) {
// Could be an existing user or a new user
// profile.username is used as the username
User.findOrCreate({
where: {
email: profile.emails,
displayName: profile.displayName
}
}).spread(function(user) {
return done(null, user);
});
}));
// This just stores the username is an encrypted browser cookie
passport.serializeUser(function(user, done) {
done(null, user.username);
});
// This fetches the user by username retrieved from the
// cookie that was set during serializeUser
passport.deserializeUser(function(uname, done) {
User.find({
where: {
username: uname
}
}).then(function(user) {
if (!user) return done(new Error('Invalid user'));
return done(null, user);
});
});
// Redirect the user to facebook for authentication. When complete, Facebook
// will redirect the user back to the application at /auth/facebook/callback
//app.get('/auth/facebook', passport.authenticate('facebook'));
app.get('/auth/facebook',
passport.authenticate('facebook', { scope: ['email']}),
function(req, res){
});
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If access
// was granted, the user will be logged in. Otherwise, authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
failureRedirect: '/login'
}),
function(req, res) {
res.cookie('signIn', 'true');
res.redirect('/');
}
);
// This is the middleware that needs to be used for
// protecting APIs that require authorization
return function(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the login page /auth/twitter
res.redirect('/auth/facebook');
};
};
知道为什么 sequelize 会抛出这个错误吗? "value" 列是什么?
配置文件是 { value, type }
个对象的数组 http://passportjs.org/docs/profile
所以你应该通过 profile.emails[0].value
或 profile.emails.map(p => p.value)