Skip to content

Commit 861b019

Browse files
committed
Add support for passing appContext to getCsrfToken
Requested in #345 getSession() already does this so seems reasonable to support it in getCsrfToken too.
1 parent a236da9 commit 861b019

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/client/index.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ const getSession = async ({ req, ctx, triggerEvent = true } = {}) => {
120120
}
121121

122122
// Universal method (client + server)
123-
const getCsrfToken = async ({ req } = {}) => {
123+
const getCsrfToken = async ({ req, ctx } = {}) => {
124+
// If passed 'appContext' via getInitialProps() in _app.js then get the req
125+
// object from ctx and use that for the req value to allow getCsrfToken() to
126+
// work seemlessly in getInitialProps() on server side pages *and* in _app.js.
127+
if (!req && ctx && ctx.req) { req = ctx.req }
128+
124129
const baseUrl = _apiBaseUrl()
125130
const fetchOptions = req ? { headers: { cookie: req.headers.cookie } } : {}
126131
const data = await _fetchData(`${baseUrl}/csrf`, fetchOptions)

0 commit comments

Comments
 (0)