# Server returns 500 after token refresh

**URL:** https://community.sendbird.com/t/server-returns-500-after-token-refresh/8348
**Category:** React
**Created:** [September 26, 2023, 2:16pm UTC](https://community.sendbird.com/t/server-returns-500-after-token-refresh/8348 "2023-09-26T14:16:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![amit](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/amit/32/4245_2.png) [@amit](https://community.sendbird.com/u/amit)
#### Post date: [September 26, 2023, 2:16pm UTC](https://community.sendbird.com/t/server-returns-500-after-token-refresh/8348/1 "2023-09-26T14:16:15Z")

</div>

**[Problem/Question]**  
We have a chat in a react app which uses and as described in your examples.

The provider uses a custom SessionHandler to provide the ability to refresh the token, but after some time (can’t really say how many minutes) when the `sessionHandler.onSessionTokenRequired` is called and generates a new token which is than `resolve(newToken)` then we’re seeing server 500 errors from the sdk to the sendbird server:

```auto
https://api-e91a7eaa-412b-43eb-a504-b2b621c6986e.sendbird.com/v3/users/MY_USER_ID/session_key
// Obviously MY_USER_ID is a real user ID.

```

 ![Screenshot 2023-09-26 at 17.11.11](https://us1.discourse-cdn.com/flex020/uploads/sendbird/original/2X/4/4db2f20640be8a2a905589fcfc34b9ebf9dd69d6.png)  
 ![Screenshot 2023-09-26 at 17.18.04](https://us1.discourse-cdn.com/flex020/uploads/sendbird/original/2X/f/fe1553fe02e852494392aa2cca46d5c559673d6b.png)  
 ![Screenshot 2023-09-26 at 17.17.42](https://us1.discourse-cdn.com/flex020/uploads/sendbird/original/2X/8/8b6bfdf43aeaf83c48b1b3b94f85bf05db847363.png)

In addition, the `Uncaught (in promise) SendbirdError: This session token is invalid.` seems to be coming from within the sendbird package and i couldn’t find how to catch this error…

Here’s how the provider looks like:

```auto

function ChatProvider({ children }) {
  const dispatch = useDispatch();
  const [token, setToken] = useState();
  const state = useSelector((state) => state.toJS());
  const userId = state.global?.portalUser;
  const [channels, setChannels] = useState();

  const authService = useMemo(() => {
    return AuthService.getInstance()
  }, []);

  const issueSessionToken = async () => {
    if (!authService?.hasValidToken()) {
      return new Error('No Valid Token');
    }

    return new RequestHelper()
      .sendRequest('GET', '/v2/chat/accessToken')
      .then((res) => {
        if (res.status === 'success' && res.data?.accessToken !== '') {
          const token = res.data;
          setToken(token);
          return token;
        } else {
          console.error(
            'something went wrong trying to get chat access token',
            res
          );
        }
      })
      .catch((err) => {
        console.error('Error trying to get chat access token', err);
        return err;
      });
  };

  useEffect(() => {
    issueSessionToken();
  }, []);

  useEffect(() => {
    if (!token || !authService?.hasValidToken() || channels) {
      return;
    }

    function getChannels() {
      new RequestHelper()
        .sendRequest('GET', '/v2/chat/channels')
        .then((res) => {
          setChannels(res.data.channels)
          dispatch({
            type: CHAT_SET_CHANNELS,
            payload: res.data.channels ?? [],
          });
        })
        .catch((err) => {
          console.error('Error trying to get channels for coach', err);
        });
    }
    getChannels();
  }, [token]);

  const configureSession = () => {
    const sessionHandler = new SessionHandler();
    sessionHandler.onSessionTokenRequired = async (resolve, reject) => {
      const newToken = await issueSessionToken();
      if(newToken instanceof Error){
        reject('Invalid token');
      } else if (newToken) {
        resolve(newToken);
      } else {
        reject('Invalid token');
      }
    };
    return sessionHandler;
  };

  const isValidSession = useMemo(() => {
    return token && userId && authService?.hasValidToken();
  }, [token, userId]);

  return isValidSession ? (
    <SendbirdProvider
      appId={config.sendbirdAppId}
      userId={userId}
      accessToken={token.accessToken}
      configureSession={configureSession}
      isVoiceMessageEnabled={true}
      isMessageReceiptStatusEnabledOnChannelList={true}
      isTypingIndicatorEnabledOnChannelList={true}
      colorSet={{
        '--sendbird-light-primary-500': '#286345',
        '--sendbird-light-primary-400': '#286345',
        '--sendbird-light-primary-300': '#286345',
        '--sendbird-light-primary-200': '#286345',
        '--sendbird-light-primary-100': '#286345',
      }}
      config={{
        logLevel:
          process.env.REACT_APP_ENV === 'production'
            ? ['error']
            : ['warning', 'error'],
      }}
    >
      {children}
      <SendbirdListener />
    </SendbirdProvider>
  ) : (
    <div className="chat-info-pane">
      <Spinner />
    </div>
  );
}

```

The Listener component looks like this:

```auto

const SendbirdListener = ({onReconnectionFailed}) => {
  const dispatch = useDispatch();
  const sendbirdGlobalStore = useSendbirdStateContext();
  const sdkInstance = sendbirdSelectors.getSdk(sendbirdGlobalStore);

  const updateUnreadCount = (channel) => {
    const { url, unreadMessageCount } = channel;
    dispatch({
      type: CHAT_UPDATE_CHANNELS_UNREAD_COUNT,
      payload: { channelId: url, unreadMessageCount },
    });
  };

  useEffect(() => {
    const id = generateUid();
    let channelHandlerConstructor, connectionHandler;

    if (sdkInstance?.groupChannel?.addGroupChannelHandler) {
      channelHandlerConstructor = {
        onChannelChanged: (channel) => {
          updateUnreadCount(channel);
        },
      };

      const channelHandler = new GroupChannelHandler(channelHandlerConstructor);
      sdkInstance.groupChannel.addGroupChannelHandler(id, channelHandler);
    }

    return () => {
      if (sdkInstance?.groupChannel?.removeChannelHandler) {
        sdkInstance.groupChannel.removeChannelHandler(id);
      }
    };
  }, [sdkInstance]);

  return <></>;
};

export default SendbirdListener;

```

The chat itself looks like this:

```auto
function Chat(){
  return <SBConversation
    channelUrl={currentChannelUrl}
    disableMarkAsRead={true}
    showSearchIcon={false}
    animatedMessage={true}
    renderChannelHeader={() => <div />}
    reconnectOnIdle={false}
  />
}

```

* * *

// If problem, please fill out the below. If question, please delete.  
**[UIKit Version]**  
“@sendbird/chat”: “^4.9.6”,  
“@sendbird/uikit-react”: “^3.6.5”,

**[Reproduction Steps]**  
// Please provide reproduction steps and, if possible, code snippets.

**[Frequency]**  
every time

**[Current impact]**  
high impact, expired sessions are crashing the app

---

<div class="post-metadata">

### Author: ![Tyler](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/tyler/32/810_2.png) [@Tyler](https://community.sendbird.com/u/Tyler)
#### Post date: [September 26, 2023, 9:23pm UTC](https://community.sendbird.com/t/server-returns-500-after-token-refresh/8348/2 "2023-09-26T21:23:10Z")

</div>

Hey @amit,

Can you submit this to our Support team via the Dashboard? We’ll take a look at what’s going on.

Thanks,  
Tyler

---

<div class="post-metadata">

### Author: ![amit](https://sea2.discourse-cdn.com/flex020/user_avatar/community.sendbird.com/amit/32/4245_2.png) [@amit](https://community.sendbird.com/u/amit)
#### Post date: [September 27, 2023, 7:20am UTC](https://community.sendbird.com/t/server-returns-500-after-token-refresh/8348/3 "2023-09-27T07:20:44Z")

</div>

Hey @Tyler, thanks for the prompt reply!

I think i found the cause for the 500 error, i sent the wrong payload `onSessionTokenRequired()` so that’s on me and sorry…  
However, we’re still getting the expired token error when we send the new one in, I checked our server and we get the token using `/v3/users/USER_ID`.

I’ve submitted a support request as well, thanks again!
