场景变化时鉴权失败

我设置了我的 firebase 身份验证,它运行良好。但是,当我加载不同的场景并返回欢迎场景后,身份验证失败。场景切换时,如何重新授权或保持登录状态?


我的授权欢迎场景代码:


public void Start()

    {

        InitializeFirebase();

        InitializePlayGamesPlatform();

        SignInPlayGames();        

    }


public void InitializeFirebase()

    {

        Debug.Log("UserManager: Setting up Firebase Auth");

        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;

        auth.StateChanged += AuthStateChanged;

        auth.IdTokenChanged += IdTokenChanged;

        // Specify valid options to construct a secondary authentication object.

        if (otherAuthOptions != null &&

            !(String.IsNullOrEmpty(otherAuthOptions.ApiKey) ||

              String.IsNullOrEmpty(otherAuthOptions.AppId) ||

              String.IsNullOrEmpty(otherAuthOptions.ProjectId)))

        {

            try

            {

                otherAuth = Firebase.Auth.FirebaseAuth.GetAuth(Firebase.FirebaseApp.Create(

                  otherAuthOptions, "Secondary"));

                otherAuth.StateChanged += AuthStateChanged;

                otherAuth.IdTokenChanged += IdTokenChanged;

            }

            catch (Exception)

            {

                Debug.Log("UserManager: ERROR: Failed to initialize secondary authentication object.");

            }

        }

        AuthStateChanged(this, null);        

    }


public void InitializePlayGamesPlatform()

    {

        PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()

            .RequestServerAuthCode(false)

            .Build();


        PlayGamesPlatform.InitializeInstance(config);

        PlayGamesPlatform.Activate();


        auth = FirebaseAuth.DefaultInstance;

    }



撒科打诨
浏览 64回答 2
2回答

慕容森

我尝试了很多但我没有得到想要的行为(关于重新授权/应用程序不退出)即使我改变场景,凭据仍然存在。如果您想更改帐户,重新启动应用程序,然后在欢迎场景中注销(facebook 或 google)

浮云间

假设它第一次按预期工作,听起来DontDestroyOnLoad就是您要找的东西:切换场景时不会破坏该对象 -> 因此不会Start再次运行该方法。但是,您还需要将它与singleton模式结合起来,以确保在返回第一个场景时不会再次添加/运行它:public class AuthComponent : MonoBehaviour{    private static AuthComponent singleton;    private void Awake()    {        // Check if already another AuthComponent running        if(singleton)        {            Debug.Log("Already another AuthComponent running");            Destroy(gameObject);            return;        }        // Otherwise store a global reference of this AuthComponent        singleton = this;        // and make it DontDestroyOnLoad        DontDestroyOnLoad(gameObject);    }    ...}
打开App,查看更多内容
随时随地看视频慕课网APP