C++를 사용하여 계정에 여러 인증 제공업체 연결

인증 제공업체의 사용자 인증 정보를 기존 사용자 계정에 연결하면 사용자가 여러 인증 제공업체를 통해 앱에 로그인할 수 있습니다. 사용자가 로그인할 때 어떤 인증 제공업체를 사용하든 동일한 Firebase 사용자 ID로 본인 확인이 가능합니다. 예를 들어 비밀번호로 로그인한 사용자가 Google 계정을 연결하면 나중에 비밀번호와 Google 계정 중 어느 방법으로든 로그인할 수 있습니다. 또는 익명 사용자가 Facebook 계정을 연결하면 나중에 Facebook으로 로그인해서 앱을 계속 사용할 수 있습니다.

시작하기 전에

앱에 2개 이상의 인증 제공업체(익명 인증 포함)에 대한 지원을 추가하세요.

다음과 같이 기존 사용자 계정에 인증 제공업체의 사용자 인증 정보를 연결합니다.

  1. 인증 제공업체 또는 인증 방법을 사용해 사용자를 로그인 처리합니다.
  2. 새로운 인증 제공업체의 로그인 과정을 진행하되 firebase::auth::Auth::SignInWithCredential 메서드 호출 전까지만 진행합니다. 예를 들면 사용자의 Google ID 토큰, Facebook 액세스 토큰, 또는 이메일 주소와 비밀번호를 가져옵니다.
  3. 다음과 같이 새로운 인증 제공업체의 firebase::auth::Credential을 가져옵니다.

    Google 로그인
    firebase::auth::Credential credential =
        firebase::auth::GoogleAuthProvider::GetCredential(google_id_token,
                                                          nullptr);
    
    Facebook 로그인
    firebase::auth::Credential credential =
        firebase::auth::FacebookAuthProvider::GetCredential(access_token);
    
    이메일-비밀번호 로그인
    firebase::auth::Credential credential =
        firebase::auth::EmailAuthProvider::GetCredential(email, password);
    
  4. 다음과 같이 firebase::auth::Credential 객체를 로그인한 사용자의 LinkWithCredential 메서드에 전달합니다.

    // Link the new credential to the currently active user.
    firebase::auth::User current_user = auth->current_user();
    firebase::Future<firebase::auth::AuthResult> result =
        current_user.LinkWithCredential(credential);
    

    사용자 인증 정보가 다른 사용자 계정에 이미 연결되어 있다면 LinkWithCredential 호출이 실패합니다. 이 경우에는 앱에 맞게 계정과 관련 데이터를 병합하는 과정을 처리해야 합니다.

    // Gather data for the currently signed in User.
    firebase::auth::User current_user = auth->current_user();
    std::string current_email = current_user.email();
    std::string current_provider_id = current_user.provider_id();
    std::string current_display_name = current_user.display_name();
    std::string current_photo_url = current_user.photo_url();
    
    // Sign in with the new credentials.
    firebase::Future<firebase::auth::AuthResult> result =
        auth->SignInAndRetrieveDataWithCredential(credential);
    
    // To keep example simple, wait on the current thread until call completes.
    while (result.status() == firebase::kFutureStatusPending) {
      Wait(100);
    }
    
    // The new User is now active.
    if (result.error() == firebase::auth::kAuthErrorNone) {
      firebase::auth::User* new_user = *result.result();
    
      // Merge new_user with the user in details.
      // ...
      (void)new_user;
    }
    

LinkWithCredential 호출이 성공하면 사용자가 연결된 인증 제공업체를 사용해 동일한 Firebase 데이터에 액세스할 수 있습니다.

특정 인증 제공업체와 계정의 연결을 해제할 수 있습니다. 그러면 사용자가 더 이상 해당 제공업체로 로그인할 수 없게 됩니다.

인증 제공업체와 사용자 계정의 연결을 해제하려면 제공업체 ID를 Unlink 메서드에 전달합니다. 사용자 계정에 연결된 인증 제공업체의 ID를 가져오려면 ProviderData를 호출합니다.

// Unlink the sign-in provider from the currently active user.
firebase::auth::User current_user = auth->current_user();
firebase::Future<firebase::auth::AuthResult> result =
    current_user.Unlink(providerId);