Search code examples
firebasetwitterfirebase-authenticationtwitter-oauth

Retrieving Firebase Twitter Auth tokens after initial sign in


Just following the firebase twitter auth guide and I'm curious if there is a way to retrieve the twitter accessToken and secret after the first sign in.

You can retrieve these after logging in the first time with

firebase.auth().getRedirectResult().then(function (result) {
        if (result.credential) {
            // This gives you a the Twitter OAuth 1.0 Access Token and Secret.
            // You can use these server side with your app's credentials to access the Twitter API.
            var token = result.credential.accessToken;
            var secret = result.credential.secret;
            console.log([token, secret]);
            // ...
        }
        // The signed-in user info.
        var user = result.user;
        //console.log(user);
    }).catch(function (error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
    });

However, based on my understanding, this is only run the first time after logging in with Twitter.

How can I retrieve their twitter accessToken and secret when they open the page again? Should I store them when I get them the first time?


Solution

  • You can't. Firebase Auth does not store these credentials. If you need them for later use, you would need to programmatically store them after first sign-in. You could use Firebase realtime database/Firestore to do so but make sure that they can only be accessed by the corresponding authenticated user or not publicly accessible if you plan to use them server side.