簡體   English   中英

firebase auth中如何切換郵箱鏈接和密碼登錄方式?

[英]How to switch between emailLink and password sign-in method in firebase auth?

用戶可以通過 email 登錄的方法有兩種:“emailLink”和“password”(參見https://firebase.google.com/docs/auth/web/email-link-auth#differentiating_emailpassword_from_email_link

我想讓我的用戶可以選擇在這兩種方法之間切換。 但是,我沒有找到任何有關如何取消鏈接 email 登錄方法(例如密碼)的文檔。

據我了解,您只能在用戶擁有多個提供商 ID 時取消鏈接登錄方法,但電子郵件鏈接和密碼共享相同的 providerId(奇怪的是 emailLink providerID 稱為“密碼”)。

謝謝你的幫助!

編輯

當從 emailLink 切換到 password 時,以下解決方案的一個主要缺陷是切換后 email 將不再被驗證。 所以在這種情況下,最好調用updatePassword 這將導致用戶有兩個 email 登錄,“password”和“emailLink”(由fetchSignInMethodsForEmail返回)。 當用戶切換回“emailLink”時,您可以使用下面的解決方案, fetchSignInMethodsForEmail的結果將只包含“emailLink”。


所以我找到了解決方案:如果您從 emailLink 切換到 password,您只需使用以下命令:


// 1) get credential from use input
const credential = EmailAuthProvider.credential(email, newPassword)
// 2) unlink email link authentication
// https://firebase.google.com/docs/auth/web/account-linking#unlink-an-auth-provider-from-a-user-account
const newUser = await unlink(currentUser, "password")
// 3) link with new credential
const userCredential = await linkWithCredential(newUser, credential)

現在,如果我們想從密碼切換回電子郵件鏈接,我們首先必須向客戶端發送一個包含登錄鏈接的 email。 一旦用戶單擊登錄鏈接,我們將再次創建憑據並以相同的方式繼續。

請注意,如果第 2 步和第 3 步之間出現問題,用戶就完蛋了,我們必須特別注意這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM