您可以通过将身份验证提供方凭据关联至现有用户帐号,让用户可以使用多个身份验证提供方登录您的应用。无论用户使用哪个身份验证提供方服务登录,均可通过同一 Firebase 用户 ID 识别用户。例如,使用密码登录的用户可以关联 Google 帐号,以后便可使用这两种方法中的任意一种登录。或者,匿名用户可以关联 Facebook 帐号,以后就可以使用 Facebook 登录,然后继续使用您的应用。
准备工作
为您的应用增加对两个或多个身份验证提供方(可能包括匿名身份验证)的支持。
将联合身份验证提供方凭据与用户帐号关联
如需将身份验证提供方(例如 Google 或 Facebook)的凭据与现有用户帐号关联,请按以下步骤操作:
- 使用任意身份验证提供方或方法让用户登录。
- 获取要将其与用户帐号相关联的提供方所对应的
AuthProvider
对象。示例:var googleProvider = new firebase.auth.GoogleAuthProvider(); var facebookProvider = new firebase.auth.FacebookAuthProvider(); var twitterProvider = new firebase.auth.TwitterAuthProvider(); var githubProvider = new firebase.auth.GithubAuthProvider();
- 提示用户使用要关联的提供方进行登录。您可以提示您的用户,让其通过打开弹出式窗口或重定向到提供方的登录页面进行登录。在移动设备上最好使用重定向方法。
- 要使用弹出式窗口登录,请调用
linkWithPopup
:auth.currentUser.linkWithPopup(provider).then(function(result) { // Accounts successfully linked. var credential = result.credential; var user = result.user; // ... }).catch(function(error) { // Handle Errors here. // ... });
- 如需通过重定向到提供方的登录页面进行登录,请调用
linkWithRedirect
:auth.currentUser.linkWithRedirect(provider) .then(/* ... */) .catch(/* ... */);
用户在登录之后会被重定向回您的页面。然后,您可以在页面加载时调用getRedirectResult
来检索登录结果:auth.getRedirectResult().then(function(result) { if (result.credential) { // Accounts successfully linked. var credential = result.credential; var user = result.user; // ... } }).catch(function(error) { // Handle Errors here. // ... });
如果凭据已与另一用户帐号关联,帐号关联将失败。在这种情况下,您必须根据需要为您的应用合并帐号和相关数据:
// The implementation of how you store your user data depends on your application var repo = new MyUserDataRepo(); // Get reference to the currently signed-in user var prevUser = auth.currentUser; // Get the data which you will want to merge. This should be done now // while the app is still signed in as this user. var prevUserData = repo.get(prevUser); // Delete the user's data now, we will restore it if the merge fails repo.delete(prevUser); // Sign in user with the account you want to link to auth.signInWithCredential(newCredential).then(function(result) { console.log("Sign In Success", result); var currentUser = result.user; var currentUserData = repo.get(currentUser); // Merge prevUser and currentUser data stored in Firebase. // Note: How you handle this is specific to your application var mergedData = repo.merge(prevUserData, currentUserData); return prevUser.linkWithCredential(result.credential) .then(function(linkResult) { // Sign in with the newly linked credential return auth.signInWithCredential(linkResult.credential); }) .then(function(signInResult) { // Save the merged data to the new user repo.set(signInResult.user, mergedData); }); }).catch(function(error) { // If there are errors we want to undo the data merge/deletion console.log("Sign In Error", error); repo.set(prevUser, prevUserData); });
- 要使用弹出式窗口登录,请调用
将电子邮件地址和密码凭据与用户帐号相关联
如需将电子邮件地址和密码凭据添加到现有用户帐号,请按以下步骤操作:
- 使用任意身份验证提供方或方法让用户登录。
- 提示用户输入电子邮件地址和新密码。
- 使用电子邮件地址和密码创建一个
AuthCredential
对象:var credential = firebase.auth.EmailAuthProvider.credential(email, password);
将
AuthCredential
对象传递给已登录用户的linkWithCredential
方法:auth.currentUser.linkWithCredential(credential) .then(function(usercred) { var user = usercred.user; console.log("Account linking success", user); }).catch(function(error) { console.log("Account linking error", error); });
如果凭据已与另一用户帐号关联,则无法调用
linkWithCredential
。在这种情况下,您必须根据需要为您的应用合并帐号和相关数据(请参阅上方示例)
解除身份验证提供方与用户帐号的关联
您可以解除身份验证提供方与帐号的关联,这样用户就无法再通过该提供方登录了。
如需解除身份验证提供方与用户帐号的关联,请将提供方 ID 传递给 unlink
方法。您可以从 providerData
属性中获取与用户相关联的身份验证提供方的 ID。
user.unlink(providerId).then(function() { // Auth provider unlinked from account // ... }).catch(function(error) { // An error happened // ... });