firebase::Future<firebase::auth::AuthResult>result=auth->CreateUserWithEmailAndPasswordLastResult();// The lambda has the same signature as the callback function.result.OnCompletion([](constfirebase::Future<firebase::auth::AuthResult>&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::Future<firebase::auth::AuthResult>result=auth->CreateUserWithEmailAndPasswordLastResult();if(result.status()==firebase::kFutureStatusComplete){if(result.error()==firebase::auth::kAuthErrorNone){firebase::auth::AuthResult*auth_result=*result.result();printf("Create user succeeded for email %s\n",auth_result.user.email().c_str());}else{printf("Created user failed with error '%s'\n",result.error_message());}}
classMyAuthStateListener:publicfirebase::auth::AuthStateListener{public:voidOnAuthStateChanged(firebase::auth::Auth*auth)override{firebase::auth::Useruser=auth.current_user();if(user.is_valid()){// User is signed inprintf("OnAuthStateChanged: signed_in %s\n",user.uid().c_str());conststd::stringdisplayName=user.DisplayName();conststd::stringemailAddress=user.Email();conststd::stringphotoUrl=user.PhotoUrl();}else{// User is signed outprintf("OnAuthStateChanged: signed_out\n");}// ...}};
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["You can use Firebase Authentication to allow users to sign in to your app using one\nor more sign-in methods, including email address and password sign-in, and\nfederated identity providers such as Google Sign-in and Facebook Login. This\ntutorial gets you started with Firebase Authentication by showing you how to add\nemail address and password sign-in to your app.\n\nConnect your C++ project to Firebase\n\nBefore you can use\n[Firebase Authentication](/docs/reference/unity/namespace/firebase/auth),\nyou need to:\n\n- Register your C++ project and configure it to use Firebase.\n\n If your C++ project already uses Firebase, then it's already registered and\n configured for Firebase.\n- Add the [Firebase C++ SDK](/download/cpp) to your C++ project.\n\n| **Find detailed instructions for these initial\n| setup tasks in\n| [Add Firebase to your C++\n| project](/docs/cpp/setup#note-select-platform).**\n\nNote that adding Firebase to your C++ project involves tasks both in the\n[Firebase console](//console.firebase.google.com/) and in your open C++ project (for example, you download\nFirebase config files from the console, then move them into your C++ project).\n\nSign up new users\n\nCreate a form that allows new users to register with your app using their\nemail address and a password. When a user completes the form, validate the\nemail address and password provided by the user, then pass them to the\n`CreateUserWithEmailAndPassword` method: \n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result =\n auth-\u003eCreateUserWithEmailAndPassword(email, password);\n\nYou can check the status of the account creation operation either by registering\na callback on the `CreateUserWithEmailAndPasswordLastResult` Future object, or,\nif you're writing a game or app with some kind of periodic update loop, by\npolling the status in the update loop.\n\nFor example, using a Future: \n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result =\n auth-\u003eCreateUserWithEmailAndPasswordLastResult();\n\n // The lambda has the same signature as the callback function.\n result.OnCompletion(\n [](const firebase::Future\u003cfirebase::auth::AuthResult\u003e& result,\n void* user_data) {\n // `user_data` is the same as &my_program_context, below.\n // Note that we can't capture this value in the [] because std::function\n // is not supported by our minimum compiler spec (which is pre C++11).\n MyProgramContext* program_context =\n static_cast\u003cMyProgramContext*\u003e(user_data);\n\n // Process create user result...\n (void)program_context;\n },\n &my_program_context);\n\nOr, to use polling, do something like the following example in your game's\nupdate loop: \n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result =\n auth-\u003eCreateUserWithEmailAndPasswordLastResult();\n if (result.status() == firebase::kFutureStatusComplete) {\n if (result.error() == firebase::auth::kAuthErrorNone) {\n firebase::auth::AuthResult* auth_result = *result.result();\n printf(\"Create user succeeded for email %s\\n\", auth_result.user.email().c_str());\n } else {\n printf(\"Created user failed with error '%s'\\n\", result.error_message());\n }\n }\n\nSign in existing users\n\nCreate a form that allows existing users to sign in using their email address\nand password. When a user completes the form, call the\n`SignInWithEmailAndPassword` method: \n\n firebase::Future\u003cfirebase::auth::AuthResult\u003e result =\n auth-\u003eSignInWithEmailAndPassword(email, password);\n\nGet the result of the sign-in operation the same way you got the sign-up result.\n\nSet an authentication state listener and get account data\n\nTo respond to sign-in and sign-out events, attach a listener to the global\nauthentication object. This listener gets called whenever the user's sign-in\nstate changes. Because the listener runs only after the authentication object is\nfully initialized and after any network calls have completed, it is the best\nplace to get information about the signed-in user.\n\nCreate the listener by implementing the `firebase::auth::AuthStateListener`\nabstract class. For example, to create a listener that gets information about\nthe user when a user successfully signs in: \n\n class MyAuthStateListener : public firebase::auth::AuthStateListener {\n public:\n void OnAuthStateChanged(firebase::auth::Auth* auth) override {\n firebase::auth::User user = auth.current_user();\n if (user.is_valid()) {\n // User is signed in\n printf(\"OnAuthStateChanged: signed_in %s\\n\", user.uid().c_str());\n const std::string displayName = user.DisplayName();\n const std::string emailAddress = user.Email();\n const std::string photoUrl = user.PhotoUrl();\n } else {\n // User is signed out\n printf(\"OnAuthStateChanged: signed_out\\n\");\n }\n // ...\n }\n };\n\nAttach the listener with the `firebase::auth::Auth` object's\n`AddAuthStateListener` method: \n\n MyAuthStateListener state_change_listener;\n auth-\u003eAddAuthStateListener(&state_change_listener);\n\nNext steps\n\nLearn how to add support for other identity providers and anonymous guest\naccounts:\n\n- [Google Sign-in](/docs/auth/cpp/google-signin)\n- [Facebook Login](/docs/auth/cpp/facebook-login)\n- [Apple Login](/docs/auth/cpp/apple)\n- [Twitter Login](/docs/auth/cpp/twitter-login)\n- [GitHub Login](/docs/auth/cpp/github-auth)\n- [Microsoft Login](/docs/auth/cpp/microsoft-oauth)\n- [Yahoo Login](/docs/auth/cpp/yahoo-oauth)\n- [Anonymous sign-in](/docs/auth/cpp/anonymous-auth)\n- [Phone Authentication](/docs/auth/cpp/phone-auth)"]]