2016-07-29 26 views
0

我有一個iOS應用程序,它使用Firebase作爲後端進行身份驗證。在Firebase 3中重新使用acccess令牌3

一旦用戶登錄並關閉應用程序,我不希望用戶必須重新輸入他們的電子郵件和密碼。我的方法是在成功登錄鑰匙串後保存訪問令牌,然後當用戶回到應用程序時,使用鑰匙串中的令牌進行登錄。

我試過使用方法FIRAuth.auth()?.signInWithCustomToken(customToken) { (user, error) in但這不是很正確,因爲這是使用自定義令牌時,這不是我正在做的。

有沒有辦法讓我這樣做?

// login with email/password 
FIRAuth.auth()?.signInWithEmail(email, password: password, completion: { (firebaseUser, error) in 
    if error == nil { 
     FIRAuth.auth()!.currentUser!.getTokenWithCompletion({ (token, error) in 
     if error == nil { 
      // save token to keychain 
     } else { 
      print(error) 
     } 
     }) 

    } else { 
    print(error) 
    } 
}) 


// user comes back to app 
do { 
// get saved token from keychain 
    if let myToken = try keychain.get("token") { 
    FIRAuth.auth()?.signInWithCustomToken(myToken, completion: { (user: FIRUser?, error: NSError?) in 
     if error == nil { 
     // show post login screen 
     } else { 

     } 
    }) 
    } 
} catch { 
// error getting token from keychain 
} 

}

回答

0

我在錯誤的方式處理這個問題。使用第三方身份驗證提供商(如Facebook,Google等)並從其中一個服務獲取OAuth令牌時,保存令牌是適當的。

在我的情況下,當使用電子郵件和密碼登錄時,不需要令牌,而是可以將密碼安全地保存在鑰匙串中並在稍後用於登錄。