Skip to content

How to handle CSRF validation without Twig (headless) #9685

Answered by brandonkelly
engram-design asked this question in Q&A
Discussion options

You must be logged in to vote

Anonymous form submissions aren’t a particularly attractive target for an attacker, so you might just want to disable CSRF validation entirely for guest users. You can do that by adding this to your controller’s beforeAction() method:

public function beforeAction($action)
{
    if ($action->id === 'submit' && Craft::$app->user->isGuest) {
        $this->enableCsrfValidation = false;
    }

    return parent::beforeAction($action);
}

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@engram-design
Comment options

Comment options

You must be logged in to vote
1 reply
@engram-design
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants