我使用护照js进行用户凭据管理和mongoose架构文档:
var customerSchema = new Schema({
info: {
firstname: String,
lastname: String,
telephone: String,
fax: String
},
local: {
email: {
type: String
},
password: {
type: String
}
},
facebook: {
id: String,
token: String,
email: String,
name: String,
photo: String
},
google: {
id: String,
token: String,
email: String,
name: String,
photo: String
},
status: String
}, {
timestamps: true
});
使用passportjs登录并添加提供商名称时:
passport.use('local.login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function(req, email, password, done) {
req.checkBody('email', 'Email address invalid.').notEmpty().isEmail();
req.checkBody('password', 'Password invalid.').notEmpty();
var errors = req.validationErrors();
if (errors) {
var messages = [];
errors.forEach(function(error) {
messages.push(error.msg);
});
return done(null, false, req.flash('error', messages));
}
//find user
Customer.findOne({
'local.email': email
}, function(err, customer) {
if (err) {
return done(err);
}
if (!customer) {
return done(null, false, {
message: 'Customer not found.'
});
}
if (!customer.validPassword(password)) {
return done(null, false, {
message: 'Customer info invalid.'
});
}
customer.provider = "local"; //Add more var to customer info
console.log("User Info: " + customer.provider);
return done(null, customer);
});
}));
passport.serializeUser(function(user, done) {
console.log("Serialize User: " + user);
done(null, user._id);
});
当使用本地护照或facebook登录时,我想在护照会话中添加自定义字段返回提供商信息传递给模板。
那么我怎么能在Passport中做到这一点?
更新1:
定义了var:
var provider = null;
在:
...
if (!customer.validPassword(password)) {
return done(null, false, {
message: 'Customer info invalid.'
});
}
provider = "local"; //Add more var to customer info
return done(null, customer);
...
我使用@Love-Kesh帮助代码:
passport.deserializeUser(function(customerId, done) {
Customer.findById(customerId, function(err, user) {
var newUser = user.toObject();
newUser['provider'] = provider;
done(err, newUser);
});
});
它适合我的意思。提供者通过本地方法返回动态值,facebook ...
答案 0 :(得分:0)
如果我没错,您想在passport session
上添加额外信息。
所以,如果你想在护照会话上添加自定义信息,那么你可以在deserializeUser
passport.deserializeUser(function(id, done) {
getUser(id).then(function(user) {
user.anything = 'whatever you want';
return done(null, user);
});
});
您可以再获得一个SO link
答案 1 :(得分:0)
passport.use('local.login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
}, function(req, email, password, done) {
req.checkBody('email', 'Email address invalid.').notEmpty().isEmail();
req.checkBody('password', 'Password invalid.').notEmpty();
var errors = req.validationErrors();
if (errors) {
var messages = [];
errors.forEach(function(error) {
messages.push(error.msg);
});
return done(null, false, req.flash('error', messages));
}
//check if user local
//need to pass provider from client side
if(req.body.provider=='local'){
//find user
Customer.findOne({
'local.email': email
}, function(err, customer) {
if (err) {
return done(err);
}
if (!customer) {
return done(null, false, {
message: 'Customer not found.'
});
}
if (!customer.validPassword(password)) {
return done(null, false, {
message: 'Customer info invalid.'
});
}
customer.provider = "local"; //Add more var to customer info
console.log("User Info: " + customer.provider);
return done(null, customer);
});
}else{
//do whatever you want
}
});
}
}));
passport.serializeUser(function(user, done) {
console.log("Serialize User: " + user);
//pass complete user here
done(null, user);
});
更新以下代码
passport.deserializeUser(function(loggedUser, done) {
var provider = loggedUser.provider;
Customer.findById(loggedUser.id,function(error, user) {
//need to convert into object
var newUser=user.toObject();
newUser['provider']=provider;
return done(null, newUser);
});
//or return direct return done(null, loggedUser);
});