解析服务器社交登录

时间:2017-05-18 19:16:57

标签: javascript parse-server google-login

我正在开发基于Parse-server的应用程序,我想提供社交登录。我在文档http://docs.parseplatform.org/js/guide/#linking-users中找到了此指南。

我开始通过谷歌实施社交登录。我做了以下步骤:

1)我在ParseServer设置中添加了以下行

var api = new ParseServer({
...
    auth:{
        google: {}
    },
...
});

2)我在客户端通过hello.js进行了身份验证(在登录时调用user._linkWith函数)

hello.init({
  google: 'My Google id'
});

hello.on('auth.login', function(auth) {
  // Call user information, for the given network
  hello(auth.network).api('me').then(function(r) {
    const user = new Parse.User();
    user._linkWith(auth.network, auth.authResponse).then(function(user){
      console.log('You are logged in successfully.');
    });
  });
});

当我调试它时,我发现当提供者对象正在准备时,它在 _linkWith()函数中失败了。对象AuthProviders应该存储所有提供者,为空。因此,声明 provider = authProviders [' google']; 会导致未定义。调用 provider.authenticate(...); 会导致错误"无法读取属性'验证'未定义"

我错过了什么或我做错了什么?

感谢您的所有答案。

洪扎

2 个答案:

答案 0 :(得分:1)

您是否注册了authenticationProvider?您可以在我们的单元测试中找到有关如何执行此操作的示例:

https://github.com/parse-community/parse-server/blob/5813fd0bf8350a97d529e5e608e7620b2b65fd0c/spec/AuthenticationAdapters.spec.js#L139

答案 1 :(得分:1)

我也遇到了这个错误并查看了_linkWith(provider,options)源代码。它会检查options是否有authData字段(反过来应包含id和凭据)。如果是,则使用options.authData。否则,它会依赖于查找上一个答案中提到的先前注册的身份验证提供程序。

这是我使用的代码片段:

const authData = {
    "id": profile.getId(),
    "id_token": id_token
}
const options = {
    "authData": authData
}
const user = new Parse.User();
user._linkWith('google', options).then(function(user) {
    console.log('Successful user._linkWith(). returned user=' + JSON.stringify(user))
}, function(error) {
    console.log('Error linking/creating user: ' + error)
    alert('Error linking/creating user: ' + error)
    // TODO handle error
})