loadCurrentHubUser()

in src/components/auth/auth-base.ts [141:202]


  loadCurrentHubUser(authParams: AuthParams) {
    log.info('AuthBase(loadCurrentUser): Verifying token, loading current user...');
    return fetch(this.LOAD_USER_URL, {
      headers: {
        Accept: ACCEPT_HEADER,
        'Hub-API-Version': '2',
        ...this.getAuthorizationHeaders(authParams),
      },
    })
      .then((res: Response) => {
        if (res.status > 400) {
          log.log(
            `AuthBase(loadCurrentUser):Error: ${res.status}. Verifying token...`,
            res,
          );
          throw res;
        }

        log.info('AuthBase(loadCurrentUser): Current user loaded.');
        return res.json();
      })
      .then((hubUser: UserHub) => {
        if (hubUser.banned) {
          throw new Error(ERROR_MESSAGE_DATA.USER_BANNED.title);
        }

        this.setCurrentUser(hubUser);
        log.info('AuthBase(loadCurrentUser): Current user updated.');
        return authParams;
      })
      .catch(async (error: CustomError) => {
        const prevToken: string | undefined = this.getRefreshToken(authParams);

        if (!prevToken) {
          log.warn('AuthBase(loadCurrentUser):Error: Previous token is undefined.');
        }

        if (error.status === 401 && prevToken) {
          log.log('AuthBase(loadCurrentUser):Unauthorised: Refreshing token...', error?.message);
          try {
            await this.refreshToken();
          } catch (e) {
            const err = e as AnyError;
            let errorMsg = err?.message != null ? err.message : '';
            if (errorMsg.indexOf('The owner of the refresh token is banned') !== -1) {
              errorMsg = ERROR_MESSAGE_DATA.USER_BANNED.title;
            }
            this.onTokenRefreshError(errorMsg);
          }
          return;
        }

        throw error;
      })
      .catch((err: CustomError) => {
        log.log(
          `AuthBase(loadCurrentUser):Error: Token refresh failed. ${err.status}`,
          err,
        );
        throw err;
      });
  }