Skip to content

Commit

Permalink
refactor(auth): Implement new initializer function API (#3012)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Mar 8, 2023
1 parent 2c96edd commit 4133d91
Show file tree
Hide file tree
Showing 7 changed files with 737 additions and 586 deletions.
38 changes: 38 additions & 0 deletions .changeset/perfect-lobsters-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
'@urql/exchange-auth': major
---

Implement new `authExchange` API, which removes the need for an `authState` (i.e. an internal authentication state) and removes `getAuth`, replacing it with a separate `refreshAuth` flow.

The new API requires you to now pass an initializer function. This function receives a `utils`
object with `utils.mutate` and `utils.appendHeaders` utility methods.
It must return the configuration object, wrapped in a promise, and this configuration is similar to
what we had before, if you're migrating to this. Its `refreshAuth` method is now only called after
authentication errors occur and not on initialization. Instead, it's now recommended that you write
your initialization logic in-line.

```js
authExchange(async utils => {
let token = localStorage.getItem('token');
let refreshToken = localStorage.getItem('refreshToken');
return {
addAuthToOperation(operation) {
return utils.appendHeaders(operation, {
Authorization: `Bearer ${token}`,
});
},
didAuthError(error) {
return error.graphQLErrors.some(e => e.extensions?.code === 'FORBIDDEN');
},
async refreshAuth() {
const result = await utils.mutate(REFRESH, { token });
if (result.data?.refreshLogin) {
token = result.data.refreshLogin.token;
refreshToken = result.data.refreshLogin.refreshToken;
localStorage.setItem('token', token);
localStorage.setItem('refreshToken', refreshToken);
}
},
};
});
```
Loading

0 comments on commit 4133d91

Please sign in to comment.