public async Task RefreshPage()
        {
            await firestoreService.LoadUser();

            await GoogleService.LoadTodaysEvents();

            //recalculate goals/routines durations
            calculateDuration();

            SetupUI();
            PrintFirebaseUser();
        }
        protected override async void OnAppearing()
        {
            if (Application.Current.Properties.ContainsKey("accessToken") &&
                Application.Current.Properties.ContainsKey("refreshToken") &&
                Application.Current.Properties.ContainsKey("user_id"))
            {
                App.LoadApplicationProperties();

                firestoreService         = new FirestoreService();
                firebaseFunctionsService = new FirebaseFunctionsService();

                await firestoreService.LoadUser();

                await GoogleService.LoadTodaysEvents();

                await Navigation.PushAsync(new GoalsRoutinesTemplate());
            }
        }
Example #3
0
        async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
        {
            var authenticator = sender as OAuth2Authenticator;

            if (authenticator != null)
            {
                authenticator.Completed -= OnAuthCompleted;
                authenticator.Error     -= OnAuthError;
            }

            if (e.IsAuthenticated)
            {
                Navigation.PushAsync(new LoadingPage());

                // If the user is authenticated, request their basic user data from Google
                // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo
                var request  = new OAuth2Request("GET", new Uri(Constants.UserInfoUrl), null, e.Account);
                var response = await request.GetResponseAsync();

                JObject userJson = null;
                if (response != null)
                {
                    // Deserialize the data and store it in the account store
                    // The users email address will be used to identify data in SimpleDB
                    string userJsonString = await response.GetResponseTextAsync();

                    userJson = JObject.Parse(userJsonString);
                }

                if (userJson != null)
                {
                    //store.Delete(account, Constants.AppName);
                    //await store.SaveAsync(account = e.Account, Constants.AppName);
                    //await DisplayAlert("Login Successful", "", "OK");

                    //Display Successful Login Alert
                    //await DisplayAlert("Login Successful", "", "OK");

                    //Write the Toekn to console, in case it changes
                    Console.WriteLine("HERE is the TOKEN------------------------------------------------");
                    Console.WriteLine(e.Account.Properties["access_token"]);
                    Console.WriteLine("HERE is the REFRESH TOKEN----------------------------------------");
                    Console.WriteLine(e.Account.Properties["refresh_token"]);
                    Console.WriteLine("----------------------------------------------------------------");

                    //Reset accessToken
                    accessToken  = e.Account.Properties["access_token"];
                    refreshToken = e.Account.Properties["refresh_token"];

                    App.User = new user();
                    firebaseFunctionsService = new FirebaseFunctionsService();

                    //Query for email in Users collection
                    App.User.email = userJson["email"].ToString();
                    App.User.id    = firebaseFunctionsService.FindUserDoc(App.User.email).Result;

                    if (App.User.id == "")
                    {
                        await DisplayAlert("Oops!", "Looks like your trusted advisor hasn't registered your account yet. Please ask for their assistance!", "OK");

                        await Navigation.PushAsync(new LoginPage());

                        return;
                    }

                    firestoreService = new FirestoreService();

                    //Save to App.User AND Update Firebase with pertitnent info
                    var googleService = new GoogleService();
                    await googleService.SaveAccessTokenToFireBase(accessToken);

                    Console.WriteLine(refreshToken);
                    await googleService.SaveRefreshTokenToFireBase(refreshToken);

                    //Save Properies inside phone for auto login
                    Application.Current.Properties["accessToken"]  = accessToken;
                    Application.Current.Properties["refreshToken"] = refreshToken;
                    Application.Current.Properties["user_id"]      = App.User.id;

                    App.LoadApplicationProperties();

                    await firestoreService.LoadUser();

                    await GoogleService.LoadTodaysEvents();

                    //Navigate to the Daily Page after Login
                    await Navigation.PushAsync(new GoalsRoutinesTemplate());
                }
            }
        }