# Account Linking with Next JS and Auth0

**URL:** <https://community.auth0.com/t/account-linking-with-next-js-and-auth0/61922>\
**Category:** Get Help\
**Tags:** nextjs, auth0\
**Created:** [April 28, 2021, 7:58pm UTC](https://community.auth0.com/t/account-linking-with-next-js-and-auth0/61922 "2021-04-28T19:58:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dpramanik](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dpramanik/32/26194_2.png) [@dpramanik](https://community.auth0.com/u/dpramanik)\
**Post date:** [April 28, 2021, 7:58pm UTC](https://community.auth0.com/t/account-linking-with-next-js-and-auth0/61922/1 "2021-04-28T19:58:50Z")

</div>

Hi

In my nextjs application I have a user settings page which has a ‘Link Google’ button. The desired functionality is, users should be able to link their google accounts. That can be with the same email they logged in with or a different email. How can we achieve that ?

I am using the nextjs-auth0 package and universal login.

Thanks  
Debabrata Pramanik

---

<div class="post-metadata">

**Author:** ![ricardo.batista](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ricardo.batista/32/7711_2.png) [@ricardo.batista](https://community.auth0.com/u/ricardo.batista)\
**Post date:** [April 29, 2021, 7:02am UTC](https://community.auth0.com/t/account-linking-with-next-js-and-auth0/61922/2 "2021-04-29T07:02:48Z")

</div>

Hi @dpramanik

You should be able to use the [user account linking](https://auth0.com/docs/users/user-account-linking) for this. Specifically the **User-initiated account linking** , which would allow your users to link their accounts using an admin screen in your app, as you’ve specified.

You can check an example implementation here: [User-Initiated Account Linking: Client-Side Implementation](https://auth0.com/docs/users/user-initiated-account-linking-client-side-implementation)

---

<div class="post-metadata">

**Author:** ![dpramanik](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dpramanik/32/26194_2.png) [@dpramanik](https://community.auth0.com/u/dpramanik)\
**Post date:** [April 29, 2021, 9:18pm UTC](https://community.auth0.com/t/account-linking-with-next-js-and-auth0/61922/3 "2021-04-29T21:18:15Z")

</div>

Hey @ricardo.batista, thanks for replying.  
Yes I am following the link you shared, but am having few issues.  
Firstly, I am using nextjs-auth0 library with universal login and my app is a regular web app.

The steps that I am doing are as follows:

- Lets’s assume that a user has signed in normally with email and password and now he goes to his settings page and clicks the link facebook button. At this point I am redirecting them to /api/auth/login?type=facebook. Why I am passing this type is because I wanted to show the users the facebook login screen. Else, since the user is already logged in it will redirect the user to the logged in homepage.

- Now inside […auth0.js] → handleAuth → login function, I am catching that type. If type is there, which indicates that account linking was initiated, I am calling the handleLogin function having authorizationParams with connection as type. When user goes through this process, a new user fb user is created inside auth0. (I have doubt here, as I don’t want that new user to be created inside auth0, but at the same time I need that login screen and idToken of the new user.). Before this process, I have the type and sub of the primary logged in user stored in local variables(as we will need them to call the management api).

- Now when the new fb user authenticates, inside afterCallback, I am taking the idToken from session and calling the managementAPI. But the call fails.

I am certain that something is missing from my flow.

Thanks  
Debabrata Pramanik

---

<div class="post-metadata">

**Author:** ![dpramanik](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dpramanik/32/26194_2.png) [@dpramanik](https://community.auth0.com/u/dpramanik)\
**Post date:** [April 30, 2021, 9:48pm UTC](https://community.auth0.com/t/account-linking-with-next-js-and-auth0/61922/4 "2021-04-30T21:48:54Z")

</div>

The above flow seems to be working. After a successful call back from the management api, the accounts are getting merged into one. We get back the session of the new user as expected inside afterCallback function, we will need to handle these. The management api call goes like the follow.

Thanks @ricardo.batista. I will close this for now.

```
await fetch(`${domain}/api/v2/users/${sub}/identities`, { // sub / user id of the primary user
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token}`, // management api token
    },
    body: JSON.stringify({
      provider: type, // connection type
      user_id: session.user.sub, // secondary user id
    }),
  });

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [May 4, 2021, 1:36pm UTC](https://community.auth0.com/t/account-linking-with-next-js-and-auth0/61922/5 "2021-05-04T13:36:45Z")

</div>

Thanks! Let us know if you have any other questions down the road!

---

<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:** [May 19, 2021, 1:37pm UTC](https://community.auth0.com/t/account-linking-with-next-js-and-auth0/61922/6 "2021-05-19T13:37:12Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
