Skip to content
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [Unreleased]
### Fixes
- Pass `preferUserId` through `setUserID` and JWT `setEmail` into the identify-time `/users/update` call (`tryUser`), so callers can opt out of user creation when identifying a user (SDK-563).

## [2.2.2]
### Fixes
Expand Down
75 changes: 75 additions & 0 deletions src/authorization/authorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ describe('API Key Interceptors', () => {
});

describe('User Identification', () => {
const getUsersUpdatePayloads = () =>
mockRequest.history.post
.filter((e: any) => !!e.url?.match(/users\/update/gim))
.map((e: any) => JSON.parse(e.data));

beforeEach(() => {
setTypeOfAuthForTestingOnly('userID');

Expand Down Expand Up @@ -807,6 +812,26 @@ describe('User Identification', () => {
expect(response.config.params.email).toBeUndefined();
expect(response.config.params.userId).toBe('999');
});

it('defaults preferUserId to true on the identify-time users/update call', async () => {
mockRequest.onPost('/users/update').reply(200, {});
const { setUserID } = initialize('123');
await setUserID('999');

const payloads = getUsersUpdatePayloads();
expect(payloads.length).toBeGreaterThan(0);
expect(payloads[0].preferUserId).toBe(true);
});

it('passes preferUserId false through tryUser to users/update', async () => {
mockRequest.onPost('/users/update').reply(200, {});
const { setUserID } = initialize('123');
await setUserID('999', undefined, false);

const payloads = getUsersUpdatePayloads();
expect(payloads.length).toBeGreaterThan(0);
expect(payloads[0].preferUserId).toBe(false);
});
});
});

Expand Down Expand Up @@ -1011,6 +1036,32 @@ describe('User Identification', () => {
expect(response.config.params.userId).toBeUndefined();
expect(response.config.params.email).toBe('hello@gmail.com');
});

it('defaults preferUserId to true on the identify-time users/update call', async () => {
mockRequest.resetHistory();
mockRequest.onPost('/users/update').reply(200, {});
const { setEmail } = initialize('123', () =>
Promise.resolve(MOCK_JWT_KEY)
);
await setEmail('hello@gmail.com');

const payloads = getUsersUpdatePayloads();
expect(payloads.length).toBeGreaterThan(0);
expect(payloads[0].preferUserId).toBe(true);
});

it('passes preferUserId false through tryUser to users/update', async () => {
mockRequest.resetHistory();
mockRequest.onPost('/users/update').reply(200, {});
const { setEmail } = initialize('123', () =>
Promise.resolve(MOCK_JWT_KEY)
);
await setEmail('hello@gmail.com', undefined, false);

const payloads = getUsersUpdatePayloads();
expect(payloads.length).toBeGreaterThan(0);
expect(payloads[0].preferUserId).toBe(false);
});
});

describe('setUserID', () => {
Expand Down Expand Up @@ -1186,6 +1237,30 @@ describe('User Identification', () => {
).toBe(4);
}
});

it('defaults preferUserId to true on the identify-time users/update call', async () => {
mockRequest.onPost('/users/update').reply(200, {});
const { setUserID } = initialize('123', () =>
Promise.resolve(MOCK_JWT_KEY)
);
await setUserID('999');

const payloads = getUsersUpdatePayloads();
expect(payloads.length).toBeGreaterThan(0);
expect(payloads[0].preferUserId).toBe(true);
});

it('passes preferUserId false through tryUser to users/update', async () => {
mockRequest.onPost('/users/update').reply(200, {});
const { setUserID } = initialize('123', () =>
Promise.resolve(MOCK_JWT_KEY)
);
await setUserID('999', undefined, false);

const payloads = getUsersUpdatePayloads();
expect(payloads.length).toBeGreaterThan(0);
expect(payloads[0].preferUserId).toBe(false);
});
});

describe('refreshJwtToken', () => {
Expand Down
28 changes: 17 additions & 11 deletions src/authorization/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ export interface GenerateJWTPayload {
export interface WithJWT {
setEmail: (
email: string,
identityResolution?: IdentityResolution
identityResolution?: IdentityResolution,
preferUserId?: boolean
) => Promise<string>;
setUserID: (
userId: string,
identityResolution?: IdentityResolution
identityResolution?: IdentityResolution,
preferUserId?: boolean
) => Promise<string>;
logout: () => void;
refreshJwtToken: (authTypes: string) => Promise<string>;
Expand All @@ -72,7 +74,8 @@ export interface WithoutJWT {
) => Promise<string>;
setUserID: (
userId: string,
identityResolution?: IdentityResolution
identityResolution?: IdentityResolution,
preferUserId?: boolean
) => Promise<string>;
logout: () => void;
setNewAuthToken: (newToken?: string) => void;
Expand Down Expand Up @@ -464,12 +467,12 @@ export function initialize(

const handleTokenExpiration = createTokenExpirationTimer();

const tryUser = () => {
const tryUser = (preferUserId?: boolean) => {
let createUserAttempts = 0;

return async function tryUserNTimes(): Promise<any> {
try {
return await updateUser({});
return await updateUser({ preferUserId });
} catch (e) {
if (createUserAttempts < RETRY_USER_ATTEMPTS) {
createUserAttempts += 1;
Expand Down Expand Up @@ -587,7 +590,8 @@ export function initialize(
},
setUserID: async (
userId: string,
identityResolution?: IdentityResolution
identityResolution?: IdentityResolution,
preferUserId?: boolean
) => {
clearMessages();
try {
Expand All @@ -596,7 +600,7 @@ export function initialize(

// Initialize user authentication first, then create user profile
initializeUserId(userId);
await tryUser()();
await tryUser(preferUserId)();

const result = await tryMergeUser(userId, false, merge);
if (result.success) {
Expand Down Expand Up @@ -963,7 +967,8 @@ export function initialize(
},
setEmail: async (
email: string,
identityResolution?: IdentityResolution
identityResolution?: IdentityResolution,
preferUserId?: boolean
) => {
/* clear previous user */
clearMessages();
Expand All @@ -978,7 +983,7 @@ export function initialize(
initializeEmailUser(email);

// Create user profile first before attempting merge
await tryUser()();
await tryUser(preferUserId)();

const result = await tryMergeUser(email, true, merge);
if (result.success) {
Expand Down Expand Up @@ -1014,7 +1019,8 @@ export function initialize(
},
setUserID: async (
userId: string,
identityResolution?: IdentityResolution
identityResolution?: IdentityResolution,
preferUserId?: boolean
) => {
clearMessages();
try {
Expand All @@ -1028,7 +1034,7 @@ export function initialize(
initializeUserId(userId);

// Create user profile after authentication is set up
await tryUser()();
await tryUser(preferUserId)();

const result = await tryMergeUser(userId, false, merge);
if (result.success) {
Expand Down
Loading