Skip to content

Commit ea31a62

Browse files
authored
Merge pull request #201 from kouts/docs/trpc-nuxt_instructions
docs: added trpc-nuxt configuration section
2 parents 630f909 + 4333304 commit ea31a62

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/content/1.getting-started/2.configuration.md

+34
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,37 @@ export default defineNuxtConfig({
183183
},
184184
});
185185
```
186+
187+
## Using with tRPC Nuxt
188+
189+
TRPC [uses `POST` requests](https://trpc.io/docs/rpc#methods---type-mapping) for the mutation endpoints. Using the CSRF protection functionality of this module with [trpc-nuxt](https://github.com/wobsoriano/trpc-nuxt) will help you protect your mutation endpoints from CSRF.
190+
191+
In order to make the [CSRF](/security/csrf) functionality of this module work with `trpc-nuxt`, we have to add the CSRF token value into a `csrf-token` header inside the outgoing request headers in our `trpc` client.
192+
193+
```ts
194+
import { createTRPCNuxtClient, httpBatchLink } from "trpc-nuxt/client";
195+
import type { AppRouter } from "@/server/trpc/routers";
196+
197+
export default defineNuxtPlugin(() => {
198+
const headers = useRequestHeaders();
199+
const { csrf } = useCsrf();
200+
201+
const client = createTRPCNuxtClient<AppRouter>({
202+
links: [
203+
httpBatchLink({
204+
// Headers are called on every request
205+
headers() {
206+
// Add CSRF token to request headers
207+
return { ...headers, "csrf-token": csrf };
208+
},
209+
}),
210+
],
211+
});
212+
213+
return {
214+
provide: {
215+
client,
216+
},
217+
};
218+
});
219+
```

0 commit comments

Comments
 (0)