当同一用户在不同提供商之间有多个帐户时,如何在我的网站上使用 X 登录?

How can I sign in with X on my web site, when there are multiple accounts across providers for the same user?

如果我有一个网站可以登录多个不同的提供商(比如 Facebook,GitHub,Google),我应该使用什么作为本地到我的-用户的站点唯一标识符?例如,如果这两个步骤发生:

  1. 我使用 GitHub 登录(这是第一次),我的用户名是 mogronalol,电子邮件是 mogronalol@mogronalol.com。
  2. 已创建电子邮件地址为 mogronalol@mogronalol.com 且用户名为 mogronalol 的“本地到我的站点”帐户。

如果我使用电子邮件地址作为本地唯一标识符,如果我的电子邮件地址在 GitHub 中更改为 other@other.com 会怎样?同样的问题也适用于更改用户名。

如果我从 GitHub 获得某种唯一 ID 并将其用作标识符,那么如果我的电子邮件地址或用户名在 GitHub 中发生更改,我该怎么办?我每次登录时是否只是将我的本地到我的站点副本更新为与 GitHub 上的副本相同?

当然,如果我想用我的 Facebook 帐户和 GitHub 帐户登录,这个问题会更糟。如果我的电子邮件地址和/或用户名在这两个地方都不同,会发生什么情况?我的本地站点如何知道 link 这些帐户在一起?如果帐户 linked 后电子邮件地址之类的东西不同,我应该使用哪个?

首先,也许您可​​以尝试一些教程来感受 OAuth 的工作原理。

OAuth认证成功后,您的网站将收到OAuth提供商提供的一系列信息。example

在此信息中,有两个名为 uidprovider 的特殊列用于识别来自 OAuth 提供商的用户。

您将使用这两列来判断身份验证来自哪个提供商(即 facebookgithub),您还需要将这些字段保存到您的帐户列中。

然后使用其余信息在您的网站中创建帐户。

例如,使用 OAuth 提供商的电子邮件作为 email(github 的电子邮件作为电子邮件)。

创建帐户后,每次从 OAuth 提供商登录服务器时。

您只需勾选账户栏中的provideruid即可。

让我们回到你的问题。

If I use the email address as the local unique identifier, what happens if my email address changes in GitHub to other@other.com? The same question applies to changing username also.

If I got some sort of unique ID from GitHub, and used that as the identifier, then what do I do if my email address or username changes in GitHub. Do I just updated my local-to-my-site-copy to be the same as the one on GitHub each time I log in?

Github 的电子邮件或用户更改不会影响您的登录(我们只检查 provideruid 字段以登录用户)。

我建议不要与您的 OAuth 提供者的信息同步(我们只在创建帐户时使用 OAuth 提供者的信息)。

如果您要支持多个 OAuth 提供商,我建议您仔细阅读 this article

您必须将 uidprovider 与其他 table 称为身份分开。

每个账号都有多个身份。

我以前也做过

如果您不介意,here 是处理多个 OAuth 提供程序的示例代码片段。

您必须考虑登录流程中的逻辑。

例如,用户已登录并使用 OAuth 提供商登录 OAuth => Link 帐户

用户未登录并登录 OAuth => 如果找到使用 OAuth 的用户,请登录,否则使用 OAuth 提供商的信息创建帐户

Of course, this problem is worsened if I want to log in with my Facebook account as well as my GitHub account. What happens if my email address and / or username are different across both of these? How would my local site know to link the accounts together? And if things like email address are different once the accounts are linked, which one do I use?

当用户已经登录时,我们只有 link 帐户。

当您link帐户时,您可以决定使用OAuth提供者的信息来更新帐户(就像您以前注册帐户一样)。

我建议使用原始电子邮件,不要从 OAuth 提供商的信息更新它。