コード例 #1
0
        // First try to login
        private static void Login(Action callback)
        {
            DatabaseConfig configFile = Fetch();

            if (string.IsNullOrEmpty(configFile.Email) || string.IsNullOrEmpty(configFile.Password))
            {
                Debug.Log("Database configuration are not configured!");
                return;
            }

            Debug.Log("Login in to retrieve token");

            var form = new Dictionary <string, string>
            {
                { "email", configFile.Email },
                { "password", configFile.Password }
            };

            UnityWebRequest wr = UnityWebRequest.Post($"{configFile.DatabaseURL}authenticate", form);

            wr.timeout = 60;
            wr.SetRequestHeader("User-Agent", "X-Unity3D-Agent");
            wr.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            wr.SendWebRequest().completed += operation =>
            {
                if (wr.result == UnityWebRequest.Result.ConnectionError || wr.responseCode == 401 || wr.responseCode == 500)
                {
                    throw new ArgumentException("Error: ", wr.error);
                }

                // Handle result
                string str = wr.downloadHandler.text;

                try
                {
                    DATABASE_TOKEN = JsonUtility.FromJson <DatabaseToken>(str);
                    // add the 3600 seconds for the next iteration
                    var exp = DateTime.Now;
                    exp = exp.AddSeconds(DATABASE_TOKEN.expires_in);
                    DATABASE_TOKEN.expire_time += exp.Ticks;
                    File.WriteAllText(FirebaseAppFile, JsonUtility.ToJson(DATABASE_TOKEN));
                    Debug.Log(FirebaseAppFile);
                }
                catch (ArgumentNullException e)
                {
                    throw new ArgumentException(e.Message);
                }

                callback();
            };
        }
コード例 #2
0
        private static void Refresh(Action callback)
        {
            DatabaseConfig configFile = Fetch();

            if (string.IsNullOrEmpty(configFile.Email) || string.IsNullOrEmpty(configFile.Password))
            {
                Debug.Log("Database configuration are not configured!");
                return;
            }

            UnityWebRequest wr = UnityWebRequest.Get($"{configFile.DatabaseURL}refresh");

            wr.timeout = 60;
            wr.SetRequestHeader("User-Agent", "X-Unity3D-Agent");
            wr.SetRequestHeader("Content-Type", "application/json; charset=utf-8");
            wr.SetRequestHeader("Authorization", "Bearer " + DATABASE_TOKEN.refresh_token);
            wr.SendWebRequest().completed += operation =>
            {
                if (wr.result == UnityWebRequest.Result.ConnectionError || wr.responseCode == 401 || wr.responseCode == 500)
                {
                    Debug.Log("Error: " + wr.error);
                    return;
                }

                // Handle result
                string str = wr.downloadHandler.text;
                try
                {
                    DATABASE_TOKEN = JsonUtility.FromJson <DatabaseToken>(str);
                    // add the 3600 seconds for the next iteration
                    var exp = DateTime.Now;
                    exp = exp.AddSeconds(DATABASE_TOKEN.expires_in);
                    DATABASE_TOKEN.expire_time += exp.Ticks;
                    File.WriteAllText(FirebaseAppFile, JsonUtility.ToJson(DATABASE_TOKEN));
                }
                catch (ArgumentNullException e)
                {
                    Debug.Log(e.Message);
                }

                callback();
            };
        }
コード例 #3
0
        /// <summary>
        /// Check if the user is logged in
        /// </summary>
        private static void RetrieveAppFile()
        {
            if (!File.Exists(FirebaseAppFile))
            {
                Debug.Log("Creating Firebase-storytime-app.json");
                return;
            }

            // TODO read file from disk to get the token.
            //Read the text from directly from the test.txt file
            StreamReader reader     = new StreamReader(FirebaseAppFile);
            string       jsonString = reader.ReadToEnd();

            reader.Close();
            try
            {
                DATABASE_TOKEN = JsonUtility.FromJson <DatabaseToken>(jsonString);
            }
            catch (ArgumentNullException e)
            {
                Debug.Log(e.Message);
            }
        }