Skip to content

Commit cc40dd9

Browse files
committed
fix: Fix csrf token
1 parent 526e468 commit cc40dd9

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

.env.template

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ VITE_THEME_OWNERSHIP_PER_PAGE=6
1616
# plugin configurations
1717
VITE_PLUGINS_PER_PAGE=20
1818
VITE_PLUGIN_FAVORITES_PER_PAGE=6
19-
VITE_PLUGIN_OWNERSHIP_PER_PAGE=6
19+
VITE_PLUGIN_OWNERSHIP_PER_PAGE=6
20+
21+
# CSRF token identifier
22+
VITE_CSRF_TOKEN_IDENTIFIER="LOCAL-XSRF-TOKEN"

src/utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ const formatPreviewIdToTitleCase = (input: string): string => {
6868
};
6969

7070
/**
71-
* Reads the CSRF token out of the XSRF-TOKEN cookie.
71+
* Reads the CSRF token out of the csrf token cookie.
7272
*/
7373
const getCsrfTokenFromCookie = (): string | null => {
74-
const match = document.cookie.match(/(?:^|;\s*)XSRF-TOKEN=([^;]+)/);
74+
const cookieName = import.meta.env.VITE_CSRF_TOKEN_IDENTIFIER || 'XSRF-TOKEN';
75+
const escaped = cookieName.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
76+
const pattern = new RegExp(`(?:^|;\\s*)${escaped}=([^;]+)`);
77+
const match = document.cookie.match(pattern);
7578
return match ? decodeURIComponent(match[1]) : null;
7679
};
7780

0 commit comments

Comments
 (0)