【发布时间】:2015-01-06 22:28:49
【问题描述】:
我正在开发一个 nodejs 项目。 我正在使用passport.js 进行身份验证,它运行良好,但是当我尝试使用 soundcloud 获取某些人的用户名时遇到了麻烦。
这是我用来在我的数据库中存储个人资料信息的代码部分:
function findOrCreateUser (accessToken, refreshToken, profile, done) {
User.findOne({ oauthID: profile.id }, function(err, user) {
if(err) console.log(err);
if (!err && user != null) done(null, user);
else {
var user = new User( {
oauthID : profile.id,
name : profile.displayName,
created : Date.now(),
pwd : 'no-pwd',
mail : '',
path : ''
});
// temporary path using mongo _id
user.path = user._id;
console.log( "familyName " + profile.familyName ); // undefined
console.log( "givenName " + profile.givenName ); // undefined
console.log( "middleName " + profile.middleName ); // undefined
console.log("displayName "+ profile.displayName ); // empty if the user didn't fill the field
if (profile.displayName == '') user.name = "Jacky Chan";
// if email exists...(i cant access it on soundcloud but i don't really need it
if(typeof(profile.emails) != 'undefined') user.mail = profile.emails[0].value;
user.save(function(err) {
if(err) {
console.log(err);
}
else {
console.log("saving user " + user.name );
done(null, user);
};
});
};
});
}
好的,这里一切正常,profile.displayName 根据passport.js documentation 返回一个字符串。 问题是这个字符串是用不需要的用户的名字和姓氏获得的,所以它通常是空的。唯一必填字段是用户名。
这张图片说明了这一点:
所以我的问题是:
如何使用passport.js获取用户名?
非常感谢任何帮助! :)
【问题讨论】:
标签: node.js api authentication soundcloud passport.js