Skip to main content

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

 

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! 🚀


Reply