Skip to content

Commit

Permalink
fix: test CSRF_HEADER_NAME
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Dec 18, 2024
1 parent b8c28f7 commit aa96c09
Showing 1 changed file with 65 additions and 1 deletion.
66 changes: 65 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,68 @@
import { createApi } from "codeforlife/api"
import {
createApi as _createApi,
fetchBaseQuery,
} from "@reduxjs/toolkit/query/react"

import { SERVICE_API_URL } from "codeforlife/settings"
import { buildLogoutEndpoint } from "codeforlife/api/endpoints/session"
import defaultTagTypes from "codeforlife/api/tagTypes"
import { getCsrfCookie } from "codeforlife/utils/auth"

function createApi<TagTypes extends string = never>({
tagTypes = [],
}: {
tagTypes?: readonly TagTypes[]
} = {}) {
const fetch = fetchBaseQuery({
baseUrl: `${SERVICE_API_URL}/`,
credentials: "include",
prepareHeaders: (headers, { type }) => {
if (type === "mutation") {
const csrfToken = getCsrfCookie()
if (csrfToken) headers.set("csrftoken", csrfToken)
}

return headers
},
})

const api = _createApi({
// https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#implementing-a-custom-basequery
baseQuery: async (args, api, extraOptions) => {
if (api.type === "mutation" && getCsrfCookie() === undefined) {
// Get the CSRF token.
const { error } = await fetch(
{ url: "/csrf/cookie", method: "GET" },
api,
{},
)

// Validate we got the CSRF token.
if (error !== undefined) {
console.error(error)
// TODO
// window.location.href = `${PORTAL_BASE_URL}/error/500`
}
if (getCsrfCookie() === undefined) {
// TODO
// window.location.href = `${PORTAL_BASE_URL}/error/500`
}
}

// Send the HTTP request and fetch the response.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return await fetch(args, api, extraOptions)
},
tagTypes: [...defaultTagTypes, ...tagTypes],
endpoints: () => ({}),
})

return api.injectEndpoints({
endpoints: build => ({
logout: buildLogoutEndpoint<null, null>(api, build),
}),
})
}

const api = createApi({
tagTypes: ["Contributor", "AgreementSignature"],
Expand Down

0 comments on commit aa96c09

Please sign in to comment.