Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GoogleCloudLogin : MonoBehaviour
- {
- public string webClientId = "<your client id here>";
- private GoogleSignInConfiguration configuration;
- private string profilePicUrl;
- #region Google functions
- // Defer the configuration creation until Awake so the web Client ID
- // Can be set via the property inspector in the Editor.
- void Awake() {
- configuration = new GoogleSignInConfiguration {
- WebClientId = webClientId,
- RequestIdToken = true,
- RequestEmail = true,
- RequestProfile = true
- };
- }
- public void OnSignIn() {
- if (configuration == null)
- {
- configuration = new GoogleSignInConfiguration
- {
- WebClientId = webClientId,
- RequestIdToken = true,
- RequestEmail = true,
- RequestProfile = true
- };
- }
- GoogleSignIn.Configuration = configuration;
- GoogleSignIn.Configuration.UseGameSignIn = false;
- GoogleSignIn.Configuration.RequestIdToken = true;
- GoogleSignIn.Configuration.RequestEmail = true;
- GoogleSignIn.Configuration.RequestProfile = true;
- AddStatusText("Calling SignIn");
- GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
- OnAuthenticationFinished);
- }
- public void OnSignOut() {
- AddStatusText("Calling SignOut");
- GoogleSignIn.DefaultInstance.Disconnect();
- if(GoogleSignIn.Configuration.RequestIdToken == true)
- GoogleSignIn.DefaultInstance.SignOut();
- }
- public void OnDisconnect() {
- AddStatusText("Calling Disconnect");
- GoogleSignIn.DefaultInstance.Disconnect();
- AppManager.Instnace.messageBoxManager.HidePreloader ();
- }
- internal void OnAuthenticationFinished(Task<GoogleSignInUser> task) {
- if (task.IsFaulted) {
- using (IEnumerator<System.Exception> enumerator =
- task.Exception.InnerExceptions.GetEnumerator()) {
- if (enumerator.MoveNext()) {
- GoogleSignIn.SignInException error =
- (GoogleSignIn.SignInException)enumerator.Current;
- AddStatusText("Got Error: " + error.Status + " " + error.Message);
- } else {
- AddStatusText("Got Unexpected Exception?!?" + task.Exception);
- AppManager.Instnace.messageBoxManager.HidePreloader ();
- }
- }
- } else if(task.IsCanceled) {
- AddStatusText("Canceled");
- AppManager.Instnace.messageBoxManager.HidePreloader ();
- } else {
- AddStatusText("Welcome: " + task.Result.DisplayName + "!");
- AppManager.Instnace.messageBoxManager.ShowPreloaderDefaultWithBackGround ();
- LoginProcess (task);
- }
- }
- public void OnSignInSilently() {
- GoogleSignIn.Configuration = configuration;
- GoogleSignIn.Configuration.UseGameSignIn = false;
- GoogleSignIn.Configuration.RequestIdToken = true;
- AddStatusText("Calling SignIn Silently");
- GoogleSignIn.DefaultInstance.SignInSilently()
- .ContinueWith(OnAuthenticationFinished);
- }
- public void OnGamesSignIn() {
- GoogleSignIn.Configuration = configuration;
- GoogleSignIn.Configuration.UseGameSignIn = true;
- GoogleSignIn.Configuration.RequestIdToken = false;
- AddStatusText("Calling Games SignIn");
- GoogleSignIn.DefaultInstance.SignIn().ContinueWith(
- OnAuthenticationFinished);
- }
- private List<string> messages = new List<string>();
- void AddStatusText(string text) {
- if (messages.Count == 5) {
- messages.RemoveAt(0);
- }
- messages.Add(text);
- string txt = "";
- foreach (string s in messages) {
- txt += "\n" + s;
- }
- // statusText.text = txt;
- }
- #endregion
- void LoginProcess(Task<GoogleSignInUser> task)
- {
- WebService.Instnace.appUser.Address1 = "";
- WebService.Instnace.appUser.Address2 = "";
- WebService.Instnace.appUser.Country = "";
- WebService.Instnace.appUser.DateOfBirth = "";
- WebService.Instnace.appUser.Email = task.Result.Email;
- WebService.Instnace.appUser.Gender = "";
- WebService.Instnace.appUser.FirstName = task.Result.GivenName;
- WebService.Instnace.appUser.LastName = task.Result.FamilyName;
- WebService.Instnace.appUser.GoogleId = task.Result.UserId;
- WebService.Instnace.appUser.Mobile = "";
- WebService.Instnace.appUser.Password = "";
- WebService.Instnace.appUser.PostCode = "";
- WebService.Instnace.appUser.RegionId = AppManager.Instnace.regionId;
- WebService.Instnace.appUser.RelationshipStatus = "";
- WebService.Instnace.appUser.State = "";
- WebService.Instnace.appUser.Suburb = "";
- WebService.Instnace.appUser.LoginInfo = "Google";
- WebService.Instnace.appUser.isLoggedIn = 1;
- profilePicUrl = task.Result.ImageUrl.AbsoluteUri;
- AppManager.Instnace.isSocialSignInScreen = true;
- AppManager.Instnace.eSocial = eSocialSignUp.Social;
- string jsonHeader = "Google " + Convert.ToBase64String (Encoding.UTF8.GetBytes (WebService.Instnace.appUser.Email));
- WebService.Instnace.headerString = jsonHeader;
- string jsonParameter = JsonUtility.ToJson (WebService.Instnace.appUser);
- WebService.Instnace.headerString = jsonHeader;
- byte[] bodyRaw = Encoding.UTF8.GetBytes (jsonParameter);
- ACPUnityPlugin.Instnace.trackSocial("Google", "GoogleLogin", "GoogleLoginPressed");
- //****************************************************************************
- // VerifyUsers
- //****************************************************************************
- WebService.Instnace.Post (AppManager.Instnace.baseURL + "/cloud/ViewaUserActions.aspx/VerifyUsers", bodyRaw, jsonHeader, UserCheckCallback);
- }
- void UserCheckCallback(UnityWebRequest response ){
- var res = JSON.Parse (response.downloadHandler.text);
- //user already exist code = 500
- if (response.responseCode == 200) {
- if (res != null) {
- RootObject responseModel = JsonUtility.FromJson<RootObject> (response.downloadHandler.text);
- SaveUser (responseModel);
- } else {
- //Jeetesh - Hack - User already exist but we didn't get user data from server.
- Debug.Log("User present but details didn't come from server");
- AppManager.Instnace.messageBoxManager.HidePreloader ();
- }
- } else if(response.responseCode == 402) {
- UserNotRegistered ();
- }
- }
- void SaveUser(RootObject responseModel) {
- D _d = responseModel.d;
- OTPL.modal.User _user = _d.User;
- AppManager.Instnace.userId = _user.Id;
- AppManager.Instnace.regionId = _user.RegionId;
- AppManager.Instnace.userEmail = _user.Email;
- //Save Profile Picture
- StartCoroutine(AppManager.Instnace.SavePictureWithUrlAsPNG(profilePicUrl));
- ACPUnityPlugin.Instnace.trackSocial("Google", "GoogleLogin", "GoogleLoginUserLoggedIn");
- UserDataService userDbService = new UserDataService ();
- //if the user id exist then update user otherwise
- if (userDbService.UserExist (_user.Email) > 0) {
- userDbService.CreateOrReplaceUser (_user.Id, _user.Address1, _user.Address2, _user.Country, _user.DateOfBirth, _user.Email, _user.FirstName, _user.LastName, _user.Gender, _user.Mobile, _user.PostCode,
- _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);
- } else {
- userDbService.CreateOrReplaceUser (_user.Id, _user.Address1, _user.Address2, _user.Country, _user.DateOfBirth, _user.Email, _user.FirstName, _user.LastName, _user.Gender, _user.Mobile, _user.PostCode,
- _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);
- }
- AppManager.Instnace.GoToScanScreen ();
- }
- void UserNotRegistered() {
- CanvasManager.Instnace.ReturnPanelManager(ePanelManager.MainMenuPanelManager).NavigateToPanel(ePanels.SignUp_Panel);
- }
- }
Add Comment
Please, Sign In to add comment