123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- using UnityEngine;
- using UnityEngine.Networking;
- using System;
- using System.Collections;
- using System.Linq;
- using System.Collections.Generic;
- using System.Net;
- using System.Text;
- using System.ComponentModel;
- using System.Runtime.Serialization;
- using SimpleFirebaseUnity.MiniJSON;
- namespace SimpleFirebaseUnity
- {
- public enum FirebaseAuthenticationType
- {
- None,
- Email,
- Facebook,
- Google,
- Github,
- Twitter,
- Anonymous
- }
-
- [Serializable]
- public class FirebaseAuthentication
- {
- #if (UNITY_ANDROID || UNITY_EDITOR)
- public static string requestUri = "https://antm-mobile-game-full-edit-189.firebaseapp.com/__/auth/handler";
- #else
- public static string requestUri = "https://antm-5540150.firebaseapp.com/__/auth/handler";
- #endif
- public Action<FirebaseAuthentication, DataSnapshot> OnSignInWithPasswordSuccess;
- public Action<FirebaseAuthentication, FirebaseError> OnSignInWithPasswordFailed;
- public Action<FirebaseAuthentication, DataSnapshot> OnSignUpWithPasswordSuccess;
- public Action<FirebaseAuthentication, FirebaseError> OnSignUpWithPasswordFailed;
- public Action<FirebaseAuthentication, DataSnapshot> OnSignInOrUpWithCredentialSuccess;
- public Action<FirebaseAuthentication, FirebaseError> OnSignInOrUpWithCredentialFailed;
- public Action<FirebaseAuthentication, DataSnapshot> OnSignInOrUpAnonymousSuccess;
- public Action<FirebaseAuthentication, FirebaseError> OnSignInOrUpAnonymousFailed;
- public Action<FirebaseAuthentication, DataSnapshot> OnChangeDisplayNameSuccess;
- public Action<FirebaseAuthentication, FirebaseError> OnChangeDisplayNameFailed;
- public string apikey;
- public static FirebaseAuthenticationType currentAuthenticationType;
- private static string apikeyconfigs;
- public FirebaseAuthentication(string apikeyconfig)
- {
- apikeyconfigs = apikeyconfig;
- apikey = apikeyconfigs;
- }
-
- public static FirebaseAuthentication CreateNew(string apikeyconfig){
- apikeyconfigs = apikeyconfig;
- if (new FirebaseAuthentication(apikeyconfig) != null) {
- return new FirebaseAuthentication (apikeyconfig);
- } else {
- return null;
- }
- }
-
- public void SignInWithPassword(string email, string password,bool returnsecuretoken)
- {
- try
- {
- string json = "{\n email: \""+email+"\",\n password: \""+password+"\",\n returnSecureToken:"+ returnsecuretoken.ToString().ToLower()+"\n}";
- string url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + apikeyconfigs;
- Dictionary<string,string> headers = new Dictionary<string,string>();
- headers.Add("Content-Type","application/json");
- byte [] bytes = Encoding.ASCII.GetBytes(json);
- StartCoroutine(RequestCoroutine(url, bytes, headers, OnSignInWithPasswordSuccess, OnSignInWithPasswordFailed));
- }
- catch (WebException webEx)
- {
- if (OnSignInWithPasswordFailed != null) OnSignInWithPasswordFailed(this, FirebaseError.Create(webEx));
- }
- catch (Exception ex)
- {
- if (OnSignInWithPasswordFailed != null) OnSignInWithPasswordFailed(this, new FirebaseError(ex.Message));
- }
- }
- public void SignUpWithPassword(string email, string password,bool returnsecuretoken)
- {
- try
- {
- string json = "{\n email: \""+email+"\",\n password: \""+password+"\",\n returnSecureToken:"+ returnsecuretoken.ToString().ToLower()+"\n}";
- string url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=" + apikeyconfigs;
- Dictionary<string,string> headers = new Dictionary<string,string>();
- headers.Add("Content-Type","application/json");
- byte [] bytes = Encoding.ASCII.GetBytes(json);
- StartCoroutine(RequestCoroutine(url, bytes, headers, OnSignUpWithPasswordSuccess, OnSignUpWithPasswordFailed));
- }
- catch (WebException webEx)
- {
- if (OnSignUpWithPasswordFailed != null) OnSignUpWithPasswordFailed(this, FirebaseError.Create(webEx));
- }
- catch (Exception ex)
- {
- if (OnSignUpWithPasswordFailed != null) OnSignUpWithPasswordFailed(this, new FirebaseError(ex.Message));
- }
- }
- public void SignInOrUpAnonymous()
- {
- try
- {
- string url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=" + apikeyconfigs;
- Dictionary<string, string> headers = new Dictionary<string, string>();
- headers.Add("Content-Type", "application/json");
- byte[] bytes = Encoding.ASCII.GetBytes("{}");
- StartCoroutine(RequestCoroutine(url, bytes, headers, OnSignInOrUpAnonymousSuccess, OnSignInOrUpAnonymousFailed));
- }
- catch (WebException webEx)
- {
- if (OnSignInOrUpAnonymousFailed != null) OnSignInOrUpAnonymousFailed(this, FirebaseError.Create(webEx));
- }
- catch (Exception ex)
- {
- if (OnSignInOrUpAnonymousFailed != null) OnSignInOrUpAnonymousFailed(this, new FirebaseError(ex.Message));
- }
- }
- public void SignInOrUpWithCredential(string accestoken, FirebaseAuthenticationType authenticationtype, string requestUri, bool returnIdpCredential, bool returnSecureToken, string apikey = null)
- {
- try
- {
- string json = "null";
- string url = "null";
- currentAuthenticationType = authenticationtype;
- if (currentAuthenticationType == FirebaseAuthenticationType.Facebook)
- {
- json = "{\"postBody\": \"access_token=" + accestoken + "&providerId=facebook.com" + "\",\"requestUri\": \"" + requestUri + "\",\n \" returnIdpCredential\":" + returnIdpCredential.ToString().ToLower() + ",\n \"returnSecureToken\": " + returnSecureToken.ToString().ToLower() + "}";
- }
- if (currentAuthenticationType == FirebaseAuthenticationType.Google)
- {
- json = "{\"postBody\": \"id_token=" + accestoken + "&providerId=google.com" + "\",\"requestUri\": \"" + requestUri + "\",\n \" returnIdpCredential\":" + returnIdpCredential.ToString().ToLower() + ",\n \"returnSecureToken\": " + returnSecureToken.ToString().ToLower() + "}";
- Debug.Log(json);
- }
- if (apikey != null)
- {
- url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=" + apikey;
- Debug.Log("url with apikey: " + url);
- }
- else
- {
- url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=" + apikeyconfigs;
- Debug.Log("url with apikeyconfigs: " + url);
- }
- Dictionary<string,string> headers = new Dictionary<string,string>();
- headers.Add("Content-Type","application/json");
- byte [] bytes = Encoding.ASCII.GetBytes(json);
- StartCoroutine(RequestCoroutine(url, bytes, headers, OnSignInOrUpWithCredentialSuccess, OnSignInOrUpWithCredentialFailed));
- }
- catch (WebException webEx)
- {
- if (OnSignInOrUpWithCredentialFailed != null) OnSignInOrUpWithCredentialFailed(this, FirebaseError.Create(webEx));
- }
- catch (Exception ex)
- {
- if (OnSignInOrUpWithCredentialFailed != null) OnSignInOrUpWithCredentialFailed(this, new FirebaseError(ex.Message));
- }
- }
- public void ChangeDisplayName(string displayname)
- {
- try
- {
- string json = "null";
- string url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=" + apikeyconfigs;
- Dictionary<string, string> headers = new Dictionary<string, string>();
- headers.Add("Content-Type", "application/json");
- json = "{\"idToken\":\"" + "DataTools.DataUser.Instance.ProfileData.IdToken" + "\",\"displayName\":\"" + displayname + "\",\"photoUrl\":\"\",\"returnSecureToken\":true}";
- byte[] bytes = Encoding.ASCII.GetBytes(json);
- StartCoroutine(RequestCoroutine(url, bytes, headers, OnChangeDisplayNameSuccess, OnChangeDisplayNameFailed));
- }
- catch (WebException webEx)
- {
- if (OnChangeDisplayNameFailed != null) OnChangeDisplayNameFailed(this, FirebaseError.Create(webEx));
- }
- catch (Exception ex)
- {
- if (OnChangeDisplayNameFailed != null) OnChangeDisplayNameFailed(this, new FirebaseError(ex.Message));
- }
- }
- public void StartCoroutine(IEnumerator routine)
- {
- FirebaseManager.Instance.StartCoroutine (routine);
- }
-
- public void StopCoroutine(IEnumerator routine)
- {
- FirebaseManager.Instance.StopCoroutine (routine);
- }
- protected IEnumerator RequestCoroutine(string url, byte[] postData, Dictionary<string, string> headers, Action<FirebaseAuthentication, DataSnapshot> OnSuccess, Action<FirebaseAuthentication, FirebaseError> OnFailed)
- {
- using (WWW www = (headers != null) ? new WWW(url, postData, headers) : (postData != null) ? new WWW(url, postData) : new WWW(url))
- {
-
- // Wait until load done
- yield return www;
- if (!string.IsNullOrEmpty(www.error))
- {
- HttpStatusCode status = 0;
- string errMessage = "";
-
- // Parse status code
- if (www.responseHeaders.ContainsKey("STATUS"))
- {
- string str = www.responseHeaders["STATUS"] as string;
- string[] components = str.Split(' ');
- int code = 0;
- if (components.Length >= 3 && int.TryParse(components[1], out code))
- status = (HttpStatusCode)code;
- }
- if (www.error.Contains("crossdomain.xml") || www.error.Contains("Couldn't resolve"))
- {
- errMessage = "No internet connection or crossdomain.xml policy problem";
- }
- else {
- // Parse error message
- try
- {
- if (!string.IsNullOrEmpty(www.text))
- {
- Dictionary<string, object> obj = Json.Deserialize(www.text) as Dictionary<string, object>;
- if (obj != null && obj.ContainsKey("error"))
- errMessage = obj["error"] as string;
- }
- }
- catch
- {
- }
- }
- if (OnFailed != null)
- {
- if (string.IsNullOrEmpty(errMessage))
- errMessage = www.text;
- if (errMessage.Contains("Failed downloading"))
- {
- errMessage = "Request failed with no info of error.";
- }
- OnFailed(this, new FirebaseError(status, errMessage));
- }
- #if UNITY_EDITOR
- Debug.LogWarning(www.error + " (" + (int)status + ")\nResponse Message: " + errMessage);
- #endif
- }
- else
- {
- DataSnapshot snapshot = new DataSnapshot(www.text);
- if (OnSuccess != null) OnSuccess(this, snapshot);
- }
- }
- }
- }
- }
|