2017-01-18 3 views
2

Swift iOS 앱에 Firebase Auth를 사용하고 있습니다. Google은 Firebase Auth UI를 "드롭 인"인증 시스템으로 사용할 것을 권장하지만 초기 로그인 만 처리합니다. 나는 사용자가 전자 메일 및 암호와 같은 프로필을 변경하도록 허용하고 있습니다.Firebase Auth UI는 어떻게 재 인증을 처리합니까?

이러한 변경을 만들기위한 문서는 어떤 변화가 최근에 로그인 한 것으로 사용자를 필요로 여러 곳에서 언급

(https://firebase.google.com/docs/auth/ios/manage-users#get_the_currently_signed-in_user 참조)

을 일부 보안에 민감한 행동-이러한 계정을 삭제로 설정 기본 전자 메일 주소 및 암호 변경 - 사용자가 최근에 로그인 한 상태 여야합니다. 이러한 작업 중 하나를 수행하고 사용자가 너무 오래 전에 서명 한 경우 작업은 FIRAuthErrorCodeCredentialTooOld 오류로 실패합니다.

우선 API의 어느 곳에서나 FIRAuthErrorCodeCredentialTooOld 오류가없는 것으로 보입니다.

둘째, 문서는이 코드 샘플과 함께이 문제를 해결하기 위해 reauthenticate(with:)를 사용하여 제안 :

let user = FIRAuth.auth()?.currentUser 
var credential: FIRAuthCredential 

// Prompt the user to re-provide their sign-in credentials 

user?.reauthenticate(with: credential) { error in 
    if let error = error { 
    // An error happened. 
    } else { 
    // User re-authenticated. 
    } 
} 

문제는, 내가 중포 기지 인증 UI를 사용했기 때문에, 나는 사용자의 자격 증명을 얻기위한 사용자 정의 UI가 없습니다.

필자는 현재이 오류가 발생했을 때 로그인 할 때 사용한 것과 동일한 Firebase Auth UI를 사용하여 재 인증 할 수 있다고 생각합니다. 그러나 이것이 이것이 허락 된 방법인가, 아니면 전혀 작동 할 것인지, 또는 앞으로도 계속 될 것인지는 모른다. Firebase Auth UI 코드베이스를 확인했는데 어디서나 reauthenticate()에 대한 호출이 없습니다. 설명서는이 오류가 발생했을 때 특별히이 메서드를 호출하는 것이므로 혼란 스럽습니다.

여러 공급자를 포함하여 재 인증을 수행하기 위해 전체 UI를 작성해야하는 경우 Firebase Auth UI 사용의 요점은 무엇입니까?

답변

1

오류 코드와 관련하여 설명서를 업데이트하면됩니다. 이제 오류 코드는 FIRAuthErrorCode.errorCodeRequiresRecentLogin입니다.

지금 UI 문제와 관련하여 UIAlertController에 사용자가 재 인증을 위해 비밀번호를 입력하는 데 사용할 수있는 텍스트 필드가 없습니까? 전체보기 컨트롤러를 만드는 것보다 훨씬 간단하고 사용자 친화적 인 방법입니다. 당신이 암호 재설정하지 않습니다 (사용자의 이메일을 변경하거나 계정을 삭제해야 할 때마다

// initialize the UIAlertController for password confirmation 
let alert = UIAlertController(title: "", message: "Please, enter your password:", preferredStyle: UIAlertControllerStyle.alert) 
// add text field to the alert controller 
alert.addTextField(configurationHandler: { (textField) in 
    textField.placeholder = "Password" 
    textField.autocapitalizationType = .none 
    textField.autocorrectionType = .no 
    textField.isSecureTextEntry = true 
}) 
// delete button action 
alert.addAction(UIAlertAction(title: "Delete account", style: UIAlertActionStyle.destructive, handler: {action in 
    // retrieve textfield 
    let txtFld = alert.textFields![0] 
    // init the credentials (assuming you're using email/password authentication 
    let credential = FIREmailPasswordAuthProvider.credential(withEmail: (FIRAuth.auth()?.currentUser?.email)!, password: txtFld.text!) 
    FIRAuth.auth()?.currentUser?.reauthenticate(with: credential, completion: { (error) in 
     if error != nil { 
      // handle error - incorrect password entered is a possibility 
      return 
     } 

     // reauthentication succeeded! 
    }) 
})) 
// cancel reauthentication 
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: {action in })) 

// finally, present the alert controller 
self.present(alert, animated: true, completion: nil) 

:

여기에 너무 많은 문제를 거치지 않고 사용자를 다시 인증 할 수있는 방법에 대한 매우 간단 샘플입니다 로그인이 필요합니다), 위의 스 니펫을 사용하여 비밀번호를 다시 입력 할 수있는 경고 컨트롤러를 팝업하십시오.

편집 : 위에 제공된 코드는 현재 사용자의 이메일을 강제로 제거하므로 해당 단계에서 사용자가 로그인했는지 확인하십시오. 그렇지 않으면 앱이 다운됩니다.

+0

물론 사용자가 Facebook이나 Google과 같은 제공 업체에 로그인 한 경우 어떻게해야합니까? 이 오류는 전자 메일 로그인을 사용할 때만 발생한다는 것은 없습니다. –

+0

그런 경우 먼저 로그인 방법을 묻는 프롬프트가 필요합니다. 당신은 아마도 경고 컨트롤러가 아닌 적절한 뷰 컨트롤러를 필요로 할 것입니다. – ThunderStruct

+1

감사합니다. 그것은 내가 예상했던 것입니다.그래서, 원래의 질문이 남아 있습니다 : Firebase Auth UI를 다시 사용할 수 있습니까? 어디서나 재 인증을 호출하지 않습니다. 하지만 다시 로그인하는 것이 좋으면 작동해야합니다. Firebase 팀의 설명이 도움이 될 것입니다. –