https://supertokens.com/ logo
doesSessionExist is undefined error
a

Axel Jönsson

05/22/2023, 9:48 AM
Hi! i'm actually tryin to connect to Hannes Colt's backend by updating the front end sdk to latest, and grabbing the "Supertokens.getAccessToken()" instaed of the old "doesSessionExist()" or "attemptRefreshSession()". i'm getting a weird error that"doesSessionExist is undefined" when i'm running the "getAccessToken()", something is wrong in the setup but i can't really find it :/
r

rp

05/22/2023, 9:53 AM
hey @Axel Jönsson
a

Axel Jönsson

05/22/2023, 9:53 AM
hi!
r

rp

05/22/2023, 9:53 AM
can you share the error stack trace?
also, share your code along with the import statement
a

Axel Jönsson

05/22/2023, 9:53 AM
it's very small i'm affraid: " ERROR [TypeError: Cannot read property 'doesSessionExist' of undefined]"
r

rp

05/22/2023, 9:53 AM
i see.
a

Axel Jönsson

05/22/2023, 9:54 AM
this is the hook:
import {useCallback} from 'react';
import SuperTokens from 'supertokens-react-native';

interface InitUserSession {
  session: boolean;
  userId: string;
}

export default function useGetSuperTokensDataHook() {
  const _onError = useCallback((err: unknown) => {
    console.error(err);
    throw new Error('Error in initUserSession');
  }, []);

  const getSuperTokensData = useCallback(async (): Promise<
    InitUserSession | undefined
> => {
    try {
      const data = await SuperTokens?.getAccessToken();
      console.log('accesstoken--->', data);
      //Get the data we require from supertokens
      const session = await SuperTokens.attemptRefreshingSession();
      const userId = await SuperTokens.getUserId();

      if (!session) {
        throw new Error('no session');
      }
      if (!userId) {
        throw new Error('no connected user');
      }

      //return successfull promise
      return {session, userId};
    } catch (err) {
      _onError(err);
    }
  }, [_onError]);

  return {
    getSuperTokensData,
  };
}
r

rp

05/22/2023, 9:55 AM
@nkshah2 can help here
a

Axel Jönsson

05/22/2023, 9:55 AM
and the init function if you need it:
import {BACKEND_URL, SUPERTOKENS_COOKIE_DOMAIN} from '@lib/api.config';
import SuperTokens from 'supertokens-react-native';

export function initSuperTokens() {
  SuperTokens.init({
    apiDomain: BACKEND_URL,
    sessionTokenBackendDomain: SUPERTOKENS_COOKIE_DOMAIN,
  });
}
n

nkshah2

05/22/2023, 10:21 AM
Hey @Axel Jönsson Can you check if the supertokens init call happens before you try to use getAccessToken? (Just add logs before the two to make sure as well)
a

Axel Jönsson

05/22/2023, 10:29 AM
yeah, it definatly happens before
n

nkshah2

05/22/2023, 10:31 AM
Will be easier to get this debugged on call, you can join here: https://meet.google.com/bem-giwe-cbp
Just to update, the issue was that the init call was inside a useEffect but one of the children needed access to getAccessToken. Because the init call would happen after the component rendered (since useEffect triggers after mounting) it was complaining about something internal being undefined which is expected