Payload offers the ability to Authenticate via HTTP-only cookies. These can be read from the responses of login
, logout
, refresh
, and me
auth operations.
Modern browsers automatically include http-only
cookies when making requests directly to URLs—meaning that if you are running your API on https://example.com
, and you have logged in and visit https://example.com/test-page
, your browser will automatically include the Payload authentication cookie for you.
However, if you use fetch
or similar APIs to retrieve Payload resources from its REST or GraphQL API, you must specify to include credentials (cookies).
Fetch example, including credentials:
For more about including cookies in requests from your app to your Payload API, read the MDN docs.
CSRF (cross-site request forgery) attacks are common and dangerous. By using an HTTP-only cookie, Payload removes many XSS vulnerabilities, however, CSRF attacks can still be possible.
For example, let's say you have a popular app https://payload-finances.com
that allows users to manage finances, send and receive money. As Payload is using HTTP-only cookies, that means that browsers automatically will include cookies when sending requests to your domain - no matter what page created the request.
So, if a user of https://payload-finances.com
is logged in and is browsing around on the internet, they might stumble onto a page with malicious intent. Let's look at an example:
In this scenario, if your cookie was still valid, malicious-intent.com would be able to make requests like the one above on your behalf. This is a CSRF attack.
Define domains that your trust and are willing to accept Payload HTTP-only cookie based requests from. Use the csrf
option on the base Payload Config to do this: