Skip to main content
Answered

How to convert loginUnidentifiedUser to Registered user in React Native

  • February 18, 2025
  • 1 reply
  • 13 views

We are calling loginUnidentifiedUser() when user open the app without user login and if user logged in, then  calling 

Intercom.loginUserWithUserAttributes({
        userId: userId,
      })



but we are getting Error - Error in loginUserWithUserAttributes

 

Best answer by mateusz.leszkiewicz

Hi ​@seema.nagar It’s Mat from the Support Engineering Team 😀
 

The likely reason for this issue is conflict between an anonymous user session and an identified user session.

📌 Root Cause

1. Intercom does not automatically transition from an anonymous session to an identified session.

• When loginUnidentifiedUser() is called, Intercom creates a separate anonymous user session.

• When you try to log in a known user with loginUserWithUserAttributes(), the app is still in the anonymous session, causing an error.

2. You must clear the anonymous session before logging in a known user.

✅ Solution: Log Out Before Switching Users

 

Before calling loginUserWithUserAttributes(), explicitly log out the anonymous user:

Intercom.logout(); // Clears the existing anonymous session

Intercom.loginUserWithUserAttributes({
  userId: userId,
});

Updated Flow:

if (userIsLoggedIn) {
  Intercom.logout(); // Ensure a clean transition
  Intercom.loginUserWithUserAttributes({
    userId: userId,
  });
} else {
  Intercom.loginUnidentifiedUser(); // Keep anonymous session
}

🚀 Alternative: Check for Existing Sessions Before Login

 

To prevent unnecessary logouts, you can check if the user is already identified:

Intercom.getUserId((userId) => {
  if (!userId) {
    Intercom.loginUnidentifiedUser();
  }
});

Then, when a user logs in:

Intercom.getUserId((userId) => {
  if (userId !== loggedInUserId) {
    Intercom.logout();
    Intercom.loginUserWithUserAttributes({ userId: loggedInUserId });
  }
});

✅ Final Fix

1. Always call Intercom.logout() before loginUserWithUserAttributes().

2. Avoid calling loginUnidentifiedUser() if the user is logged in.

3. Use Intercom.getUserId() to check session state before switching users.

 

This should prevent session conflicts and resolve the error. Let me know if you need further debugging! 🚀

View original
Did this topic help you find an answer to your question?

1 reply

mateusz.leszkiewicz
Intercom Team
Forum|alt.badge.img+5

Hi ​@seema.nagar It’s Mat from the Support Engineering Team 😀
 

The likely reason for this issue is conflict between an anonymous user session and an identified user session.

📌 Root Cause

1. Intercom does not automatically transition from an anonymous session to an identified session.

• When loginUnidentifiedUser() is called, Intercom creates a separate anonymous user session.

• When you try to log in a known user with loginUserWithUserAttributes(), the app is still in the anonymous session, causing an error.

2. You must clear the anonymous session before logging in a known user.

✅ Solution: Log Out Before Switching Users

 

Before calling loginUserWithUserAttributes(), explicitly log out the anonymous user:

Intercom.logout(); // Clears the existing anonymous session

Intercom.loginUserWithUserAttributes({
  userId: userId,
});

Updated Flow:

if (userIsLoggedIn) {
  Intercom.logout(); // Ensure a clean transition
  Intercom.loginUserWithUserAttributes({
    userId: userId,
  });
} else {
  Intercom.loginUnidentifiedUser(); // Keep anonymous session
}

🚀 Alternative: Check for Existing Sessions Before Login

 

To prevent unnecessary logouts, you can check if the user is already identified:

Intercom.getUserId((userId) => {
  if (!userId) {
    Intercom.loginUnidentifiedUser();
  }
});

Then, when a user logs in:

Intercom.getUserId((userId) => {
  if (userId !== loggedInUserId) {
    Intercom.logout();
    Intercom.loginUserWithUserAttributes({ userId: loggedInUserId });
  }
});

✅ Final Fix

1. Always call Intercom.logout() before loginUserWithUserAttributes().

2. Avoid calling loginUnidentifiedUser() if the user is logged in.

3. Use Intercom.getUserId() to check session state before switching users.

 

This should prevent session conflicts and resolve the error. Let me know if you need further debugging! 🚀


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings