DEV Community

Discussion on: How to do the User Registration in Mojolicious

Collapse
 
cam profile image
Cam Stuart • Edited

Thanks for this! it is very helpful!! Should a csrf token also be used in the form? if so, how do we verify it in the controller action?

Collapse
 
akuks profile image
Ashutosh • Edited

Yes, CSRF can also be used in the controller you need to write this code in the controller

# CSRF Protection
    my $v        = $c->validation;
    return $c->render( template => 'not_found.html.ep', status => 403 )
      if $v->csrf_protect->has_error('csrf_token');
Enter fullscreen mode Exit fullscreen mode

And in the Template

%= form_for '/form_action_url => ( method => 'post', name => 'form_name' ) => begin
    <!-- Other Form Fields -->
    %= csrf_field
% end
Enter fullscreen mode Exit fullscreen mode

That's it. You are good to with.