Refactor code to use CSRF protection