Files
dodo/frontend/src/lib/csrf.ts
T
2026-09-05 17:33:30 +08:00

14 lines
517 B
TypeScript

export function csrfHeader(method?: string) {
if (method && !['GET', 'HEAD', 'OPTIONS'].includes(method.toUpperCase())) {
const token = getCookie('dodo_csrf')
if (token) return { 'x-csrf-token': token }
}
return {}
}
export function getCookie(name: string) {
if (typeof document === 'undefined') return ''
const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
const match = document.cookie.match(new RegExp(`(?:^|; )${escaped}=([^;]*)`))
return match ? decodeURIComponent(match[1]) : ''
}