-
-
Notifications
You must be signed in to change notification settings - Fork 457
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
refactor(auth): Implement new initializer function API #3012
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kitten
changed the title
Feat/init auth refactor
feat(auth): Implement new initializer function-based API
Mar 7, 2023
kitten
changed the title
feat(auth): Implement new initializer function-based API
refactor(auth): Implement new initializer function-based API
Mar 7, 2023
Here's a small guide on how to map things from the "old" API to this "new" API, as proposed. Legacy APIauthExchange({
addAuthToOperation({ authState, operation }) {
if (!authState || !authState.token) {
return operation; // DONT_ADD_AUTH
}
return operation; // ADD_AUTH
},
willAuthError({ authState, operation }) {
return false; // WILL_AUTH_ERROR
},
didAuthError({ error }) {
return false; // DID_AUTH_ERROR
},
async getAuth ({ authState, mutate }) {
if (!authState) {
// INITIALIZE_AUTH
const token = localStorage.getItem('token');
const refreshToken = localStorage.getItem('refreshToken');
return token ? { token, refreshToken } : null;
}
// REFRESH_AUTH
const result = await mutate(refreshMutation, { token: authState?.refreshToken });
if (result.data?.refreshLogin) {
localStorage.setItem('token', result.data.refreshLogin.token);
localStorage.setItem('refreshToken', result.data.refreshLogin.refreshToken);
return {
token: result.data.refreshLogin.token,
refreshToken: result.data.refreshLogin.refreshToken,
};
}
// BAIL_REFRESH_AUTH
localStorage.clear();
logout();
return null;
},
}); Proposed APIauthExchange(async (utils) => {
// INITIALIZE_AUTH
let token = localStorage.getItem('token');
let refreshToken = localStorage.getItem('refreshToken');
return {
addAuthToOperation(operation) {
if (!token) {
return operation; // DONT_ADD_AUTH
}
// ADD_AUTH
return utils.appendHeaders(operation, {
Authorization: `Bearer ${token}`,
});
},
willAuthError(_operation) {
return false; // WILL_AUTH_ERROR
},
didAuthError(error, _operation) {
return false; // DID_AUTH_ERROR
},
async refreshAuth() {
// REFRESH_AUTH
const result = await mutate(refreshMutation, { token: authState?.refreshToken });
if (result.data?.refreshLogin) {
token = result.data.refreshLogin.token;
refreshToken = result.data.refreshLogin.refreshToken;
localStorage.setItem('token', token);
localStorage.setItem('refreshToken', refreshToken);
} else {
// BAIL_REFRESH_AUTH
localStorage.clear();
logout();
}
},
};
}); |
kitten
force-pushed
the
feat/init-auth-refactor
branch
from
March 7, 2023 17:13
29b08b2
to
8e8ffa5
Compare
kitten
changed the title
refactor(auth): Implement new initializer function-based API
refactor(auth): Implement new initializer function API
Mar 7, 2023
JoviDeCroock
approved these changes
Mar 8, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a huge improvement
kitten
force-pushed
the
feat/init-auth-refactor
branch
from
March 8, 2023 17:12
cb96b85
to
02fb651
Compare
kadikraman
approved these changes
Mar 8, 2023
This was referenced Mar 8, 2023
Closed
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #2815
Summary
This is a breaking change, which updates the
authExchange
API.This PR eliminates the need for an
authState
(i.e. the authentication state) to be kept with theauthExchange
, and moves it into the responsibility of the user.It also eliminates
getAuth
's "double duty" of being called, both, to initialize the authentication state, and to refresh the authentication state after an authentication error.The
authExchange
now accepts an initializer function, which is an async function (or a function returning a promise). This function is user-defined and will receive a utilities object and must return an authentication configuration object.This configuration object is unchanged but now contains
refreshAuth
rather thangetAuth
. Several arguments that its functions accept have been simplified.As we can see, the initializer function itself now initializes the authentication state, and the state is just kept in local variables (which also now makes it easier to keep the state in some "third object" outside of the
authExchange
without any hacky code)The function returns the configuration and can use
utils.mutate
to send GraphQL mutations for authentication purposes, and now also hasutils.appendHeaders
to add headers to operations!Set of changes
addAuthToOperation
,willAuthError
, anddidAuthError
argumentsauthState
andgetAuth
and replace with initializergetAuth
flow withrefreshAuth
calld.ts
output)NOTE: This isn't super final yet, but the goal here is to remove the API docs on the website and move those over to TSDocs. Hence, the website docs for the
authExchange
API are very lean now.