示例#1
0
        async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
        {
            var authenticator = sender as OAuth2Authenticator;

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

            GoogleUsers user = null;

            if (e.IsAuthenticated)
            {
                // 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();

                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 userJson = await response.GetResponseTextAsync();

                    user            = JsonConvert.DeserializeObject <GoogleUsers>(userJson);
                    user.IsToggled  = false;
                    user.IsLoggedIn = true;
                }


                if (account != null)
                {
                    store.Delete(account, Constants.AppName);
                }



                await store.SaveAsync(account = e.Account, Constants.AppName);


                bool newuser = false;
                if (await FirebaseHelper.CheckEmail(user.Email) == false)
                {
                    await FirebaseHelper.AddUser(user.Email, user.Picture, user.Name, user.Id, user.IsToggled, user.IsLoggedIn);

                    newuser = true;
                }


                ClearPersisitance();

                Application.Current.Properties.Add("Id", user.Id);
                Application.Current.Properties.Add("FirstName", user.GivenName);
                Application.Current.Properties.Add("LastName", user.FamilyName);
                Application.Current.Properties.Add("DisplayName", user.Name);
                Application.Current.Properties.Add("EmailAddress", user.Email);
                Application.Current.Properties.Add("ProfilePicture", user.Picture);
                Application.Current.Properties.Add("IsToggled", false);
                await Application.Current.SavePropertiesAsync();                 //persistance

                if (newuser)
                {
                    await FirebaseHelper.AddPantryItem("Example Item", "5", "12/15");
                }



                App.Current.MainPage = new AppShell();
            }
        }