Google Sign-In'i uygulamanıza entegre ederek, kullanıcılarınızın Google Hesaplarını kullanarak Firebase ile kimlik doğrulaması yapmasına izin verebilirsiniz.
Sen başlamadan önce
- Firebase'i C++ projenize ekleyin .
- Google'ı Firebase konsolunda oturum açma yöntemi olarak etkinleştirin:
- Firebase konsolunda , Yetkilendirme bölümünü açın.
- Oturum açma yöntemi sekmesinde, Google oturum açma yöntemini etkinleştirin ve Kaydet 'i tıklayın.
firebase::auth::Auth
sınıfına erişin
Auth
sınıfı, tüm API çağrıları için ağ geçididir.- Yetkilendirme ve Uygulama başlık dosyalarını ekleyin:
#include "firebase/app.h" #include "firebase/auth.h"
- Başlatma kodunuzda bir
firebase::App
sınıfı oluşturun.#if defined(__ANDROID__) firebase::App* app = firebase::App::Create(firebase::AppOptions(), my_jni_env, my_activity); #else firebase::App* app = firebase::App::Create(firebase::AppOptions()); #endif // defined(__ANDROID__)
-
firebase::App
içinfirebase::auth::Auth
sınıfını edinin.App
veAuth
arasında bire bir eşleme var.firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
Firebase ile kimlik doğrulaması yapın
- Google'da oturum açmak için bir kimlik belirteci almak üzere Android ve iOS+ talimatlarını izleyin.
- Bir kullanıcı başarıyla oturum açtıktan sonra, kimlik belirtecini bir Firebase kimlik bilgisi ile değiştirin ve Firebase kimlik bilgisini kullanarak Firebase ile kimlik doğrulaması yapın:
firebase::auth::Credential credential = firebase::auth::GoogleAuthProvider::GetCredential(google_id_token, nullptr); firebase::Future<firebase::auth::User*> result = auth->SignInWithCredential(credential);
- Programınızın düzenli olarak çalışan bir güncelleme döngüsü varsa (saniyede 30 veya 60 kez diyelim), sonuçları her güncellemede bir kez
Auth::SignInWithCredentialLastResult
:firebase::Future<firebase::auth::User*> result = auth->SignInWithCredentialLastResult(); if (result.status() == firebase::kFutureStatusComplete) { if (result.error() == firebase::auth::kAuthErrorNone) { firebase::auth::User* user = *result.result(); printf("Sign in succeeded for `%s`\n", user->display_name().c_str()); } else { printf("Sign in failed with error '%s'\n", result.error_message()); } }
ile kontrol edebilirsiniz Veya programınız olay odaklıysa, tercih edebilirsiniz. Gelecek hakkında bir geri arama kaydetmek için .
Bir Gelecek için bir geri arama kaydedin
Bazı programlar, saniyede 30 veya 60 kez çağrılanUpdate
işlevlerine sahiptir. Örneğin birçok oyun bu modeli takip ediyor. Bu programlar, asenkron çağrıları yoklamak için LastResult
işlevlerini çağırabilir. Ancak, programınız olay odaklıysa, geri arama işlevlerini kaydetmeyi tercih edebilirsiniz. Geleceğin tamamlanması üzerine bir geri arama işlevi çağrılır.void OnCreateCallback(const firebase::Future<firebase::auth::User*>& result, void* user_data) { // The callback is called when the Future enters the `complete` state. assert(result.status() == firebase::kFutureStatusComplete); // Use `user_data` to pass-in program context, if you like. MyProgramContext* program_context = static_cast<MyProgramContext*>(user_data); // Important to handle both success and failure situations. if (result.error() == firebase::auth::kAuthErrorNone) { firebase::auth::User* user = *result.result(); printf("Create user succeeded for email %s\n", user->email().c_str()); // Perform other actions on User, if you like. firebase::auth::User::UserProfile profile; profile.display_name = program_context->display_name; user->UpdateUserProfile(profile); } else { printf("Created user failed with error '%s'\n", result.error_message()); } } void CreateUser(firebase::auth::Auth* auth) { // Callbacks work the same for any firebase::Future. firebase::Future<firebase::auth::User*> result = auth->CreateUserWithEmailAndPasswordLastResult(); // `&my_program_context` is passed verbatim to OnCreateCallback(). result.OnCompletion(OnCreateCallback, &my_program_context); }İsterseniz geri arama işlevi bir lambda olabilir.
void CreateUserUsingLambda(firebase::auth::Auth* auth) { // Callbacks work the same for any firebase::Future. firebase::Future<firebase::auth::User*> result = auth->CreateUserWithEmailAndPasswordLastResult(); // The lambda has the same signature as the callback function. result.OnCompletion( [](const firebase::Future<firebase::auth::User*>& result, void* user_data) { // `user_data` is the same as &my_program_context, below. // Note that we can't capture this value in the [] because std::function // is not supported by our minimum compiler spec (which is pre C++11). MyProgramContext* program_context = static_cast<MyProgramContext*>(user_data); // Process create user result... (void)program_context; }, &my_program_context); }
Sonraki adımlar
Bir kullanıcı ilk kez oturum açtıktan sonra, yeni bir kullanıcı hesabı oluşturulur ve oturum açtığı kullanıcı adı ve parolası, telefon numarası veya yetkilendirme sağlayıcı bilgileri gibi kimlik bilgileriyle ilişkilendirilir. Bu yeni hesap, Firebase projenizin bir parçası olarak depolanır ve kullanıcının nasıl oturum açtığından bağımsız olarak projenizdeki her uygulamada bir kullanıcıyı tanımlamak için kullanılabilir.
Uygulamalarınızda, kullanıcının temel profil bilgilerini
firebase::auth::User
nesnesinden alabilirsiniz:firebase::auth::User* user = auth->current_user(); if (user != nullptr) { std::string name = user->display_name(); std::string email = user->email(); std::string photo_url = user->photo_url(); // The user's ID, unique to the Firebase project. // Do NOT use this value to authenticate with your backend server, // if you have one. Use firebase::auth::User::Token() instead. std::string uid = user->uid(); }
Firebase Gerçek Zamanlı Veritabanınız ve Bulut Depolama Güvenlik Kurallarınızda , oturum açmış kullanıcının benzersiz kullanıcı kimliğini
auth
değişkeninden alabilir ve bunu bir kullanıcının hangi verilere erişebileceğini kontrol etmek için kullanabilirsiniz.
Bir kullanıcının oturumunu kapatmak için SignOut()
'u arayın:
auth->SignOut();