Kimlik doğrulama sağlayıcısı kimlik bilgilerini mevcut bir kullanıcı hesabına bağlayarak kullanıcıların birden fazla kimlik doğrulama sağlayıcısı kullanarak uygulamanızda oturum açmasına izin verebilirsiniz. Kullanıcılar, oturum açmak için kullandıkları kimlik doğrulama sağlayıcısından bağımsız olarak aynı Firebase kullanıcı kimliğiyle tanımlanabilir. Örneğin, şifreyle oturum açan bir kullanıcı, bir Google hesabını bağlayabilir ve gelecekte bu yöntemlerden herhangi birini kullanarak oturum açabilir. Veya anonim bir kullanıcı bir Facebook hesabını bağlayabilir ve daha sonra uygulamanızı kullanmaya devam etmek için Facebook'ta oturum açabilir.
Sen başlamadan önce
Uygulamanıza iki veya daha fazla kimlik doğrulama sağlayıcısı (muhtemelen anonim kimlik doğrulama dahil) için destek ekleyin.
Birleşik kimlik doğrulama sağlayıcısı kimlik bilgilerini bir kullanıcı hesabına bağlama
Google veya Facebook gibi bir kimlik doğrulama sağlayıcısının kimlik bilgilerini mevcut bir kullanıcı hesabına bağlamak için:
- Herhangi bir kimlik doğrulama sağlayıcısını veya yöntemini kullanarak kullanıcının oturumunu açın.
- Kullanıcının hesabına bağlamak istediğiniz sağlayıcıya karşılık gelen
AuthProvider
nesnesini alın. Örnekler:Web modular API
import { GoogleAuthProvider, FacebookAuthProvider, TwitterAuthProvider, GithubAuthProvider } from "firebase/auth"; const googleProvider = new GoogleAuthProvider(); const facebookProvider = new FacebookAuthProvider(); const twitterProvider = new TwitterAuthProvider(); const githubProvider = new GithubAuthProvider();
Web namespaced API
var googleProvider = new firebase.auth.GoogleAuthProvider(); var facebookProvider = new firebase.auth.FacebookAuthProvider(); var twitterProvider = new firebase.auth.TwitterAuthProvider(); var githubProvider = new firebase.auth.GithubAuthProvider();
- Kullanıcıdan bağlamak istediğiniz sağlayıcıda oturum açmasını isteyin. Bir açılır pencere açarak veya sağlayıcının oturum açma sayfasına yönlendirerek kullanıcılarınızdan oturum açmalarını isteyebilirsiniz. Mobil cihazlarda yönlendirme yöntemi tercih edilmektedir.
- Açılır pencereyle oturum açmak için
linkWithPopup
öğesini çağırın:Web modular API
import { getAuth, linkWithPopup, GoogleAuthProvider } from "firebase/auth"; const provider = new GoogleAuthProvider(); const auth = getAuth(); linkWithPopup(auth.currentUser, provider).then((result) => { // Accounts successfully linked. const credential = GoogleAuthProvider.credentialFromResult(result); const user = result.user; // ... }).catch((error) => { // Handle Errors here. // ... });
Web namespaced API
auth.currentUser.linkWithPopup(provider).then((result) => { // Accounts successfully linked. var credential = result.credential; var user = result.user; // ... }).catch((error) => { // Handle Errors here. // ... });
- Sağlayıcının oturum açma sayfasına yönlendirerek oturum açmak için
linkWithRedirect
arayın: 'linkWithRedirect'i kullanırken en iyi uygulamaları izleyin.Kullanıcı oturum açtıktan sonra sayfanıza geri yönlendirilir. Ardından, sayfanız yüklendiğindeWeb modular API
import { getAuth, linkWithRedirect, GoogleAuthProvider } from "firebase/auth"; const provider = new GoogleAuthProvider(); const auth = getAuth(); linkWithRedirect(auth.currentUser, provider) .then(/* ... */) .catch(/* ... */);
Web namespaced API
auth.currentUser.linkWithRedirect(provider) .then(/* ... */) .catch(/* ... */);
getRedirectResult
çağırarak oturum açma sonucunu alabilirsiniz:Web modular API
import { getRedirectResult } from "firebase/auth"; getRedirectResult(auth).then((result) => { const credential = GoogleAuthProvider.credentialFromResult(result); if (credential) { // Accounts successfully linked. const user = result.user; // ... } }).catch((error) => { // Handle Errors here. // ... });
Web namespaced API
auth.getRedirectResult().then((result) => { if (result.credential) { // Accounts successfully linked. var credential = result.credential; var user = result.user; // ... } }).catch((error) => { // Handle Errors here. // ... });
Kimlik bilgileri zaten başka bir kullanıcı hesabına bağlıysa hesap bağlama işlemi başarısız olur. Bu durumda, hesapları ve ilişkili verileri birleştirme işlemini uygulamanıza uygun şekilde gerçekleştirmelisiniz:
Web modular API
import { getAuth, signInWithCredential, linkWithCredential, OAuthProvider } from "firebase/auth"; // The implementation of how you store your user data depends on your application const repo = new MyUserDataRepo(); // Get reference to the currently signed-in user const auth = getAuth(); const 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. const 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 signInWithCredential(auth, newCredential).then((result) => { console.log("Sign In Success", result); const currentUser = result.user; const currentUserData = repo.get(currentUser); // Merge prevUser and currentUser data stored in Firebase. // Note: How you handle this is specific to your application const mergedData = repo.merge(prevUserData, currentUserData); const credential = OAuthProvider.credentialFromResult(result); return linkWithCredential(prevUser, credential) .then((linkResult) => { // Sign in with the newly linked credential const linkCredential = OAuthProvider.credentialFromResult(linkResult); return signInWithCredential(auth, linkCredential); }) .then((signInResult) => { // Save the merged data to the new user repo.set(signInResult.user, mergedData); }); }).catch((error) => { // If there are errors we want to undo the data merge/deletion console.log("Sign In Error", error); repo.set(prevUser, prevUserData); });
Web namespaced API
// 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((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((linkResult) => { // Sign in with the newly linked credential return auth.signInWithCredential(linkResult.credential); }) .then((signInResult) => { // Save the merged data to the new user repo.set(signInResult.user, mergedData); }); }).catch((error) => { // If there are errors we want to undo the data merge/deletion console.log("Sign In Error", error); repo.set(prevUser, prevUserData); });
- Açılır pencereyle oturum açmak için
E-posta adresini ve parola kimlik bilgilerini bir kullanıcı hesabına bağlama
Mevcut bir kullanıcı hesabına e-posta adresi ve parola kimlik bilgileri eklemek için:
- Herhangi bir kimlik doğrulama sağlayıcısını veya yöntemini kullanarak kullanıcının oturumunu açın.
- Kullanıcıdan bir e-posta adresi ve yeni şifre isteyin.
- E-posta adresi ve parolayla bir
AuthCredential
nesnesi oluşturun:Web modular API
import { EmailAuthProvider } from "firebase/auth"; const credential = EmailAuthProvider.credential(email, password);
Web namespaced API
var credential = firebase.auth.EmailAuthProvider.credential(email, password);
AuthCredential
nesnesini oturum açmış kullanıcınınlinkWithCredential
yöntemine iletin:Web modular API
import { getAuth, linkWithCredential } from "firebase/auth"; const auth = getAuth(); linkWithCredential(auth.currentUser, credential) .then((usercred) => { const user = usercred.user; console.log("Account linking success", user); }).catch((error) => { console.log("Account linking error", error); });
Web namespaced API
auth.currentUser.linkWithCredential(credential) .then((usercred) => { var user = usercred.user; console.log("Account linking success", user); }).catch((error) => { console.log("Account linking error", error); });
Kimlik bilgileri zaten başka bir kullanıcı hesabına bağlıysa
linkWithCredential
çağrısı başarısız olur. Bu durumda, hesapları ve ilişkili verileri birleştirme işlemini uygulamanıza uygun şekilde yapmanız gerekir (yukarıdaki örneğe bakın).
Kimlik doğrulama sağlayıcısının kullanıcı hesabıyla bağlantısını kaldırma
Kullanıcının artık o sağlayıcıda oturum açamaması için, bir kimlik doğrulama sağlayıcısının bir hesapla olan bağlantısını kaldırabilirsiniz.
Bir kimlik doğrulama sağlayıcısının kullanıcı hesabıyla olan bağlantısını kaldırmak için sağlayıcı kimliğini unlink
yöntemine iletin. Bir kullanıcıya bağlı kimlik doğrulama sağlayıcılarının sağlayıcı kimliklerini, providerData
özelliğinden alabilirsiniz.
Web modular API
import { getAuth, unlink } from "firebase/auth"; const auth = getAuth(); unlink(auth.currentUser, providerId).then(() => { // Auth provider unlinked from account // ... }).catch((error) => { // An error happened // ... });
Web namespaced API
user.unlink(providerId).then(() => { // Auth provider unlinked from account // ... }).catch((error) => { // An error happened // ... });