使用Facebook登录使用REST API将新用户添加到私有数据库

时间:2017-02-17 01:52:33

标签: facebook rest ionic-framework passport.js

我花了一天时间研究这个问题,并且认为我可以自己解决这个问题,但没有运气。

在这种情况下,我在移动应用中使用了FB身份验证。这工作正常。用户可以登录,我可以访问公共个人资料等。

问题是我希望有新用户或第一次登录在我的私人数据库中创建新记录,因为我希望存储的不仅仅是电子邮件和FB ID。例如,最喜欢的项目。我也可以通过帖子轻松完成这项工作(www.mywebsite.com/api/users /)。

所以,真正的问题是,我不想让一条只允许人们将用户添加到我的数据库的路线,不知不觉。

有没有办法将访问令牌传递给API路由,以确保用户只能将新的用户记录添加到db中,如果他们拥有有效的FB登录?

我已经成立了着名的" auth / facebook"网络上流行的路线也很好用,直到我从我的应用程序访问它。然后抛出X-origin错误(我相信部分原因是回调路由)。

这篇文章很相似,但仍然没有完全覆盖它。 Authenticating against a REST API with iOS client using Facebook SSO as the only login mechanism

请帮忙!

谢谢, 韦恩

1 个答案:

答案 0 :(得分:0)

经过两天的挖掘,我找到了一个相当简单的解决方案。

facebook图形API将允许您提交访问令牌以接收基本用户数据。

所以现在我的解决方案是:

1. Use cordova-facebook4 plugin to authenticate within the app. (You can find details here: https://github.com/jeduan/cordova-plugin-facebook4
2. Send the FB id AND authentication token to my server route, like this:  `myapiserver.com:1234/auth/facebook/<token>/<id>`
3. On the server side, send an ajax request to https://graph.facebook.com/me?access_token=<token received from the
cordova plugin>. This will return the name and FB ID as JSON.
4. On the server, compare the ID returned from facebook to the ID sent with the request. If they match, add the new user, if not, do
not add the user and return an error.  Of course, the server will
not allow a new account to be added to the system if that FB id is
already in use.

此时,可能造成的最大伤害似乎是某人破解了另一个人的FB帐户,获取了他们的令牌和身份证,然后使用该帐户在我的系统上为该人创建用户。这似乎毫无意义,我不会担心这些攻击。

如果您知道更好的解决方案或错过了安全漏洞,请发表评论。

  

参考:How to get the Facebook user id using the access token