Skip to content
This repository has been archived by the owner on Sep 14, 2022. It is now read-only.

Commit

Permalink
docs: add single page application example
Browse files Browse the repository at this point in the history
closes #174
closes #175
  • Loading branch information
zelongc authored and dougwilson committed Apr 23, 2019
1 parent 41f19fa commit f3e614d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Expand Up @@ -199,6 +199,28 @@ fetch('/process', {
})
```

#### Single Page Application (SPA)

Many SPA frameworks like Angular have CSRF support built in automatically.
Typically they will reflect the value from a specific cookie, like
`XSRF-TOKEN` (which is the case for Angular).

To take advantage of this, set the value from `req.csrfToken()` in the cookie
used by the SPA framework. This is only necessary to do on the route that
renders the page (where `res.render` or `res.sendFile` is called in Express,
for example).

The following is an example for Express of a typical SPA response:

<!-- eslint-disable no-undef -->

```js
app.all('*', function (req, res) {
res.cookie('XSRF-TOKEN', req.csrfToken())
res.render('index')
})
```

### Ignoring Routes

**Note** CSRF checks should only be disabled for requests that you expect to
Expand Down

0 comments on commit f3e614d

Please sign in to comment.