Skip to content

Commit 83c1052

Browse files
committed
feat: check for xsrf token cookie presence
1 parent 651119f commit 83c1052

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/orion.ts

+30-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,36 @@ export class Orion {
106106
);
107107
}
108108

109-
await Orion.makeHttpClient()
110-
.getAxios()
111-
.get(`sanctum/csrf-cookie`, { baseURL: Orion.getHost() });
109+
const httpClient = Orion.makeHttpClient();
110+
let response = null;
111+
112+
try {
113+
response = await httpClient
114+
.getAxios()
115+
.get(`sanctum/csrf-cookie`, { baseURL: Orion.getHost() });
116+
} catch (error) {
117+
throw new Error(
118+
`Unable to retrieve XSRF token cookie due to network error. Please ensure that SANCTUM_STATEFUL_DOMAINS and SESSION_DOMAIN environment variables are configured correctly on the API side.`
119+
);
120+
}
121+
122+
const xsrfTokenPresent =
123+
document.cookie
124+
.split(';')
125+
.filter((cookie: string) =>
126+
cookie.includes(httpClient.getAxios().defaults.xsrfCookieName || 'XSRF-TOKEN')
127+
).length > 0;
128+
129+
if (!xsrfTokenPresent) {
130+
console.log(`Response status: ${response.status}`);
131+
console.log(`Response headers:`);
132+
console.log(response.headers);
133+
console.log(`Cookies: ${document.cookie}`);
134+
135+
throw new Error(
136+
`XSRF token cookie is missing in the response. Please ensure that SANCTUM_STATEFUL_DOMAINS and SESSION_DOMAIN environment variables are configured correctly on the API side.`
137+
);
138+
}
112139
}
113140

114141
protected static buildHttpClientConfig(): AxiosRequestConfig {

tests/integration/drivers/default/server.ts

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export default function makeServer() {
2626
this.namespace = '';
2727

2828
this.get('/sanctum/csrf-cookie', () => {
29+
const cookieExpiration = new Date(new Date().getTime() + 24 * 3600 * 1000);
30+
document.cookie = `XSRF-TOKEN=test; path=/; expires=${cookieExpiration.toUTCString()};`;
31+
2932
return [];
3033
});
3134

tests/integration/orion.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ afterEach(() => {
1313
});
1414

1515
describe('Orion tests', () => {
16-
test('fetching csrf cookie', async () => {
16+
test('retrieving csrf cookie', async () => {
1717
Orion.setAuthDriver(AuthDriver.Sanctum);
1818

1919
await Orion.csrf();

0 commit comments

Comments
 (0)