【发布时间】:2019-12-25 16:21:39
【问题描述】:
我正在尝试使用 firebase 统一实现身份验证状态更改。我不知道我是否做得对,因为医生对我来说有点不清楚。来自你们的一个很好的代码示例会受到我的极大尊重。这是文档的链接。 https://firebase.google.com/docs/auth/unity/manage-users?authuser=0
文档中的标题是(获取当前登录的用户)。我真的需要帮助伙计们。我不明白如何正确使用它。我试图从 firebase 开发人员那里获得帮助,但没有得到回应。我你们想要一个详细的细节,我们可以在不和谐的地方聊天。让我知道。 :)
DatabaseReference reference;
public static Login instance;
FirebaseAuth auth;
FirebaseUser Player;
bool isServiceAvailable;
bool isAuthenticated;
public Text TEXTName;
public Text TEXTID;
public Text Data;
public Text State;
private User user;
private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
instance = this;
DontDestroyOnLoad(gameObject);
}
}
// Start is called before the first frame update
void Start()
{
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(" I hide the url");
reference = FirebaseDatabase.DefaultInstance.RootReference;
State.text = "Checking state";
InitializeFirebase();
LogInUser();
}
// Update is called once per frame
void Update()
{
}
// Handle initialization of the necessary firebase modules:
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)
{
if (auth.CurrentUser != Player)
{
bool signedIn = Player != auth.CurrentUser && auth.CurrentUser != null;
if (!signedIn && user != null)
{
Debug.Log("Signed out " + Player.UserId);
State.text = "Sign out";
}
Player = auth.CurrentUser;
if (signedIn)
{
Debug.Log("Signed in " + Player.UserId);
State.text = "Sign in";
}
}
}
void OnDestroy()
{
auth.StateChanged -= AuthStateChanged;
auth = null;
}
public void SignIn()
{
FirebaseAuth auth = FirebaseAuth.DefaultInstance;
auth.SignInWithEmailAndPasswordAsync(TEXTID.text, TEXTName.text).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
return;
}
FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
}
【问题讨论】:
标签: c# firebase unity3d firebase-authentication