# Account Linking

**URL:** <https://forum.keycloak.org/t/account-linking/18384>\
**Category:** Getting advice\
**Tags:** authentication, identity-brokering, oidc\
**Created:** [November 11, 2022, 8:04am UTC](https://forum.keycloak.org/t/account-linking/18384 "2022-11-11T08:04:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nizcentrox](https://avatars.discourse-cdn.com/v4/letter/n/bc79bd/32.png) [@nizcentrox](https://forum.keycloak.org/u/nizcentrox)\
**Post date:** [November 11, 2022, 8:04am UTC](https://forum.keycloak.org/t/account-linking/18384/1 "2022-11-11T08:04:31Z")

</div>

Need help with Account Linking

Info:  
Backend is in NodeJS  
Frontend is ReactNative  
Keycloak v18

Requirement:

1. Frontend fetches the linking URI genereted by backend
2. Hits the URI to initiate account linking to external idp

Problem:  
When hitting the URI generated from frontend with or without the access\_token, we get the “not\_logged\_in” error

When we attach the cookies ( getting them from impersonation call on backend using service token ):  
Keycloak\_identity, Keycloak\_identity\_legacy, Keycloak\_session, Keycloak\_session\_legacy,  
we get a new error = “invalid\_token”

( tested using postman for cross domain cookies )

We have tried multiple values for session Id i.g, session id from access token the user is using, session id from the retrieved cookie ( keycloak\_identity)

Link to Keycloak Doc for Account Linking: [Server Developer Guide](https://www.keycloak.org/docs/latest/server_development/#client-initiated-account-linking)

One of the code sample we have tried:

```auto
  const sessionState = await extractSessionStateFromRequest(req)
  const clientId = KEYCLOAK_ID // this is the client id being used for backend connectivity

  const nonce = randomUUID()
  const idp = "google-link"
  const input = nonce + sessionState + clientId + idp
  const hash = crypto.createHash("sha256").update(input).digest("base64");
  const base64HashedData = hash.split('+').join('-').split('/').join('_').split('=').join('')

  const url = new URL(`${KEYCLOAK_URL}/realms/${KEYCLOAK_REALM}/broker/${idp}/link`)
  url.searchParams.set("nonce", nonce)
  url.searchParams.set("hash", base64HashedData)
  url.searchParams.set("client_id", clientId)
  url.searchParams.set("redirect_uri", "some_url")

  apiOk(res, { url });

```

Thanks for reading it.  
Any help is appreciated.

Have a nice day!

---

<div class="post-metadata">

**Author:** ![iglimanaj](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/iglimanaj/32/3251_2.png) [@iglimanaj](https://forum.keycloak.org/u/iglimanaj)\
**Post date:** [February 17, 2023, 10:23pm UTC](https://forum.keycloak.org/t/account-linking/18384/2 "2023-02-17T22:23:00Z")

</div>

I have experienced the same problem. Any hints would help me as well 🙂

---

<div class="post-metadata">

**Author:** ![Barolina](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/barolina/32/12088_2.png) [@Barolina](https://forum.keycloak.org/u/Barolina)\
**Post date:** [April 14, 2025, 7:18am UTC](https://forum.keycloak.org/t/account-linking/18384/3 "2025-04-14T07:18:37Z")

</div>

Can you tell me if you were able to resolve this issue?
