我正在为 Meteor 使用 Useraccounts 包。
我已经配置好了,用户名/电子邮件+密码登录正在使用登录时显示的用户名(用户需要在注册时输入用户名)。
但是,当我单击社交登录按钮登录时,我已登录但没有注册用户名。因此,无论我做什么,用户名日志都不会显示任何内容,即没有用户名。
我按照以下链接进行配置: useraccounts link to 3rd party login
我在 lib/config.js 中有以下内容
ServiceConfiguration.configurations.update(
{ "service": "facebook" },
{
$set: {
"appId": "XXXXXXXXXXXXXXX",
"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
},
{ upsert: true }
);
下面修改邮箱/密码登录
var pwd =
AccountsTemplates.removeField('password');
AccountsTemplates.removeField('email');
AccountsTemplates.addFields([
{
_id: "username",
type: "text",
required: true,
displayName: "username",
minLength: 5,
errStr: 'error.minChar'
},
我可能错过了它,但我没有看到任何关于从第 3 方登录中提取用户名的讨论。有谁知道如何进一步配置?例如,如果 Facebook 或 Twitter 用户名是 ABC,我希望 ABC 在第 3 方社交登录时在应用中反射(reflect)为用户名。
更新
Accounts.onCreateUser(function(options, user){
if(user.services){
if(user.services.google) {
user.username = user.services.google.username;
}
else if(user.services.twitter) {
user.username = user.services.twitter.username;
}
else if(user.services.facebook) {
user.username = user.services.facebook.username;
}
else if(user.services.github) {
user.username = user.services.github.username;
}
}
if(options.profile){
user.profile = options.profile;
}
return user;
});
最佳答案
您可以在创建用户时注册服务器端 Hook ,并强制使用您想要的任何用户名,包括来自第 3 方服务的用户名。
https://docs.meteor.com/#/full/accounts_oncreateuser
Accounts.onCreateUser(function(options, user){
if(user.services){
if(user.services.google){
user.username = user.services.google.name;
}
else if(user.services.twitter){
[..]
}
[...]
}
//
if(options.profile){
user.profile = options.profile;
}
return user;
});
但是请注意,这不会强制用户名唯一性,因为 Facebook 上的 2 个人显然可以共享相同的名称,因此如果您的应用程序存在问题,您必须自己弄清楚。
关于javascript - 如何通过 Useraccounts 包从 3rd 方社交登录中提取用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30463958/