Skip to content

Commit

Permalink
Merge pull request #5438 from Expensify/marcaaron-useCreateLoginToken
Browse files Browse the repository at this point in the history
Use CreateLogin authToken instead of relying on reauthenticate
  • Loading branch information
HorusGoul authored Oct 25, 2021
2 parents 628306a + 01dbb0d commit 59f8a3a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ function reauthenticate(command = '') {
partnerPassword: CONFIG.EXPENSIFY.PARTNER_PASSWORD,
partnerUserID: credentials.autoGeneratedLogin,
partnerUserSecret: credentials.autoGeneratedPassword,
authToken,
})
.then((response) => {
// If authentication fails throw so that we hit
Expand Down
19 changes: 8 additions & 11 deletions src/libs/actions/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,10 @@ function fetchAccountDetails(login) {
* re-authenticating after an authToken expires.
*
* @param {String} authToken
* @param {String} encryptedAuthToken – Not required for the CreateLogin API call, but passed to setSuccessfulSignInData
* @param {String} email
* @return {Promise}
*/
function createTemporaryLogin(authToken, encryptedAuthToken, email) {
function createTemporaryLogin(authToken, email) {
const autoGeneratedLogin = Str.guid('expensify.cash-');
const autoGeneratedPassword = Str.guid();

Expand All @@ -182,13 +181,14 @@ function createTemporaryLogin(authToken, encryptedAuthToken, email) {
doNotRetry: true,
forceNetworkRequest: true,
email,
includeEncryptedAuthToken: true,
})
.then((createLoginResponse) => {
if (createLoginResponse.jsonCode !== 200) {
throw new Error(createLoginResponse.message);
}

setSuccessfulSignInData({...createLoginResponse, encryptedAuthToken});
setSuccessfulSignInData(createLoginResponse);

// If we have an old generated login for some reason
// we should delete it before storing the new details
Expand Down Expand Up @@ -237,9 +237,8 @@ function signIn(password, twoFactorAuthCode) {
twoFactorAuthCode,
email: credentials.login,
})
.then((authenticateResponse) => {
const {authToken, encryptedAuthToken, email} = authenticateResponse;
createTemporaryLogin(authToken, encryptedAuthToken, email);
.then(({authToken, email}) => {
createTemporaryLogin(authToken, email);
})
.catch((error) => {
Onyx.merge(ONYXKEYS.ACCOUNT, {error: translateLocal(error.message), loading: false});
Expand All @@ -252,14 +251,12 @@ function signIn(password, twoFactorAuthCode) {
* @param {String} accountID
* @param {String} email
* @param {String} shortLivedToken
* @param {string} encryptedAuthToken
*/
function signInWithShortLivedToken(accountID, email, shortLivedToken, encryptedAuthToken) {
function signInWithShortLivedToken(accountID, email, shortLivedToken) {
Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, loading: true});

createTemporaryLogin(shortLivedToken, encryptedAuthToken, email).then((response) => {
createTemporaryLogin(shortLivedToken, email).then((response) => {
Onyx.merge(ONYXKEYS.SESSION, {
authToken: shortLivedToken,
accountID,
email,
});
Expand Down Expand Up @@ -305,7 +302,7 @@ function setPassword(password, validateCode, accountID) {
})
.then((response) => {
if (response.jsonCode === 200) {
createTemporaryLogin(response.authToken, response.encryptedAuthToken, response.email);
createTemporaryLogin(response.authToken, response.email);
return;
}

Expand Down
3 changes: 1 addition & 2 deletions src/pages/LogInWithShortLivedTokenPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ class LogInWithShortLivedTokenPage extends Component {
const accountID = parseInt(lodashGet(this.props.route.params, 'accountID', ''), 10);
const email = lodashGet(this.props.route.params, 'email', '');
const shortLivedToken = lodashGet(this.props.route.params, 'shortLivedToken', '');
const encryptedAuthToken = lodashGet(this.props.route.params, 'encryptedAuthToken', '');

// If the user is revisiting the component authenticated with the right account, we don't need to do anything, the componentWillUpdate when betas are loaded and redirect
if (this.props.session.authToken && email === this.props.session.email) {
return;
}

signInWithShortLivedToken(accountID, email, shortLivedToken, encryptedAuthToken);
signInWithShortLivedToken(accountID, email, shortLivedToken);
}

componentDidUpdate() {
Expand Down

0 comments on commit 59f8a3a

Please sign in to comment.