• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thoughtspot / visual-embed-sdk / #2434

16 Sep 2025 11:57AM UTC coverage: 94.033% (-0.05%) from 94.081%
#2434

push

web-flow
SCAL-270176 : don't pre render if already present on screen (#300)

1226 of 1387 branches covered (88.39%)

Branch coverage included in aggregate %.

3 of 4 new or added lines in 2 files covered. (75.0%)

4 existing lines in 1 file now uncovered.

2966 of 3071 relevant lines covered (96.58%)

84.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.06
/src/authToken.ts
1
import { ERROR_MESSAGE } from './errors';
26✔
2
import { EmbedConfig } from './types';
3
import { getValueFromWindow, storeValueInWindow } from './utils';
26✔
4
import { fetchAuthTokenService, verifyTokenService } from './utils/authService/authService';
26✔
5
import { logger } from './utils/logger';
26✔
6

7
const cacheAuthTokenKey = 'cachedAuthToken';
26✔
8

9
const getCacheAuthToken = (): string | null => getValueFromWindow(cacheAuthTokenKey);
46✔
10
const storeAuthTokenInCache = (token: string): void => {
26✔
11
    storeValueInWindow(cacheAuthTokenKey, token);
296✔
12
};
13

14
// This method can be used to get the authToken using the embedConfig
15
/**
16
 *
17
 * @param embedConfig
18
 */
19
export async function getAuthenticationToken(embedConfig: EmbedConfig): Promise<string> {
26✔
20
    const cachedAuthToken = getCacheAuthToken();
26✔
21
    // Since we don't have token validation enabled , we cannot tell if the
22
    // cached token is valid or not. So we will always fetch a new token.
23
    if (cachedAuthToken && !embedConfig.disableTokenVerification) {
26✔
24
        let isCachedTokenStillValid;
25
        try {
3✔
26
            isCachedTokenStillValid = await validateAuthToken(embedConfig, cachedAuthToken, true);
3✔
27
        } catch {
28
            isCachedTokenStillValid = false;
1✔
29
        }
30

31
        if (isCachedTokenStillValid) return cachedAuthToken;
3✔
32
    }
33

34
    const { authEndpoint, getAuthToken } = embedConfig;
24✔
35

36
    let authToken = null;
24✔
37
    if (getAuthToken) {
24✔
38
        authToken = await getAuthToken();
22✔
39
    } else {
15!
UNCOV
40
        const response = await fetchAuthTokenService(authEndpoint);
×
UNCOV
41
        authToken = await response.text();
×
UNCOV
42
    }
×
43

44
    try {
45
        // this will throw error if the token is not valid
2✔
46
        await validateAuthToken(embedConfig, authToken);
2✔
47
    } catch (e) {
48
        logger.error(`${ERROR_MESSAGE.INVALID_TOKEN_ERROR} Error : ${e.message}`);
49
        throw e;
17✔
50
    }
51

17✔
52
    storeAuthTokenInCache(authToken);
53
    return authToken;
2✔
54
}
2✔
55

56
const validateAuthToken = async (
57
    embedConfig: EmbedConfig,
15✔
58
    authToken: string,
15✔
59
    suppressAlert?: boolean,
60
): Promise<boolean> => {
61
    const cachedAuthToken = getCacheAuthToken();
26✔
62
    if (embedConfig.disableTokenVerification) {
63
        logger.info('Token verification is disabled. Assuming token is valid.');
64
        return true;
65
    }
66
    try {
20✔
67
        const isTokenValid = await verifyTokenService(embedConfig.thoughtSpotHost, authToken);
20✔
68
        if (isTokenValid) return true;
1✔
69
    } catch {
1✔
70
        return false;
71
    }
19✔
72

19✔
73
    if (cachedAuthToken && cachedAuthToken === authToken) {
19✔
74
        if (!embedConfig.suppressErrorAlerts && !suppressAlert) {
UNCOV
75
            // eslint-disable-next-line no-alert
×
76
            alert(ERROR_MESSAGE.DUPLICATE_TOKEN_ERR);
77
        }
78
        throw new Error(ERROR_MESSAGE.DUPLICATE_TOKEN_ERR);
3✔
79
    } else {
2✔
80
        throw new Error(ERROR_MESSAGE.INVALID_TOKEN_ERROR);
81
    }
1✔
82
};
83

2✔
84
/**
85
 * Resets the auth token and a new token will be fetched on the next request.
1✔
86
 * @example
87
 * ```js
88
 * resetCachedAuthToken();
89
 * ```
90
 * @version SDK: 1.28.0 | ThoughtSpot: *
91
 * @group Authentication / Init
92
 */
93
export const resetCachedAuthToken = (): void => {
94
    storeAuthTokenInCache(null);
95
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc