您可以通过将 Google Sign-In 集成到您的应用中,让您的用户使用他们的 Google 帐户向 Firebase 进行身份验证。
在你开始之前
- 将 Firebase 添加到您的 C++ 项目中。
- 在 Firebase 控制台中启用 Google 作为登录方法:
- 在Firebase 控制台中,打开Auth部分。
- 在登录方法选项卡上,启用Google登录方法并单击保存。
访问firebase::auth::Auth
类
Auth
类是所有 API 调用的网关。- 添加 Auth 和 App 头文件:
#include "firebase/app.h" #include "firebase/auth.h"
- 在您的初始化代码中,创建一个
firebase::App
类。#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
获取firebase::auth::Auth
类。App
和Auth
之间存在一对一的映射关系。firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
使用 Firebase 进行身份验证
- 按照Android和iOS+的说明获取用于 Google 登录的 ID 令牌。
- 用户成功登录后,将 ID 令牌交换为 Firebase 凭据,并使用 Firebase 凭据向 Firebase 进行身份验证:
firebase::auth::Credential credential = firebase::auth::GoogleAuthProvider::GetCredential(google_id_token, nullptr); firebase::Future<firebase::auth::User*> result = auth->SignInWithCredential(credential);
- 如果您的程序有一个定期运行的更新循环(例如每秒 30 或 60 次),您可以使用
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()); } }
或者,如果您的程序是事件驱动的,您可能更喜欢在 Future 上注册回调。
在 Future 上注册回调
某些程序具有每秒调用 30 或 60 次的Update
函数。例如,许多游戏都遵循这种模式。这些程序可以调用LastResult
函数来轮询异步调用。但是,如果您的程序是事件驱动的,您可能更喜欢注册回调函数。在 Future 完成时调用回调函数。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); }如果您愿意,回调函数也可以是 lambda。
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); }
下一步
用户首次登录后,会创建一个新用户帐户并将其链接到凭据(即用户名和密码、电话号码或身份验证提供商信息),即用户登录时使用的凭据。这个新帐户作为 Firebase 项目的一部分存储,可用于在项目中的每个应用中识别用户,无论用户如何登录。
在您的应用程序中,您可以从
firebase::auth::User
对象获取用户的基本个人资料信息: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 实时数据库和云存储安全规则中,您可以从
auth
变量中获取登录用户的唯一用户 ID,并使用它来控制用户可以访问哪些数据。
您可以通过将身份验证提供程序凭据链接到现有用户帐户来允许用户使用多个身份验证提供程序登录您的应用程序。
要注销用户,请调用SignOut()
:
auth->SignOut();