vapor-community / CSRF

A package to add protection to Vapor against CSRF attacks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No services

grosch opened this issue · comments

I'm getting an error that "No services are available for 'CSRF' when I follow your installation instructions.

Have you added services.register(CSRF())?

@0xTim Nope. Wasn't sure how to do it. So it's basically two steps then?

services.register(CSRF())
middlewares.use(CSRF.self)

I found that not doing the services and instead doing middlewards.use(CSRF()) seemed to work as well. Can you explain the difference between the two?

Yeah that’s the two steps (it’s sneakily hidden right in the top of the code block in the README).

Basically with the code above, the first line registers the CSRF type to the services so that when the router is being built and goes to retrieve CSRF.self it knows what to use. What you’ve done is effectively the same thing in one line, the router will use CSRF() (an object) instead of the type, so it doesn’t need to look it up.

The reason for the difference is that you may want to register a middleware that uses the same instance across all event loops, of have a seperate instance for each event loop (avoids having to deal with thread safety). Make sense?

I’m sure the instructions were updated after my post. That’s my story and I’m sticking to it 😁