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
126 changes: 70 additions & 56 deletions src/libs/Authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,73 +150,87 @@ function reauthenticate(command = ''): Promise<boolean> {
partnerPassword,
partnerUserID: credentials.autoGeneratedLogin,
partnerUserSecret: credentials.autoGeneratedPassword,
}).then((response) => {
if (!response) {
return false;
}
})
.then((response) => {
if (!response) {
return false;
}

Log.hmmm('Reauthenticate - Processing authentication result', {
command,
});

if (response.jsonCode === CONST.JSON_CODE.UNABLE_TO_RETRY) {
// When a fetch() fails due to a network issue and an error is thrown we won't log the user out. Most likely they
// have a spotty connection and will need to retry reauthenticate when they come back online. Error so it can be handled by the retry mechanism.
const error = new Error('Unable to retry Authenticate request');
trackAuthenticationError(error, {
errorType: 'network_retry',
functionName: 'reauthenticate',
jsonCode: response.jsonCode,
Log.hmmm('Reauthenticate - Processing authentication result', {
command,
});
throw error;
}

// If authentication fails and we are online then log the user out
if (response.jsonCode !== 200) {
const errorMessage = getAuthenticateErrorMessage(response);
if (response.jsonCode === CONST.JSON_CODE.UNABLE_TO_RETRY) {
// When a fetch() fails due to a network issue and an error is thrown we won't log the user out. Most likely they
// have a spotty connection and will need to retry reauthenticate when they come back online. Error so it can be handled by the retry mechanism.
const error = new Error('Unable to retry Authenticate request');
trackAuthenticationError(error, {
errorType: 'network_retry',
functionName: 'reauthenticate',
jsonCode: response.jsonCode,
command,
});
throw error;
}

// If authentication fails and we are online then log the user out
if (response.jsonCode !== 200) {
const errorMessage = getAuthenticateErrorMessage(response);

trackAuthenticationError(new Error('Authentication failed'), {
errorType: 'auth_failure',
functionName: 'reauthenticate',
jsonCode: response.jsonCode,
command,
errorMessage,
});
setIsAuthenticating(false);
Log.hmmm('Redirecting to Sign In because we failed to reauthenticate', {
command,
error: errorMessage,
});
redirectToSignIn(errorMessage);
return false;
}

// If we reauthenticate due to an expired delegate token, restore the delegate's original account.
// This is because the credentials used to reauthenticate were for the delegate's original account, and not for the account they were connected as.
if (isConnectedAsDelegate({delegatedAccess: account?.delegatedAccess})) {
Log.info('Reauthenticate while connected as a delegate. Restoring original account.');
restoreDelegateSession(response);
return true;
}

// Update authToken in Onyx and in our local variables so that API requests will use the new authToken
updateSessionAuthTokens(response.authToken, response.encryptedAuthToken);

// Note: It is important to manually set the authToken that is in the store here since any requests that are hooked into
// reauthenticate .then() will immediate post and use the local authToken. Onyx updates subscribers lately so it is not
// enough to do the updateSessionAuthTokens() call above.
setAuthToken(response.authToken ?? null);

// The authentication process is finished so the network can be unpaused to continue processing requests
setIsAuthenticating(false);

trackAuthenticationError(new Error('Authentication failed'), {
errorType: 'auth_failure',
functionName: 'reauthenticate',
jsonCode: response.jsonCode,
Log.hmmm('Reauthenticate - Re-authentication successful', {
command,
errorMessage,
});
setIsAuthenticating(false);
Log.hmmm('Redirecting to Sign In because we failed to reauthenticate', {

return true;
})
.catch((error) => {
trackAuthenticationError(error as Error, {
errorType: 'unexpected_error',
functionName: 'reauthenticate',
command,
error: errorMessage,
});
redirectToSignIn(errorMessage);
return false;
}

// If we reauthenticate due to an expired delegate token, restore the delegate's original account.
// This is because the credentials used to reauthenticate were for the delegate's original account, and not for the account they were connected as.
if (isConnectedAsDelegate({delegatedAccess: account?.delegatedAccess})) {
Log.info('Reauthenticate while connected as a delegate. Restoring original account.');
restoreDelegateSession(response);
return true;
}

// Update authToken in Onyx and in our local variables so that API requests will use the new authToken
updateSessionAuthTokens(response.authToken, response.encryptedAuthToken);

// Note: It is important to manually set the authToken that is in the store here since any requests that are hooked into
// reauthenticate .then() will immediate post and use the local authToken. Onyx updates subscribers lately so it is not
// enough to do the updateSessionAuthTokens() call above.
setAuthToken(response.authToken ?? null);

// The authentication process is finished so the network can be unpaused to continue processing requests
setIsAuthenticating(false);

Log.hmmm('Reauthenticate - Re-authentication successful', {
command,
Log.alert('Reauthenticate - Unexpected error during authentication', {
error: (error as Error).message,
command,
});
throw error;
});

return true;
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/libs/telemetry/trackAuthenticationError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Sentry from '@sentry/react-native';
import CONST from '@src/CONST';

type AuthenticationFunction = 'Authenticate' | 'reauthenticate';
type AuthenticationErrorType = 'missing_params' | 'network_retry' | 'auth_failure';
type AuthenticationErrorType = 'missing_params' | 'network_retry' | 'auth_failure' | 'unexpected_error';

type AuthenticationErrorContext = {
errorType: AuthenticationErrorType;
Expand Down
Loading