How to display logged in user's info into widget?

How to ensure that the chat updates with user details (name, email) when they log in.
Similarly, chat sessions is cleared when users log out.
Also when the user switches different languages how to load the widget with translation without reloading?
Here is my code :

// @ts-ignore
import TawkMessengerReact from '@tawk.to/tawk-messenger-react';
import CryptoJS from 'crypto-js';
import { useCallback, useEffect, useRef } from 'react';

interface ITawkComponent {
  authUser: any;
  lang: string;
}

declare global {
  interface Window {
    Tawk_API: any; // Adjust the type accordingly if you have more specific information about the Tawk API
  }
}

function hashInBase64(userId: string): string {
  const secretKey = 'b4a7d6783ff8**********f3c952d2a561dba883';
  const hash = CryptoJS.HmacSHA256(userId, secretKey);
  const hashInBase64 = CryptoJS.enc.Hex.stringify(hash);
  return hashInBase64;
}

const TawkComponent: React.FC<ITawkComponent> = ({ authUser, lang }) => {
  const tawkMessengerRef = useRef<any>();

  const loginToTawk = useCallback(() => {
    const userId = localStorage.getItem('userId');    //example : JLT6XX4MEFVS
    const userName = localStorage.getItem('userName');
    const userEmail = localStorage.getItem('userEmail');
    if (!userId || !userName || !userEmail) return; // Check if user information is available

    const hash = hashInBase64(userId);

    window.Tawk_API = window.Tawk_API || {};
    window.Tawk_API.login(
      {
        hash: hash,
        userId: userId,
        name: userName,
        email: userEmail,
      },
      function (error: any) {
        if (error) {
          console.error('Error occurred while logging in to Tawk:', error);
          // Handle error if needed
        } else {
          console.log('User logged in to Tawk successfully');
          // You can add further actions if login is successful
        }
      }
    );
  }, []);

  const onChatMaximized = useCallback(() => {
    loginToTawk();
  }, [loginToTawk]);

  useEffect(() => {
    const script = document.createElement('script');
    script.async = true;
    script.charset = 'UTF-8';
    script.setAttribute('crossorigin', '*');

    const widgetId = lang === 'en' || lang === 'de' ? '1g***d3ld' : '1ge***ccu';
    script.src = `https://embed.tawk.to/633605c******e12d8979f6c/${widgetId}`;

    script.onload = () => {
      window.Tawk_API = window.Tawk_API || {};
      window.Tawk_API.onLoad = () => {
        // Additional logic on Tawk load if needed
      };
    };

    document.head.appendChild(script);

    return () => {
      document.head.removeChild(script);
    };
  }, [lang]);

  return (
    <>
      {authUser ? (
        <TawkMessengerReact
          propertyId='633605c******e12d8979f6c'
          widgetId={lang === 'en' || lang === 'de' ? '1g***d3ld' : '1ge***ccu'}
          ref={tawkMessengerRef}
          onChatMaximized={onChatMaximized}
        />
      ) : (
        <TawkMessengerReact
          propertyId='633605c******e12d8979f6c'
          widgetId={lang === 'en' || lang === 'de' ? '1g***d3ld' : '1ge***ccu'}
          ref={tawkMessengerRef}
        />
      )}
    </>
  );
};

export default TawkComponent;

I tried this way but it does not work.

I’ve been struggling with this for literally years. It seems when you set an email it appends to a list of emails with there being some “primary” email Tawk set (The first one you ever pass in).

Since i want to let guests talk to me, i have a general guest email of guests@mydomain.com.

The issue is when i later log the user in, that guest email seems to stick around and theres no way to ditch it.

Further, even although in app, i see my test users details, the display name is all messed up. It doesnt seem to matter what i set the display name as, forever i see the V9656780986756 type label in the main screen on the app. Sometimes it randomly works, but i cannot for the life of me figure out why and when it will and wont work.

This app is just buggy…i think writing my own little client is the next step because this is kind of crazy.

Screenshots in case someone decides to look into this, which i’d love…


In this one i expect the display name to be [free]andrew… etc but its still the V173554… string, why? no idea

Same on the main screen (but im limited to one embed so cant post that)

2 Likes