Skip to content

Commit

Permalink
Add support for passing appContext to getCsrfToken
Browse files Browse the repository at this point in the history
Requested in #345

getSession() already does this so seems reasonable to support it in getCsrfToken too.
  • Loading branch information
iaincollins committed Jul 10, 2020
1 parent a236da9 commit 861b019
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ const getSession = async ({ req, ctx, triggerEvent = true } = {}) => {
}

// Universal method (client + server)
const getCsrfToken = async ({ req } = {}) => {
const getCsrfToken = async ({ req, ctx } = {}) => {
// If passed 'appContext' via getInitialProps() in _app.js then get the req
// object from ctx and use that for the req value to allow getCsrfToken() to
// work seemlessly in getInitialProps() on server side pages *and* in _app.js.
if (!req && ctx && ctx.req) { req = ctx.req }

const baseUrl = _apiBaseUrl()
const fetchOptions = req ? { headers: { cookie: req.headers.cookie } } : {}
const data = await _fetchData(`${baseUrl}/csrf`, fetchOptions)
Expand Down

0 comments on commit 861b019

Please sign in to comment.