Skip to content

Commit

Permalink
Update CSRF on every modal init
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Nov 16, 2021
1 parent 1ca0653 commit 15fd7ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 4 additions & 7 deletions js/src/forum/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import app from 'flarum/forum/app';
// import { extend } from 'flarum/common/extend';
// import LogInModal from 'flarum/forum/components/LogInModal';
// import SignUpModal from 'flarum/forum/components/SignUpModal';
import { extend } from 'flarum/common/extend';
import Modal from 'flarum/common/components/Modal';
import updateCSRF from './utils/updateCSRF';

app.initializers.add('acpl-lscache', () => {
// [LogInModal, SignUpModal].forEach((Component) => {
// extend(Component.prototype, 'oninit', updateCSRF);
// });
updateCSRF();
// We extend each modal to also include those added by external extensions
extend(Modal.prototype, 'oninit', updateCSRF);
});
4 changes: 4 additions & 0 deletions js/src/forum/utils/updateCSRF.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import app from 'flarum/forum/app';

export default async () => {
if (app.session.user) {
return;
}

// We do not use `app.request` here because that function returns only the body and we need headers
const res = await fetch(`${app.forum.attribute('apiUrl')}/lscache-csrf`);
const token = res.headers.get('X-CSRF-Token');
Expand Down

0 comments on commit 15fd7ff

Please sign in to comment.