Skip to content

[WIP] check if Onyx is ready when app is authenticating #5888

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 20 additions & 2 deletions src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ Onyx.connect({
callback: val => credentials = val,
});

let isOnyxReady = false;
let authToken;
Onyx.connect({
key: ONYXKEYS.SESSION,
callback: val => authToken = val ? val.authToken : null,
callback: (val) => {
authToken = val ? val.authToken : null;
isOnyxReady = true;
},
});

/**
Expand Down Expand Up @@ -80,7 +84,21 @@ function addDefaultValuesToParameters(command, parameters) {
}

// Tie into the network layer to add auth token to the parameters of all requests
Network.registerParameterEnhancer(addDefaultValuesToParameters);
Network.registerParameterEnhancer((command, parameters) => {
// - When the app is bootstrapping the auth token is first retrieved from Onyx (if exists),
// after that the auth token variable is re-assigned from the response of the function
// reauthenticate.
// - We wait until auth token is retrieved from Onyx (using isOnyxReady) because
// calling addDefaultValuesToParameters before can force a log out when the auth
// token is still valid (also we return nothing in this function if Onyx is not ready).
// - We want to force a log out when Onyx is ready and the auth token doesn't exist or is
// not valid.
if (isOnyxReady) {
return addDefaultValuesToParameters(command, parameters);
}

LogUtil.hmmm('Onyx is not ready in Network.registerParameterEnhancer ', {command, parameters});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not returning here means the network call is probably losing it's parameters

App/src/libs/Network.js

Lines 199 to 213 in 886aa24

const finalParameters = _.isFunction(enhanceParameters)
? enhanceParameters(queuedRequest.command, requestData)
: requestData;
// Check to see if the queue has paused again. It's possible that a call to enhanceParameters()
// has paused the queue and if this is the case we must return. We don't retry these requests
// since if a request is made without an authToken we sign out the user.
if (!canMakeRequest(queuedRequest)) {
return;
}
HttpUtils.xhr(queuedRequest.command, finalParameters, queuedRequest.type, queuedRequest.shouldUseSecure)
.then(response => onResponse(queuedRequest, response))
.catch(error => onError(queuedRequest, error));
});

If the idea is to acknowledge and still let the request to go through without the default values, we should at least return the parameters here

});

/**
* @throws {Error} If the "parameters" object has a null or undefined value for any of the given parameterNames
Expand Down