com.guru.unity.sdk.core/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.Auth.cs

55 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Firebase.Auth;
using Firebase.Extensions;
using UnityEngine;
namespace Guru
{
public static partial class FirebaseUtil
{
public static FirebaseUser CurrentUser => FirebaseAuth.DefaultInstance.CurrentUser;
private static readonly WaitForSeconds _wait = new WaitForSeconds(10);
public static void AuthUser()
{
//FirebaseAuth获取用户验证并同步用户数据
if (CurrentUser != null)
{
Log.I(LOG_TAG, $"[Auth] user existsUserId:{CurrentUser.UserId}");
OnFirebaseAuthResult?.Invoke(true);
return;
}
string authToken = IPMConfig.IPM_FIREBASE_TOKEN;
Log.I(LOG_TAG, $"[Auth] Firebase Token:{authToken}");
if (string.IsNullOrEmpty(authToken) || !NetworkUtil.IsNetAvaliable)
{
CoroutineHelper.Instance.StartDelayed(_wait, AuthUser);
OnFirebaseAuthResult?.Invoke(false);
return;
}
FirebaseAuth.DefaultInstance.SignInWithCustomTokenAsync(authToken)
.ContinueWithOnMainThread(task =>
{
// ----- Task failed -----
if (task.IsCanceled || task.IsFaulted)
{
Log.E(LOG_TAG,"[Auth] SignInWithCustomTokenAsync encountered an error: " + task.Exception);
OnFirebaseAuthResult?.Invoke(false);
CoroutineHelper.Instance.StartDelayed(_wait, AuthUser);
return;
}
// ----- User is NULL -----
if (CurrentUser == null)
{
OnFirebaseAuthResult?.Invoke(false);
CoroutineHelper.Instance.StartDelayed(_wait, AuthUser);
return;
}
// ----- Success -----
OnFirebaseAuthResult?.Invoke(true); // 最后判定是成功的
});
}
}
}