Skip to content

Commit 46deb55

Browse files
committed
Add support for OAuth 2.0 State
1 parent 7f4f70c commit 46deb55

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"clean": "rm -rf dist",
99
"build": "tsc",
1010
"fix": "npx prettier src --write",
11-
"prepare": "yarn fix && yarn clean && yarn build",
11+
"prepare": "npm run fix && npm run clean && npm run build",
1212
"test": "vitest"
1313
},
1414
"exports": {

src/index.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ function oAuthPluginServer(
132132
const sub = options.subField?.name || 'sub'
133133
const oAuthStrategyCount = (incoming.custom?.oAuthStrategyCount || 0) + 1
134134
const strategyName = `oauth2-${oAuthStrategyCount}`
135+
const sessionMiddleware = session(
136+
options.sessionOptions ?? {
137+
resave: false,
138+
saveUninitialized: false,
139+
secret:
140+
process.env.PAYLOAD_SECRET ||
141+
log('Missing process.env.PAYLOAD_SECRET') ||
142+
'unsafe',
143+
store: options.databaseUri
144+
? MongoStore.create({ mongoUrl: options.databaseUri })
145+
: undefined,
146+
}
147+
)
135148

136149
if (options.clientID) {
137150
// Validate paths, they must be unique
@@ -252,6 +265,12 @@ function oAuthPluginServer(
252265
},
253266
},
254267
endpoints: (incoming.endpoints || []).concat([
268+
{
269+
path: authorizePath,
270+
method: 'get',
271+
root: true,
272+
handler: sessionMiddleware,
273+
},
255274
{
256275
path: authorizePath,
257276
method: 'get',
@@ -262,19 +281,7 @@ function oAuthPluginServer(
262281
path: callbackPath,
263282
method: 'get',
264283
root: true,
265-
handler: session(
266-
options.sessionOptions ?? {
267-
resave: false,
268-
saveUninitialized: false,
269-
secret:
270-
process.env.PAYLOAD_SECRET ||
271-
log('Missing process.env.PAYLOAD_SECRET') ||
272-
'unsafe',
273-
store: options.databaseUri
274-
? MongoStore.create({ mongoUrl: options.databaseUri })
275-
: undefined,
276-
}
277-
),
284+
handler: sessionMiddleware,
278285
},
279286
{
280287
path: callbackPath,

0 commit comments

Comments
 (0)