@@ -122,9 +122,23 @@ function oAuthPluginServer(
122122 const authorizePath = options . authorizePath ?? '/oauth2/authorize'
123123 const collectionSlug = ( options . userCollection ?. slug as 'users' ) || 'users'
124124 const sub = options . subField ?. name || 'sub'
125+ const oAuthStrategyCount = ( incoming . custom ?. oAuthStrategyCount || 0 ) + 1
126+ const strategyName = `oauth2-${ incoming . custom ?. oAuthStrategyCount } `
125127
126- // Passport strategy
127128 if ( options . clientID ) {
129+ // Validate paths, they must be unique
130+ const oAuthPaths = incoming . custom ?. oAuthPaths || new Set ( )
131+ if ( oAuthPaths . has ( authorizePath ) )
132+ throw new Error (
133+ `Choose a unique authorizePath for oAuth strategy ${ oAuthStrategyCount } (not ${ options . authorizePath } )`
134+ )
135+ oAuthPaths . add ( authorizePath )
136+ if ( oAuthPaths . has ( callbackPath ) )
137+ throw new Error (
138+ `Choose a unique callbackPath for oAuth strategy ${ oAuthStrategyCount } (not ${ options . callbackPath } )`
139+ )
140+
141+ // Passport strategy
128142 const strategy = new OAuth2Strategy ( options , async function (
129143 accessToken : string ,
130144 refreshToken : string ,
@@ -154,7 +168,7 @@ function oAuthPluginServer(
154168 if ( users . docs && users . docs . length ) {
155169 user = users . docs [ 0 ]
156170 user . collection = collectionSlug
157- user . _strategy = 'oauth2'
171+ user . _strategy = strategyName
158172 } else {
159173 // Register new user
160174 user = await payload . create ( {
@@ -168,7 +182,7 @@ function oAuthPluginServer(
168182 } )
169183 log ( 'signin.user' , user )
170184 user . collection = collectionSlug
171- user . _strategy = 'oauth2'
185+ user . _strategy = strategyName
172186 }
173187
174188 cb ( null , user )
@@ -185,21 +199,22 @@ function oAuthPluginServer(
185199 // else cb(null, user)
186200 // }
187201
188- passport . use ( strategy )
202+ passport . use ( strategyName , strategy )
203+ // passport.serializeUser((user: Express.User, done) => {
204+ passport . serializeUser ( ( user : any , done ) => {
205+ done ( null , user . id )
206+ } )
207+ passport . deserializeUser ( async ( id : string , done ) => {
208+ const ok = await payload . findByID ( { collection : collectionSlug , id } )
209+ done ( null , ok )
210+ } )
189211 } else {
190212 console . warn ( 'No client id, oauth disabled' )
191213 }
192- // passport.serializeUser((user: Express.User, done) => {
193- passport . serializeUser ( ( user : any , done ) => {
194- done ( null , user . id )
195- } )
196- passport . deserializeUser ( async ( id : string , done ) => {
197- const ok = await payload . findByID ( { collection : collectionSlug , id } )
198- done ( null , ok )
199- } )
200214
201215 return {
202216 ...incoming ,
217+ custom : { ...incoming . custom , oAuthStrategyCount } ,
203218 admin : {
204219 ...incoming . admin ,
205220 webpack : ( webpackConfig ) => {
@@ -225,7 +240,7 @@ function oAuthPluginServer(
225240 path : authorizePath ,
226241 method : 'get' ,
227242 root : true ,
228- handler : passport . authenticate ( 'oauth2' ) ,
243+ handler : passport . authenticate ( strategyName ) ,
229244 } ,
230245 {
231246 path : callbackPath ,
@@ -249,7 +264,7 @@ function oAuthPluginServer(
249264 path : callbackPath ,
250265 method : 'get' ,
251266 root : true ,
252- handler : passport . authenticate ( 'oauth2' , { failureRedirect : '/' } ) ,
267+ handler : passport . authenticate ( strategyName , { failureRedirect : '/' } ) ,
253268 } ,
254269 {
255270 path : callbackPath ,
0 commit comments