Add csrf function to middleware
Refactor csrf method