Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twitter logout / account switch #81

Open
raphaelyancey opened this issue Feb 5, 2016 · 1 comment
Open

Twitter logout / account switch #81

raphaelyancey opened this issue Feb 5, 2016 · 1 comment

Comments

@raphaelyancey
Copy link

As it seems impossible to destroy the session & cache via the OAuth.io API to implement a logout function, I implemented a Switch Account button in my application that calls for .popup('twitter') with force_login: true as specified in the Twitter API docs.

This forces the user to log in again, and therefore I can overwrite the api object returned by the .popup() method when successful. But when using this new API, it posts as the previous account (before switching), even if the tokens are different.

Interesting bits of code (Angular service)

Service.login = function(force) {

    var q = $q.defer();

    OAuth.popup('twitter', { cache: !force, authorize: { force_login: !!force }})
    .done(function(api) {
        Service.api = api;
        Service.api.get('1.1/account/verify_credentials.json')
        .done(function(user) {
            Service.user = user;
            q.resolve(user);
        })
        .fail(function(err) {
            Service.user = null;
            q.reject({ message: 'CANNOT_GET_USER_INFO', error: err });
        })
    })
    .fail(function(err) { q.reject({ message: 'LOGIN_FAILED', error: err }) });

    return q.promise;
}
Service.post = function(post, pictures) {

    var q = $q.defer();

    Service.login()
    .then(function(user) {
        Service.api.post('1.1/statuses/update.json', { data: { status: post } })
        .done(function(posted) { q.resolve(posted) })
        .fail(function(err) { q.reject({ message: 'POST_FAILED', error: err }) });
    })
    .catch(function(err) { q.reject(err) });

    return q.promise;
}

In my controller (stripped down but you get the idea)
<button ng-click="login(true)">Switch account</button>

@raphaelyancey
Copy link
Author

Okay, that's how I implemented it : it deletes related localStorage items. This method is called in the Service.login(true) method where I added if(!!force) Service.logout().

Service.logout = function() {
    var keys = ['oauthio_cache', 'oauthio_provider_twitter'];
    _.each(keys, function(key) { localStorage.removeItem(key) });
    return _.reduce(keys, function(memo, key) { return !!localStorage.getItem(key) && memo }, false);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant