# Delegating token with Firebase after linking accounts with a Rule

**URL:** <https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356>\
**Category:** Get Help\
**Tags:** firebase, social-connections, lock\
**Created:** [April 25, 2017, 10:25pm UTC](https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356 "2017-04-25T22:25:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adampmoore](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adampmoore/32/2181_2.png) [@adampmoore](https://community.auth0.com/u/adampmoore)\
**Post date:** [April 25, 2017, 10:25pm UTC](https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356/1 "2017-04-25T22:25:19Z")

</div>

I have username/password login integration with Firebase in my web app and that is working great. I’m attempting to add ability to login with Facebook, and link accounts using this Rule:  
[https://github.com/auth0/rules/blob/master/rules/link-users-by-email.md](https://github.com/auth0/rules/blob/master/rules/link-users-by-email.md)

This works and does link and merge accounts in Auth0. However, it does not seem to delegate the token with firebase correctly on the app side… in my app.js I have this for delegating the token:

```
lock.on("authenticated", function(authResult) {
    localStorage.setItem('id_token', authResult.idToken);
    localStorage.setItem('access_token', authResult.accessToken);

     // Set the options to retreive a firebase delegation token
        var options = {
          id_token : authResult.idToken,
          api : 'firebase',
          scope : 'openid name email',
          target: auth0ClientId
        };

        // Make a call to the Auth0 '/delegate'
        auth0.getDelegationToken(options, function(err, result) {
          if(!err) {
            // Exchange the delegate token for a Firebase auth token
            firebase.auth().signInWithCustomToken(result.id_token).catch(function(error) {
              console.log(error);
            });
          }
        });

      lock.getUserInfo(authResult.accessToken, function(error, profile) {

        if (error) {
          console.log(error);
          return;
        }

        localStorage.setItem('profile', JSON.stringify(profile));
        authUserInit();

    });

```

I think maybe the timing between the Rule and the getDelegationToken is out of order - do I have to move that piece to the rule or can I trigger it to run again once the accounts have been linked?

---

<div class="post-metadata">

**Author:** ![tuongaz](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@tuongaz](https://community.auth0.com/u/tuongaz)\
**Post date:** [May 2, 2017, 11:05am UTC](https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356/2 "2017-05-02T11:05:11Z")

</div>

I am having the same issue! Anyone can help?

---

<div class="post-metadata">

**Author:** ![adampmoore](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adampmoore/32/2181_2.png) [@adampmoore](https://community.auth0.com/u/adampmoore)\
**Post date:** [May 2, 2017, 11:11am UTC](https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356/3 "2017-05-02T11:11:02Z")

</div>

I was able to solve this I believe by changing:

scope : ‘openid name email’

To:

scope : ‘openid’,

---

<div class="post-metadata">

**Author:** ![tuongaz](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@tuongaz](https://community.auth0.com/u/tuongaz)\
**Post date:** [May 2, 2017, 11:33am UTC](https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356/4 "2017-05-02T11:33:36Z")

</div>

I have just tried but it didn’t work, still 2 accounts in Firebase

---

<div class="post-metadata">

**Author:** ![adampmoore](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adampmoore/32/2181_2.png) [@adampmoore](https://community.auth0.com/u/adampmoore)\
**Post date:** [May 2, 2017, 11:47am UTC](https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356/5 "2017-05-02T11:47:49Z")

</div>

Oh, I think you are having a different problem than my original post. I’m still having the duplicate firebase account problem, going to work on that this week, if I find a solution I’ll post it here.

---

<div class="post-metadata">

**Author:** ![tuongaz](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@tuongaz](https://community.auth0.com/u/tuongaz)\
**Post date:** [May 2, 2017, 11:57am UTC](https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356/6 "2017-05-02T11:57:51Z")

</div>

I think i found the Firebase duplicate account issue, it’s because the Rule script, it picks the new account as the primary account, which at that time does not exist in the Firebase. To solve this, it needs to pick the existing account in the Firebase as the primary account.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 2, 2018, 5:53am UTC](https://community.auth0.com/t/delegating-token-with-firebase-after-linking-accounts-with-a-rule/6356/7 "2018-03-02T05:53:29Z")

</div>


