How to reset customer's identityin a FcWidget?

Hello,

Problem I face:
I am implementing the FcWidget to my page. I would like to have the FcWidget service available to the customers before and after a customer has logged in. My problem is that when the user (customer A) has been created on Freshchat, the FcWidget is always associated with that account (customer A), even when the customer has logged out or even logged in as another customer (customer B) on my site.

For switching between different logged in users, I guess storing the restoreId is the solution (am I right?)

But for logged out customers, I want the FcWidget to consider the customers as anonymous user and I have no idea how to do that. I tried to delete the browser history, but it doesn’t clear the association.

My code:

    let widgetConfig: FreshChatConfig = {
      token: TOKEN,
      host: HOST,
      restoreId: null
    };
    
    const { firstName, lastName } = this.userData;
    if (firstName || lastName) {
      widgetConfig = { ...widgetConfig, firstName, lastName };
    }

    window.fcWidget.init(widgetConfig);

    console.log(
      await window.fcWidget.user.get()  // it always return the user object even when the user is not logged in: { data: { firstName: "Max", lastName: "Smith", …}, success: true, status: 200}
    );
  }

Any help will be appriciated. Thanks.

2 Likes

hey, any suggestions?

Hi @shan,

You can use window.fcWidget.user.clear() to reset the widget. The sample code is below.

window.fcWidget.user.clear().then(function() {
  console.log('User cleared');
  }, function() {
    console.log("User Not cleared");
  });

We usually recommend this action take place on click of Logout, and on the success of this complete the app’s logout action.

Let me know if this helps.

2 Likes