![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
@urql/exchange-auth
Advanced tools
Readme
An exchange for managing authentication in urql
@urql/exchange-auth
is an exchange for the urql
GraphQL client which helps handle auth headers and token refresh
First install @urql/exchange-auth
alongside urql
:
yarn add @urql/exchange-auth
# or
npm install --save @urql/exchange-auth
You'll then need to add the authExchange
, that this package exposes to your urql
Client
import { createClient, cacheExchange, fetchExchange } from 'urql';
import { makeOperation } from '@urql/core';
import { authExchange } from '@urql/exchange-auth';
const client = createClient({
url: 'http://localhost:1234/graphql',
exchanges: [
cacheExchange,
authExchange(async utils => {
// called on initial launch,
// fetch the auth state from storage (local storage, async storage etc)
let token = localStorage.getItem('token');
let refreshToken = localStorage.getItem('refreshToken');
return {
addAuthToOperation(operation) {
if (token) {
return utils.appendHeaders(operation, {
Authorization: `Bearer ${token}`,
});
}
return operation;
},
willAuthError(_operation) {
// e.g. check for expiration, existence of auth etc
return !token;
},
didAuthError(error, _operation) {
// check if the error was an auth error
// this can be implemented in various ways, e.g. 401 or a special error code
return error.graphQLErrors.some(e => e.extensions?.code === 'FORBIDDEN');
},
async refreshAuth() {
// called when auth error has occurred
// we should refresh the token with a GraphQL mutation or a fetch call,
// depending on what the API supports
const result = await mutate(refreshMutation, {
token: authState?.refreshToken,
});
if (result.data?.refreshLogin) {
// save the new tokens in storage for next restart
token = result.data.refreshLogin.token;
refreshToken = result.data.refreshLogin.refreshToken;
localStorage.setItem('token', token);
localStorage.setItem('refreshToken', refreshToken);
} else {
// otherwise, if refresh fails, log clear storage and log out
localStorage.clear();
logout();
}
},
};
}),
fetchExchange,
],
});
Handling the logout logic in refreshAuth
is the easiest way to get started,
but it means the errors will always get swallowed by the authExchange
.
If you want to handle errors globally, this can be done using the mapExchange
:
import { mapExchange } from 'urql';
// this needs to be placed ABOVE the authExchange in the exchanges array, otherwise the auth error
// will show up hear before the auth exchange has had the chance to handle it
mapExchange({
onError(error) {
// we only get an auth error here when the auth exchange had attempted to refresh auth and
// getting an auth error again for the second time
const isAuthError = error.graphQLErrors.some(
e => e.extensions?.code === 'FORBIDDEN',
);
if (isAuthError) {
// clear storage, log the user out etc
}
}
}),
FAQs
An exchange for managing authentication and token refresh in urql
We found that @urql/exchange-auth demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 19 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.