Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/llms-txt-hub / apps/web/lib/csrf-client.ts |
| Date | Not specified in source |
| Repository | Janady13/llms-txt-hub |
| Revision | e0f4bf978d4df168369c7601d9c5f5ea89e84377 |
| Branch | main |
| Path or archive member | apps/web/lib/csrf-client.ts |
| Original SHA-256 | c203029f54b6466a0ccb82db6271e41f2e231e3bfffc8172c58cd2577c883bd9 |
| Git blob SHA | 85772b669452efa9e61ae9dc600726f3aeee051d |
| Reading-copy SHA-256 | c203029f54b6466a0ccb82db6271e41f2e231e3bfffc8172c58cd2577c883bd9 |
Recovered source transcript
Download complete source text ↓38 lines · 0.8 KB
'use client'
const CSRF_HEADER_NAME = 'x-csrf-token'
/**
* Get CSRF token from meta tag for client-side usage
*/
export function getCSRFTokenForClient(): string {
if (typeof window === 'undefined') {
return ''
}
// Try to get from meta tag first
const metaTag = document.querySelector('meta[name="csrf-token"]')
if (metaTag) {
return metaTag.getAttribute('content') || ''
}
return ''
}
/**
* Add CSRF token to fetch requests automatically
* This is the client-side version that can be used in React components
*/
export function fetchWithCSRF(url: string, options: RequestInit = {}): Promise<Response> {
const token = getCSRFTokenForClient()
if (token && !['GET', 'HEAD'].includes(options.method || 'GET')) {
options.headers = {
...options.headers,
[CSRF_HEADER_NAME]: token
}
}
return fetch(url, options)
}