Example #1
0
 public Athlete(GoogleUserProfile profile)
 {
     Name             = profile.Name;
     Email            = profile.Email;
     AuthenticationId = profile.Id;
     ProfileImageUrl  = profile.Picture;
     Initialize();
 }
        /// <summary>
        /// Registers an athlete with the backend and returns the new athlete profile
        /// </summary>
        async Task <Athlete> RegisterAthlete(GoogleUserProfile profile)
        {
            AuthenticationStatus = "Registering athlete";
            var athlete = new Athlete(profile);

            var task = AzureService.Instance.SaveAthlete(athlete);

            await RunSafe(task);

            if (task.IsCompleted && task.IsFaulted)
            {
                return(null);
            }

            "You're now an officially registered athlete!".ToToast();
            return(athlete);
        }
        public void LogOut(bool clearCookies)
        {
            Utility.SetSecured("AuthToken", string.Empty, "xamarin.sport", "authentication");
            AzureService.Instance.Client.Logout();

            App.AuthToken                = null;
            AuthUserProfile              = null;
            Settings.Instance.AthleteId  = null;
            Settings.Instance.AuthUserID = null;

            if (clearCookies)
            {
                Settings.Instance.RegistrationComplete = false;
                _authenticator.ClearCookies();
            }

            Settings.Instance.Save();
        }
        /// <summary>
        /// Attempts to get the user profile from Google. Will use the refresh token if the auth token has expired
        /// </summary>
        async public Task <bool> GetUserProfile()
        {
            //Can't get profile w/out a token
            if (App.AuthToken == null)
            {
                return(false);
            }

            if (AuthUserProfile != null)
            {
                return(true);
            }

            using (new Busy(this))
            {
                AuthenticationStatus = "Getting Google user profile";
                var task = GoogleApiService.Instance.GetUserProfile();
                await RunSafe(task, false);

                if (task.IsFaulted && task.IsCompleted)
                {
                    //Need to get refresh token from Azure somehow
                    //Likely our authtoken has expired
//					AuthenticationStatus = "Refreshing token";
//
//					var refreshTask = GoogleApiService.Instance.GetNewAuthToken(Settings.Instance.RefreshToken);
//					await RunSafe(refreshTask);
//
//					if(refreshTask.IsCompleted && !refreshTask.IsFaulted)
//					{
//						//Success in getting a new auth token - now lets attempt to get the profile again
//						if(!string.IsNullOrWhiteSpace(refreshTask.Result) && App.AuthToken != refreshTask.Result)
//						{
//							//We have a valid token now, let's try this again
//							App.AuthToken = refreshTask.Result;
//							await Settings.Instance.Save();
//							return await GetUserProfile();
//						}
//					}
                }

                if (task.IsCompleted && !task.IsFaulted && task.Result != null)
                {
                    AuthenticationStatus = "Authentication complete";
                    AuthUserProfile      = task.Result;

                    InsightsManager.Identify(AuthUserProfile.Email, new Dictionary <string, string> {
                        {
                            "Name",
                            AuthUserProfile.Name
                        }
                    });

                    Settings.Instance.AuthUserID = AuthUserProfile.Id;
                    await Settings.Instance.Save();
                }
                else
                {
                    AuthenticationStatus = "Unable to authenticate";
                }
            }

            return(AuthUserProfile != null);
        }