connectrpc / connect-query-es

TypeScript-first expansion pack for TanStack Query that gives you Protobuf superpowers.
https://connectrpc.com/docs/web/query/getting-started
Apache License 2.0
223 stars 14 forks source link

Interceptor Auth #397

Open amjadfqs opened 1 month ago

amjadfqs commented 1 month ago

Is there any example for interceptor auth that refresh the token whenever it expires.

Thanks in advance.

timostamm commented 1 week ago

We don't have an example, but here's a basic interceptor:

import type { Interceptor } from "@connectrpc/connect";

function interceptor(): Interceptor {
  let token = "abc";
  return (next) => async (req) => {
    req.header.set("token", token);
    return await next(req);
  }
}

You can add arguments to configure the interceptor, hold state in the function body, and perform a refresh in the returned async function.