简体   繁体   English

如何在iOS应用中合并本地用户和Facebook用户

[英]How to combine local and Facebook users in iOS app

I want to give the opportunity for a user of an app to register/login with Facebook or by creating an account. 我想为应用程序的用户提供一个向Facebook注册/登录或创建帐户的机会。 I know that I can get the user's Facebook account email address, and their first and last names. 我知道我可以获取用户的Facebook帐户电子邮件地址,以及他们的名字和姓氏。 That's basically the only information for creating a 'local' app account, apart from a password. 除了密码之外,这基本上是创建“本地”应用程序帐户的唯一信息。 How can I make sure that if that person logs in to Facebook on another device, that their two devices are linked to the same 'local' account? 如果该人在另一台设备上登录Facebook,我如何确保他们的两个设备链接到同一个“本地”帐户? (ie if they choose to sign in with Facebook with 2 devices, I only want one local account to be created on my server for that user). (即,如果他们选择使用2台设备使用Facebook登录,我只希望在服务器上为该用户创建一个本地帐户)。

Ideally, I want the login schemes for both to be identical. 理想情况下,我希望两者的登录方案都相同。 So if that user logs in with Facebook, I can check (securely) that the FB account is linked to a 'local' account, and automatically log that device in without making the user type in a password. 因此,如果该用户使用Facebook登录,我可以(安全地)检查FB帐户是否已链接到“本地”帐户,并自动登录该设备,而无需输入用户密码。 Is this possible? 这可能吗?

Edit: The 'local' users will be stored in a database on my server, and the front end will be done in Python running alongside the API for the app. 编辑:“本地”用户将存储在我服务器上的数据库中,前端将在与应用程序API一起运行的Python中完成。 Note that 'local' is just referring to the fact that it uses my app web service rather than an external social network. 请注意,“本地”仅是指它使用我的应用程序网络服务而不是外部社交网络。

You can do that within your users database as per below: 您可以按照以下步骤在用户数据库中执行此操作:

assuming you store the user data in a table named userinfo , this table should contain user e-mail, first name, etc.. 假设您将用户数据存储在名为userinfo的表中,则该表应包含用户电子邮件,名字等。

Add another column in this table named fbemail. 在此表中添加另一列,名为fbemail。

If users signs in using web service, his email will be saved in the email field & the fbemail should be null, if signs in using FB, then both email & fbemail should be the extracted email. 如果用户使用Web服务登录,则他的电子邮件将保存在电子邮件字段中,并且fbemail应该为空;如果使用FB登录,则电子邮件和fbemail都应为提取的电子邮件。

when the user uses FB login, check the fbemail field, if not found, then this is a new user, add his data, if not, then this is a returning user, no need to add his data. 当用户使用FB登录时,请检查fbemail字段,如果未找到,则这是一个新用户,添加他的数据,如果没有,则这是一个回头用户,无需添加他的数据。

Option 1. 选项1。

You can identify your Facebook user by his Facebook User ID. 您可以通过Facebook用户ID来识别您的Facebook用户。 If he logs in using Facebook on other device you know it cause he sends you his Facebook User ID in the authentication process. 如果他在其他设备上使用Facebook登录,则您知道这是由于他在身份验证过程中向您发送了他的Facebook用户ID。 He also sends you Facebook access token which you validate contacting Facebook to see if it is correct. 他还会向您发送Facebook访问令牌,您可以验证该令牌是否与Facebook联系以查看是否正确。 Using this approach you have to have a different authentication scheme for Facebook user and "normal", email user. 使用这种方法,您必须为Facebook用户和“普通”电子邮件用户使用不同的身份验证方案。

Option 2. 选项2。

To have the same login scheme you can use Facebook to get user email and prepend it in the email text field in your registration screen. 要使用相同的登录方案,您可以使用Facebook获取用户电子邮件,并将其添加在注册屏幕的电子邮件文本字段中。 The user would need to additionally provide a password. 用户将需要另外提供密码。 This means that you are not really doing a Login with Facebook, but use Facebook to obtain an email (and any additional information) so the user does not have to type it. 这意味着您实际上并不是在使用Facebook登录,而是使用Facebook获取电子邮件(以及任何其他信息),因此用户不必键入电子邮件。

This is an old post but still very valid. 这是旧帖子,但仍然非常有效。 You are correct, anybody who has your FB email could potentially access your server rest-api and log into it. 您是正确的,拥有FB电子邮件的任何人都可以访问您的服务器rest-api并登录。 To access a backend service you will need to use as password the FB access token generated during the FB log-in. 要访问后端服务,您将需要使用FB登录期间生成的FB访问令牌作为密码。 This is stored in the device keychain and can be retrieved as: 这存储在设备钥匙串中,可以通过以下方式检索:

NSString *accessToken = [[FBSDKAccessToken currentAccessToken] tokenString];
NSString *userID = [[FBSDKAccessToken currentAccessToken] userID];

The topic of using a FB authentication system in parallel to a custom login/registration system is covered in this FB guide: Using Facebook Login with Existing Login Systems . 该FB指南涵盖了与自定义登录/注册系统并行使用FB身份验证系统的主题: 在现有登录系统中使用Facebook登录

In sum, different scenarios need to be addressed: 总之,需要解决不同的情况:

  • A person signs up for your app using their email and password, but later they want to use Facebook Login to obtain data from their Facebook account, to post to their timeline, or just to use to log in with in future. 一个人使用他们的电子邮件和密码来注册您的应用程序,但是后来他们想要使用Facebook登录从他们的Facebook帐户中获取数据,发布到他们的时间轴,或者只是用于将来的登录。
  • A person signs up for the app using their email and password, but later chooses to log in with Facebook separately. 一个人使用他们的电子邮件和密码来注册该应用程序,但后来选择分别使用Facebook登录。 This guide assumes that the email supplied first and the primary email associated with their Facebook account are the same. 本指南假定首先提供的电子邮件和与其Facebook帐户关联的主要电子邮件是相同的。
  • A person signs up for the app using Facebook Login and later wants to log in to this account using an email address and password. 有人使用Facebook登录名注册了该应用,后来又想使用电子邮件地址和密码登录该帐户。

The guide recommends using two different tables for the FB log-in and the custom login. 该指南建议使用两个不同的表进行FB登录和自定义登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM