Skip to content

Commit

Permalink
Add method to override default CSRF script
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Oct 31, 2024
1 parent 152aa89 commit 4a73e63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Facades/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @method static ApplicationCacher createApplicationDriver(array $config)
* @method static \Illuminate\Cache\Repository cacheStore()
* @method static void flush()
* @method static void csrfJs(string $js)
* @method static void nocacheJs(string $js)
* @method static void nocachePlaceholder(string $placeholder)
* @method static void includeJs()
Expand Down
14 changes: 13 additions & 1 deletion src/StaticCaching/Cachers/FileCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class FileCacher extends AbstractCacher
*/
private $shouldOutputJs = false;

/**
* @var string
*/
private $csrfJs;

/**
* @var string
*/
Expand Down Expand Up @@ -196,6 +201,11 @@ private function isLongQueryStringPath($path)
return Str::contains($path, '_lqs_');
}

public function setCsrfJs(string $js)
{
$this->csrfJs = $js;
}

public function setNocacheJs(string $js)
{
$this->nocacheJs = $js;
Expand All @@ -205,7 +215,7 @@ public function getCsrfTokenJs(): string
{
$csrfPlaceholder = CsrfTokenReplacer::REPLACEMENT;

return <<<EOT
$default = <<<EOT
(function() {
fetch('/!/csrf', {
method: 'POST',
Expand Down Expand Up @@ -237,6 +247,8 @@ public function getCsrfTokenJs(): string
});
})();
EOT;

return $this->csrfJs ?? $default;
}

public function getNocacheJs(): string
Expand Down
5 changes: 5 additions & 0 deletions src/StaticCaching/StaticCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ private function flushNocache()
$this->cacheStore()->forget('nocache::urls');
}

public function csrfJs(string $js)
{
$this->fileDriver()->setNocacheJs($js);
}

public function nocacheJs(string $js)
{
$this->fileDriver()->setNocacheJs($js);
Expand Down

0 comments on commit 4a73e63

Please sign in to comment.