xamarin / Xamarin.Social

Xamarin.Social
Apache License 2.0
123 stars 74 forks source link

Services need a way to delete and deauthorize accounts #5

Open praeclarum opened 11 years ago

praeclarum commented 11 years ago

If an account goes bad, then we need a way to delete them from the account store.

    public static void DeleteAccount (this FacebookService facebook, Account account)
    {
        var acs = AccountStore.Create ();
        //          acs.Delete (account);
    }

    public static async Task<bool> DeauthorizeAccount (this FacebookService facebook, Account account)
    {
        var acs = AccountStore.Create ();
        acs.Delete (account);

        var req = facebook.CreateRequest (
            "DELETE",
            new Uri ("https://graph.facebook.com/me/permissions"),
            account);
        using (var resp = await req.GetResponseAsync ()) {
            return (bool)JsonValue.Parse (resp.GetResponseText ());
        }
    }
johnbeans commented 10 years ago

If you also have the Xamarin.Auth component, you can delete it yourself. I have done it like this in a Xamarin.Forms app:

            var theChangeAccountButton = new Button {
                TextColor = Color.White,
                Font = Font.OfSize (textFont, textSize),
                Text = "Change account",
                BackgroundColor = Color.Transparent,
            };
            string flickrUser ="";
            if (accounts.Count () > 0) {
                flickrUser = accounts.First ().Username;
                theChangeAccountButton.Text = "Logout of " + flickrUser;
            } else {
                theChangeAccountButton.IsVisible = false;
            }
            theChangeAccountButton.Clicked +=  (sender, e) => {
                var store = AccountStore.Create ();
                IEnumerable<Account> accounts = store.FindAccountsForService ("Flickr");
                Console.WriteLine("Number of flickr accounts before is " + accounts.Count().ToString());
                Account thisAccount;
                if(accounts.Count() > 0){
                    for(int i=0;i<accounts.Count();i++){
                        thisAccount = accounts.First();
                        store.Delete(thisAccount,"Flickr");
                    }
                }
                accounts = store.FindAccountsForService ("Flickr");
                Console.WriteLine("Number of flickr accounts afterwards is " + accounts.Count());
                if(accounts.Count() == 0){
                    theChangeAccountButton.IsVisible = false;
                    DisplayAlert("Flickr","You are logged out of "+flickrUser+".","OK");
                }else{
                    DisplayAlert("Flickr","For some reason, that didn't work.","OK");
                    flickrUser = accounts.First ().Username;
                    theChangeAccountButton.Text = "Logout of " + flickrUser;
                }

            };

When there are no flickr users saved, the button is hidden. When there is at least one, the button shows "Logout of user@domain.com". Normally, there's just one, so handling multiple flickr accounts is just for robustness.