Skip to content
Open
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
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