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

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

            User 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 <User>(userJson);
                }

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

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

                //UserDialogs.Instance.Alert("", "Email address: " + user.Email + "\n fullname:" + user.Name + "\n gender:" + user.Gender, "OK");
                MyToast t = new MyToast();
                UserDialogs.Instance.Toast(t.ShowMyToast(Color.Green, "Successful google login"));
                User.Username = user.Email;
                User.Password = user.Email;
            }
            else
            {
                MyToast t = new MyToast();
                UserDialogs.Instance.Toast(t.ShowMyToast(Color.PaleVioletRed, "Unsuccessful google login"));
            }
        }
示例#2
0
        async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
        {
            try
            {
                using (UserDialogs.Instance.Loading(""))
                {
                    var authenticator = sender as OAuth2Authenticator;
                    if (authenticator != null)
                    {
                        authenticator.Completed -= OnAuthCompleted;
                        authenticator.Error     -= OnAuthError;
                    }

                    User user = null;
                    if (e.IsAuthenticated)
                    {
                        OAuth2Request oAuth2Request = null;
                        string        returnedJson  = null;
                        // If the user is authenticated, request their basic user data from Google
                        // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo
                        switch (SocialMedia)
                        {
                        case "facebook":
                            oAuth2Request = new OAuth2Request("GET", new Uri(Constants.graphAPI), null, e.Account);
                            break;

                        case "google":
                            oAuth2Request = new OAuth2Request("GET", new Uri(Constants.UserInfoUrl), null, e.Account);

                            break;

                        default:
                            break;
                        }
                        //var request = new OAuth2Request("GET", new Uri(Constants.UserInfoUrl), null, e.Account);
                        var response = await oAuth2Request.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();

                            switch (SocialMedia)
                            {
                            case "google":
                                try
                                {
                                    user = JsonConvert.DeserializeObject <User>(userJson);
                                    lvm.User.Username     = user.Email;
                                    lvm.User.Password     = user.Email;
                                    lvm.User.EmailAddress = user.Email;
                                    lvm.User.ConfirmPin   = user.Email;
                                }
                                catch (Exception ex)
                                {
                                    var log = ex;
                                }
                                break;

                            case "facebook":
                                try
                                {
                                    var fbuser = JsonConvert.DeserializeObject <FaceBookProfile>(userJson);

                                    //var fbuser = JsonValue.Parse(response.GetResponseText());
                                    lvm.User.Username     = fbuser.Email;
                                    lvm.User.Password     = fbuser.Email;
                                    lvm.User.EmailAddress = fbuser.Email;
                                    lvm.User.ConfirmPin   = fbuser.Email;
                                }
                                catch (Exception ex)
                                {
                                    var log = ex;
                                }

                                break;

                            default:
                                break;
                            }
                        }

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

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

                        //UserDialogs.Instance.Alert("", "Email address: " + user.Email + "\n fullname:" + user.Name + "\n gender:" + user.Gender, "OK");
                        MyToast t = new MyToast();
                        UserDialogs.Instance.Toast(t.ShowMyToast(Color.Green, "Successful login"));
                        GlobalStaticFields.Username = lvm.User.Username;
                        var tk = await lvm.GetTokenFromAPI().ConfigureAwait(false);

                        if (tk)
                        {
                            lvm.PersistUserDetails();
                            //Application.Current.MainPage.Navigation.PushAsync(new AppLanding());
                            Device.BeginInvokeOnMainThread
                            (
                                async() => await Application.Current.MainPage.Navigation.PushAsync(new AppLanding())
                            );
                        }
                        else
                        {
                            var reg = await lvm.Register().ConfigureAwait(false);

                            if (reg)
                            {
                                //lvm.PersistUserDetails();
                                Application.Current.MainPage.Navigation.PushAsync(new AppLanding());
                                //Device.BeginInvokeOnMainThread
                                //  (
                                //async () => await Application.Current.MainPage.Navigation.PushAsync(new AppLanding())
                                //);
                            }
                            else
                            {
                                Device.BeginInvokeOnMainThread
                                    (() =>
                                    UserDialogs.Instance.Toast(t.ShowMyToast(Color.OrangeRed, $"Unsuccessful. {lvm.RegisterationError} ")));
                            }
                        }
                    }
                    else
                    {
                        MyToast t = new MyToast();
                        UserDialogs.Instance.Toast(t.ShowMyToast(Color.PaleVioletRed, $"Unsuccessful {SocialMedia} login"));
                    }
                }
            }
            catch (Exception ex)
            {
                var log = ex;
            }
        }
示例#3
0
        async void OnAuthCompleted(object sender, AuthenticatorCompletedEventArgs e)
        {
            using (UserDialogs.Instance.Loading(""))
            {
                var authenticator = sender as OAuth2Authenticator;
                if (authenticator != null)
                {
                    authenticator.Completed -= OnAuthCompleted;
                    authenticator.Error     -= OnAuthError;
                }

                User 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 <User>(userJson);
                    }

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

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

                    //UserDialogs.Instance.Alert("", "Email address: " + user.Email + "\n fullname:" + user.Name + "\n gender:" + user.Gender, "OK");
                    MyToast t = new MyToast();
                    UserDialogs.Instance.Toast(t.ShowMyToast(Color.Green, "Successful google login"));
                    lvm.User.Username     = user.Email;
                    lvm.User.Password     = user.Email;
                    lvm.User.EmailAddress = user.Email;
                    lvm.User.ConfirmPin   = user.Email;
                    var tk = await lvm.GetTokenFromAPI().ConfigureAwait(false);

                    if (tk)
                    {
                        Navigation.PushAsync(new AppLanding());
                    }
                    else
                    {
                        var reg = await lvm.Register().ConfigureAwait(false);

                        if (reg)
                        {
                            Device.BeginInvokeOnMainThread
                            (
                                async() => Navigation.PushAsync(new AppLanding())
                            );
                        }
                        else
                        {
                            Device.BeginInvokeOnMainThread
                                (() =>
                                UserDialogs.Instance.Toast(t.ShowMyToast(Color.OrangeRed, $"Unsuccessful. {lvm.RegisterationError} ")));
                        }
                    }
                }
                else
                {
                    MyToast t = new MyToast();
                    UserDialogs.Instance.Toast(t.ShowMyToast(Color.PaleVioletRed, "Unsuccessful google login"));
                }
            }
        }