如果要使用FirebaseAuth.unitypackage使用firebase登录,则需要先注册才能登录。 启动应用程序后,我需要立即登录。我该怎么办? 但是,它可以与UnityEditor一起很好地工作。如果我使用IOS构建并运行它,该如何解决? 代码如下所示。
private Firebase.Auth.FirebaseAuth auth;
private Firebase.Auth.FirebaseAuth otherAuth;
private Dictionary<string, Firebase.Auth.FirebaseUser> userByAuth =
new Dictionary<string, Firebase.Auth.FirebaseUser>();
private string email = "";
private string password = "";
private string displayName = "";
private string phoneNumber = "";
private string receivedCode = "";
// Whether to sign in / link or reauthentication *and* fetch user profile data.
private bool signInAndFetchProfile = false;
// Flag set when a token is being fetched. This is used to avoid printing the token
// in IdTokenChanged() when the user presses the get token button.
private bool fetchingToken = false;
// Enable / disable password input box.
// NOTE: In some versions of Unity the password input box does not work in
// iOS simulators.
public bool usePasswordInput = false;
// Set the phone authentication timeout to a minute.
private uint phoneAuthTimeoutMs = 60 * 1000;
// The verification id needed along with the sent code for phone authentication.
private string phoneAuthVerificationId;
// Options used to setup secondary authentication object.
private Firebase.AppOptions otherAuthOptions = new Firebase.AppOptions
{
ApiKey = "",
AppId = "",
ProjectId = ""
};
const int kMaxLogSize = 16382;
Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
// When the app starts, check to make sure that we have
// the required dependencies to use Firebase, and if not,
// add them if possible.
private void Start()
{
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available)
{
InitializeFirebase();
}
else
{
Debug.LogError(
"Could not resolve all Firebase dependencies: " + dependencyStatus);
}
});
}
// Handle initialization of the necessary firebase modules:
private void InitializeFirebase()
{
Debug.Log("Setting up Firebase Auth");
auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);
}
// Track state changes of the auth object.
void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
Firebase.Auth.FirebaseAuth senderAuth = sender as Firebase.Auth.FirebaseAuth;
Firebase.Auth.FirebaseUser user = null;
if (senderAuth != null) userByAuth.TryGetValue(senderAuth.App.Name, out user);
if (senderAuth == auth && senderAuth.CurrentUser != user)
{
bool signedIn = user != senderAuth.CurrentUser && senderAuth.CurrentUser != null;
if (!signedIn && user != null)
{
Debug.Log("Signed out " + user.UserId);
}
user = senderAuth.CurrentUser;
userByAuth[senderAuth.App.Name] = user;
if (signedIn)
{
Debug.Log("Signed in " + user.UserId);
displayName = user.DisplayName ?? "";
DisplayDetailedUserInfo(user, 1);
}
}
}
protected bool LogTaskCompletion(Task task, string operation)
{
//If complete == true, return true;
//bool complete = false;
if (task.IsCanceled)
{
CommonData.MessageAlert(operation + " canceled.");
Debug.Log(operation + " canceled.");
CommonData.bIsLoginError = true;
return false;
}
else if (task.IsFaulted)
{
CommonData.bIsLoginError = true;
Debug.Log(operation + " encounted an error.");
foreach (Exception exception in task.Exception.Flatten().InnerExceptions)
{
string authErrorCode = "";
Firebase.FirebaseException firebaseEx = exception as Firebase.FirebaseException;
if (firebaseEx != null)
{
authErrorCode = String.Format("AuthError.{0}: ",
((Firebase.Auth.AuthError)firebaseEx.ErrorCode).ToString());
if (((Firebase.Auth.AuthError)firebaseEx.ErrorCode).ToString() == "UserNotFound")
CommonData.MessageAlert("User Not Found!");
else if (((Firebase.Auth.AuthError)firebaseEx.ErrorCode).ToString() == "WrongPassword")
CommonData.MessageAlert("Wrong Password");
else
CommonData.MessageAlert(exception.ToString());
}
else
CommonData.MessageAlert(exception.ToString());
Debug.Log(authErrorCode + exception.ToString());
}
return false;
}
else if (task.IsCompleted)
{
Debug.Log(operation + " completed");
CommonData.MessageAlert("Login Completed!");
//complete = true;
CommonData.bIsLoginError = false;
return true;
}
return true;
// return complete;
}
// Create a user with the email and password.
public Task CreateUserWithEmailAsync()
{
Debug.Log(String.Format("Attempting to create user {0}...", CommonData.useremail));
// DisableUI();
// This passes the current displayName through to HandleCreateUserAsync
// so that it can be passed to UpdateUserProfile(). displayName will be
// reset by AuthStateChanged() when the new user is created and signed in.
string newDisplayName = displayName;
return auth.CreateUserWithEmailAndPasswordAsync(CommonData.useremail, CommonData.password)
.ContinueWith((task) =>
{
CommonData.IsUserAuthenticated = true;
if (LogTaskCompletion(task, "User Creation"))
{
var user = task.Result;
DisplayDetailedUserInfo(user, 1);
return UpdateUserProfileAsync(newDisplayName: newDisplayName);
}
return task;
}).Unwrap();
}
// Update the user's display name with the currently selected display name.
public Task UpdateUserProfileAsync(string newDisplayName = null)
{
if (auth.CurrentUser == null)
{
Debug.Log("Not signed in, unable to update user profile");
return Task.FromResult(0);
}
displayName = newDisplayName ?? displayName;
Debug.Log("Updating user profile");
// DisableUI();
return auth.CurrentUser.UpdateUserProfileAsync(new Firebase.Auth.UserProfile
{
DisplayName = displayName,
PhotoUrl = auth.CurrentUser.PhotoUrl,
}).ContinueWith(task =>
{
// EnableUI();
if (LogTaskCompletion(task, "User profile"))
{
DisplayDetailedUserInfo(auth.CurrentUser, 1);
}
});
}
// Sign-in with an email and password.
public Task SigninWithEmailAsync(string email, string password)
{
Debug.Log(String.Format("Attempting to sign in as {0}...", email));
if (signInAndFetchProfile)
{
return auth.SignInAndRetrieveDataWithCredentialAsync(
Firebase.Auth.EmailAuthProvider.GetCredential(email, password)).ContinueWith(
HandleSignInWithSignInResult);
}
else
{
return auth.SignInWithEmailAndPasswordAsync(email, password)
.ContinueWith(HandleSignInWithUser);
}
}
}
SigninWithEmailAsync(电子邮件字符串,密码字符串)将使用auth = null执行该功能。