Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/authorization/authorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe('API Key Interceptors', () => {
});

beforeEach(() => {
jest.clearAllTimers();
setTypeOfAuthForTestingOnly('userID');

mockRequest.onPost('/users/update').reply(200, {
Expand Down Expand Up @@ -1190,6 +1191,7 @@ describe('User Identification', () => {

describe('refreshJwtToken', () => {
beforeEach(() => {
jest.clearAllTimers();
mockRequest.resetHistory();
});

Expand Down
22 changes: 17 additions & 5 deletions src/authorization/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ let authIdentifier: null | string = null;
let userInterceptor: number | null = null;
let authInterceptor: number | null = null;
let apiKey: null | string = null;
let generateJWTGlobal: any = null;
const unknownUserManager = new UnknownUserEventManager();

export interface GenerateJWTPayload {
email?: string;
userID?: string;
}

type GenerateJWTFunction = (payload: GenerateJWTPayload) => Promise<string>;
let generateJWTGlobal: GenerateJWTFunction | null = null;

export interface WithJWT {
setEmail: (
email: string,
Expand All @@ -61,6 +63,9 @@ export interface WithJWT {
logout: () => void;
refreshJwtToken: (authTypes: string) => Promise<string>;
clearRefresh: () => void;
setGenerateJWT: (
newGenerateJWT: (payload: GenerateJWTPayload) => Promise<string>
) => void;
setVisitorUsageTracked: (consent: boolean) => void;
clearVisitorEventsAndUserData: () => void;
}
Expand Down Expand Up @@ -395,7 +400,7 @@ export function initialize(
generateJWT?: (payload: GenerateJWTPayload) => Promise<string>
) {
apiKey = authToken;
generateJWTGlobal = generateJWT;
generateJWTGlobal = generateJWT ?? null;
const logLevel = config.getConfig('logLevel');
if (!generateJWT && IS_PRODUCTION) {
/* only let people use non-JWT mode if running the app locally */
Expand Down Expand Up @@ -706,7 +711,7 @@ export function initialize(
baseAxiosRequest.interceptors.response.eject(responseInterceptor);
}

return generateJWT(payload)
return (generateJWTGlobal as GenerateJWTFunction)(payload)
.then((token) => {
const authorizationToken = new AuthorizationToken();
authorizationToken.setToken(token);
Expand Down Expand Up @@ -768,7 +773,9 @@ export function initialize(
? { email: newEmail }
: { userID: authIdentifier ?? '' };

return generateJWT(payloadToPass).then((newToken) => {
return (generateJWTGlobal as GenerateJWTFunction)(
payloadToPass
).then((newToken) => {
const authorizationToken = new AuthorizationToken();
authorizationToken.setToken(newToken);

Expand Down Expand Up @@ -862,7 +869,7 @@ export function initialize(
key if the Iterable API told us the JWT is invalid.
*/
if (error?.response?.status === 401) {
return generateJWT(payload)
return (generateJWTGlobal as GenerateJWTFunction)(payload)
.then((newToken) => {
const authorizationToken = new AuthorizationToken();
authorizationToken.setToken(newToken);
Expand Down Expand Up @@ -1112,6 +1119,11 @@ export function initialize(
}
});
},
setGenerateJWT: (
newGenerateJWT: (payload: GenerateJWTPayload) => Promise<string>
) => {
generateJWTGlobal = newGenerateJWT;
},
setVisitorUsageTracked: (consent: boolean) => {
/* if consent is true, we want to clear unknown user data and start tracking */
if (consent) {
Expand Down
Loading