feat: append csrf to formdata

This commit is contained in:
DIYgod 2024-05-19 21:08:37 +08:00
parent 655d1f899b
commit d0007d1b3d
No known key found for this signature in database
2 changed files with 6 additions and 5 deletions

View File

@ -56,7 +56,7 @@ export function FollowForm({ type }: { type: string }) {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
keyword: prefix,
keyword: prefix || "",
},
})
const mutation = useMutation({

View File

@ -3,9 +3,6 @@ import { getCsrfToken } from "@hono/auth-js/react"
export const apiFetch = ofetch.create({
baseURL: import.meta.env.VITE_API_URL,
headers: {
"Content-Type": "application/json",
},
credentials: "include",
onRequest: async ({ options }) => {
if (options.method && options.method.toLowerCase() !== "get") {
@ -13,7 +10,11 @@ export const apiFetch = ofetch.create({
if (!options.body) {
options.body = {}
}
;(options.body as Record<string, any>).csrfToken = csrfToken
if (options.body instanceof FormData) {
options.body.append("csrfToken", csrfToken)
} else {
;(options.body as Record<string, any>).csrfToken = csrfToken
}
}
},
})