Bạn có thể cho phép người dùng xác thực bằng Firebase thông qua tài khoản Facebook của họ bằng cách tích hợp tính năng Đăng nhập bằng Facebook vào ứng dụng.
Trước khi bắt đầu
- Thêm Firebase vào dự án C++.
- Trên trang web Facebook for Developers, hãy lấy App ID và App Secret cho ứng dụng của bạn.
- Bật tính năng Đăng nhập bằng Facebook:
- Trong bảng điều khiển Firebase, hãy mở mục Xác thực.
- Trên thẻ Phương thức đăng nhập, hãy bật phương thức đăng nhập Facebook và chỉ định Mã ứng dụng và Khoá bí mật của ứng dụng mà bạn nhận được từ Facebook.
- Sau đó, hãy đảm bảo rằng URI chuyển hướng OAuth (ví dụ:
my-app-12345.firebaseapp.com/__/auth/handler
) được liệt kê là một trong các URI chuyển hướng OAuth trong trang cài đặt ứng dụng Facebook của bạn trên trang web Facebook for Developers trong cấu hình Product Settings > Facebook Login (Cài đặt sản phẩm > Đăng nhập bằng Facebook).
Truy cập vào lớp firebase::auth::Auth
Lớp Auth
là cổng cho tất cả các lệnh gọi API.
- Thêm tệp tiêu đề Auth và App:
#include "firebase/app.h" #include "firebase/auth.h"
- Trong mã khởi tạo, hãy tạo một lớp
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__)
- Lấy lớp
firebase::auth::Auth
chofirebase::App
của bạn. Có mối liên kết một với một giữaApp
vàAuth
.firebase::auth::Auth* auth = firebase::auth::Auth::GetAuth(app);
Xác thực bằng Firebase
- Làm theo hướng dẫn dành cho Android và iOS+ để nhận mã truy cập cho người dùng Facebook đã đăng nhập.
- Sau khi người dùng đăng nhập thành công, hãy trao đổi mã truy cập để lấy thông tin đăng nhập Firebase và xác thực bằng Firebase bằng thông tin đăng nhập Firebase:
firebase::auth::Credential credential = firebase::auth::FacebookAuthProvider::GetCredential(access_token); firebase::Future<firebase::auth::AuthResult> result = auth->SignInAndRetrieveDataWithCredential(credential);
- Nếu chương trình của bạn có một vòng lặp cập nhật chạy thường xuyên (chẳng hạn như 30 hoặc 60 lần mỗi giây), bạn có thể kiểm tra kết quả một lần cho mỗi lần cập nhật bằng
Auth::SignInAndRetrieveDataWithCredentialLastResult
: Hoặc nếu chương trình của bạn dựa trên sự kiện, bạn có thể muốn đăng ký một lệnh gọi lại trên Future.firebase::Future<firebase::auth::AuthResult> result = auth->SignInAndRetrieveDataWithCredentialLastResult(); if (result.status() == firebase::kFutureStatusComplete) { if (result.error() == firebase::auth::kAuthErrorNone) { firebase::auth::AuthResult auth_result = *result.result(); printf("Sign in succeeded for `%s`\n", auth_result.user.display_name().c_str()); } else { printf("Sign in failed with error '%s'\n", result.error_message()); } }
Đăng ký lệnh gọi lại trên Future
Một số chương trình có các hàmUpdate
được gọi 30 hoặc 60 lần mỗi giây.
Ví dụ: nhiều trò chơi tuân theo mô hình này. Các chương trình này có thể gọi các hàm LastResult
để thăm dò các lệnh gọi không đồng bộ.
Tuy nhiên, nếu chương trình của bạn dựa trên sự kiện, bạn có thể muốn đăng ký các hàm gọi lại.
Hàm callback được gọi khi Future hoàn tất.
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::AuthResult> result = auth->CreateUserWithEmailAndPasswordLastResult(); // `&my_program_context` is passed verbatim to OnCreateCallback(). result.OnCompletion(OnCreateCallback, &my_program_context); }
void CreateUserUsingLambda(firebase::auth::Auth* auth) { // Callbacks work the same for any firebase::Future. firebase::Future<firebase::auth::AuthResult> 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); }
Các bước tiếp theo
Sau khi người dùng đăng nhập lần đầu tiên, một tài khoản người dùng mới sẽ được tạo và liên kết với thông tin đăng nhập (tức là tên người dùng và mật khẩu, số điện thoại hoặc thông tin nhà cung cấp dịch vụ uỷ quyền) mà người dùng đã đăng nhập. Tài khoản mới này được lưu trữ trong dự án Firebase của bạn và có thể dùng để xác định một người dùng trên mọi ứng dụng trong dự án, bất kể người dùng đăng nhập bằng cách nào.
-
Trong các ứng dụng của mình, bạn có thể lấy thông tin hồ sơ cơ bản của người dùng từ đối tượng
firebase::auth::User
:firebase::auth::User user = auth->current_user(); if (user.is_valid()) { 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(); }
Trong Firebase Realtime Database và Cloud Storage Quy tắc bảo mật, bạn có thể lấy mã nhận dạng người dùng riêng biệt của người dùng đã đăng nhập từ biến
auth
và dùng mã nhận dạng đó để kiểm soát dữ liệu mà người dùng có thể truy cập.
Bạn có thể cho phép người dùng đăng nhập vào ứng dụng của bạn bằng nhiều trình cung cấp dịch vụ xác thực bằng cách liên kết thông tin đăng nhập của trình cung cấp dịch vụ xác thực với một tài khoản người dùng hiện có.
Để đăng xuất người dùng, hãy gọi
SignOut()
:
auth->SignOut();