A link for TRPC that refreshes the token when it expires.
Install the package with your package manager of choice.
npm install trpc-refresh-token-link
Then use it in your code.
import { refreshTokenLink } from "trpc-refresh-token-link";
import { createTRPCClient } from "@trpc/client";
const api = createTRPCClient({
links: [
/* other links */
refreshTokenLink({
/* options */
}),
],
});
The refreshTokenLink
function accepts an object with the following properties:
shouldRefresh: (op: Operation) => boolean
: A function that determines whether the token should be refreshed for a given operation. To call the refresh token function, returntrue
.refreshToken: (op: Operation) => Promise<void>
: A function that refreshes the token and saves it to the client.