Jeeteshb

GoogleLogin

Jul 12th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 KB | None | 0 0
  1. public class GoogleCloudLogin : MonoBehaviour
  2. {
  3. public string webClientId = "<your client id here>";
  4.  
  5. private GoogleSignInConfiguration configuration;
  6. private string profilePicUrl;
  7. #region Google functions
  8. // Defer the configuration creation until Awake so the web Client ID
  9. // Can be set via the property inspector in the Editor.
  10. void Awake() {
  11. configuration = new GoogleSignInConfiguration {
  12. WebClientId = webClientId,
  13. RequestIdToken = true,
  14. RequestEmail = true,
  15. RequestProfile = true
  16. };
  17. }
  18.  
  19. public void OnSignIn() {
  20.  
  21. if (configuration == null)
  22. {
  23. configuration = new GoogleSignInConfiguration
  24. {
  25. WebClientId = webClientId,
  26. RequestIdToken = true,
  27. RequestEmail = true,
  28. RequestProfile = true
  29. };
  30. }
  31.  
  32. GoogleSignIn.Configuration = configuration;
  33.  
  34. GoogleSignIn.Configuration.UseGameSignIn = false;
  35. GoogleSignIn.Configuration.RequestIdToken = true;
  36. GoogleSignIn.Configuration.RequestEmail = true;
  37. GoogleSignIn.Configuration.RequestProfile = true;
  38.  
  39. AddStatusText("Calling SignIn");
  40. GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
  41. OnAuthenticationFinished);
  42. }
  43.  
  44. public void OnSignOut() {
  45. AddStatusText("Calling SignOut");
  46. GoogleSignIn.DefaultInstance.Disconnect();
  47. if(GoogleSignIn.Configuration.RequestIdToken == true)
  48. GoogleSignIn.DefaultInstance.SignOut();
  49. }
  50.  
  51. public void OnDisconnect() {
  52. AddStatusText("Calling Disconnect");
  53. GoogleSignIn.DefaultInstance.Disconnect();
  54. AppManager.Instnace.messageBoxManager.HidePreloader ();
  55. }
  56.  
  57. internal void OnAuthenticationFinished(Task<GoogleSignInUser> task) {
  58. if (task.IsFaulted) {
  59. using (IEnumerator<System.Exception> enumerator =
  60. task.Exception.InnerExceptions.GetEnumerator()) {
  61. if (enumerator.MoveNext()) {
  62. GoogleSignIn.SignInException error =
  63. (GoogleSignIn.SignInException)enumerator.Current;
  64. AddStatusText("Got Error: " + error.Status + " " + error.Message);
  65. } else {
  66. AddStatusText("Got Unexpected Exception?!?" + task.Exception);
  67. AppManager.Instnace.messageBoxManager.HidePreloader ();
  68. }
  69. }
  70. } else if(task.IsCanceled) {
  71. AddStatusText("Canceled");
  72. AppManager.Instnace.messageBoxManager.HidePreloader ();
  73. } else {
  74. AddStatusText("Welcome: " + task.Result.DisplayName + "!");
  75. AppManager.Instnace.messageBoxManager.ShowPreloaderDefaultWithBackGround ();
  76. LoginProcess (task);
  77. }
  78. }
  79.  
  80. public void OnSignInSilently() {
  81. GoogleSignIn.Configuration = configuration;
  82. GoogleSignIn.Configuration.UseGameSignIn = false;
  83. GoogleSignIn.Configuration.RequestIdToken = true;
  84. AddStatusText("Calling SignIn Silently");
  85.  
  86. GoogleSignIn.DefaultInstance.SignInSilently()
  87. .ContinueWith(OnAuthenticationFinished);
  88.  
  89. }
  90.  
  91.  
  92. public void OnGamesSignIn() {
  93. GoogleSignIn.Configuration = configuration;
  94. GoogleSignIn.Configuration.UseGameSignIn = true;
  95. GoogleSignIn.Configuration.RequestIdToken = false;
  96.  
  97. AddStatusText("Calling Games SignIn");
  98.  
  99. GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
  100. OnAuthenticationFinished);
  101. }
  102.  
  103. private List<string> messages = new List<string>();
  104. void AddStatusText(string text) {
  105. if (messages.Count == 5) {
  106. messages.RemoveAt(0);
  107. }
  108. messages.Add(text);
  109. string txt = "";
  110. foreach (string s in messages) {
  111. txt += "\n" + s;
  112. }
  113. // statusText.text = txt;
  114. }
  115. #endregion
  116.  
  117.  
  118. void LoginProcess(Task<GoogleSignInUser> task)
  119. {
  120. WebService.Instnace.appUser.Address1 = "";
  121. WebService.Instnace.appUser.Address2 = "";
  122. WebService.Instnace.appUser.Country = "";
  123. WebService.Instnace.appUser.DateOfBirth = "";
  124. WebService.Instnace.appUser.Email = task.Result.Email;
  125. WebService.Instnace.appUser.Gender = "";
  126. WebService.Instnace.appUser.FirstName = task.Result.GivenName;
  127. WebService.Instnace.appUser.LastName = task.Result.FamilyName;
  128. WebService.Instnace.appUser.GoogleId = task.Result.UserId;
  129. WebService.Instnace.appUser.Mobile = "";
  130. WebService.Instnace.appUser.Password = "";
  131. WebService.Instnace.appUser.PostCode = "";
  132. WebService.Instnace.appUser.RegionId = AppManager.Instnace.regionId;
  133. WebService.Instnace.appUser.RelationshipStatus = "";
  134. WebService.Instnace.appUser.State = "";
  135. WebService.Instnace.appUser.Suburb = "";
  136. WebService.Instnace.appUser.LoginInfo = "Google";
  137. WebService.Instnace.appUser.isLoggedIn = 1;
  138.  
  139. profilePicUrl = task.Result.ImageUrl.AbsoluteUri;
  140. AppManager.Instnace.isSocialSignInScreen = true;
  141. AppManager.Instnace.eSocial = eSocialSignUp.Social;
  142. string jsonHeader = "Google " + Convert.ToBase64String (Encoding.UTF8.GetBytes (WebService.Instnace.appUser.Email));
  143. WebService.Instnace.headerString = jsonHeader;
  144. string jsonParameter = JsonUtility.ToJson (WebService.Instnace.appUser);
  145. WebService.Instnace.headerString = jsonHeader;
  146. byte[] bodyRaw = Encoding.UTF8.GetBytes (jsonParameter);
  147.  
  148. ACPUnityPlugin.Instnace.trackSocial("Google", "GoogleLogin", "GoogleLoginPressed");
  149. //****************************************************************************
  150. // VerifyUsers
  151. //****************************************************************************
  152. WebService.Instnace.Post (AppManager.Instnace.baseURL + "/cloud/ViewaUserActions.aspx/VerifyUsers", bodyRaw, jsonHeader, UserCheckCallback);
  153. }
  154.  
  155. void UserCheckCallback(UnityWebRequest response ){
  156.  
  157. var res = JSON.Parse (response.downloadHandler.text);
  158. //user already exist code = 500
  159. if (response.responseCode == 200) {
  160.  
  161. if (res != null) {
  162. RootObject responseModel = JsonUtility.FromJson<RootObject> (response.downloadHandler.text);
  163. SaveUser (responseModel);
  164. } else {
  165. //Jeetesh - Hack - User already exist but we didn't get user data from server.
  166. Debug.Log("User present but details didn't come from server");
  167. AppManager.Instnace.messageBoxManager.HidePreloader ();
  168. }
  169. } else if(response.responseCode == 402) {
  170.  
  171. UserNotRegistered ();
  172. }
  173. }
  174. void SaveUser(RootObject responseModel) {
  175.  
  176. D _d = responseModel.d;
  177. OTPL.modal.User _user = _d.User;
  178.  
  179. AppManager.Instnace.userId = _user.Id;
  180. AppManager.Instnace.regionId = _user.RegionId;
  181. AppManager.Instnace.userEmail = _user.Email;
  182.  
  183. //Save Profile Picture
  184. StartCoroutine(AppManager.Instnace.SavePictureWithUrlAsPNG(profilePicUrl));
  185. ACPUnityPlugin.Instnace.trackSocial("Google", "GoogleLogin", "GoogleLoginUserLoggedIn");
  186.  
  187. UserDataService userDbService = new UserDataService ();
  188. //if the user id exist then update user otherwise
  189. if (userDbService.UserExist (_user.Email) > 0) {
  190. userDbService.CreateOrReplaceUser (_user.Id, _user.Address1, _user.Address2, _user.Country, _user.DateOfBirth, _user.Email, _user.FirstName, _user.LastName, _user.Gender, _user.Mobile, _user.PostCode,
  191. _user.RelationshipStatus, _user.State, _user.Suburb, _user.FacebookId, _user.GoogleId, _user.LinkedinId, _user.Password, _user.PasswordSalt, _user.RegionId, WebService.Instnace.appUser.LoginInfo, WebService.Instnace.appUser.isLoggedIn, _user.DateCreated, _user.interest);
  192. } else {
  193. userDbService.CreateOrReplaceUser (_user.Id, _user.Address1, _user.Address2, _user.Country, _user.DateOfBirth, _user.Email, _user.FirstName, _user.LastName, _user.Gender, _user.Mobile, _user.PostCode,
  194. _user.RelationshipStatus, _user.State, _user.Suburb, _user.FacebookId, _user.GoogleId, _user.LinkedinId, _user.Password, _user.PasswordSalt, _user.RegionId, WebService.Instnace.appUser.LoginInfo, WebService.Instnace.appUser.isLoggedIn, _user.DateCreated, _user.interest);
  195. }
  196. AppManager.Instnace.GoToScanScreen ();
  197. }
  198.  
  199. void UserNotRegistered() {
  200. CanvasManager.Instnace.ReturnPanelManager(ePanelManager.MainMenuPanelManager).NavigateToPanel(ePanels.SignUp_Panel);
  201. }
  202.  
  203. }
Add Comment
Please, Sign In to add comment