Skip to content

Commit 6a44e82

Browse files
committed
Add support for multiple strategies
1 parent eafa033 commit 6a44e82

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ export default buildConfig({
6464
plugins: [
6565
payloadCloud(),
6666
oAuthPlugin({
67+
buttonLabel: 'Sign in with oAuth',
6768
databaseUri: process.env.DATABASE_URI,
6869
clientID: process.env.OAUTH_CLIENT_ID,
6970
clientSecret: process.env.OAUTH_CLIENT_SECRET,
7071
authorizationURL: process.env.OAUTH_AUTH_ENDPOINT,
7172
tokenURL: process.env.OAUTH_TOKEN_ENDPOINT,
72-
callbackURL: process.env.OAUTH_CALLBACK_ENDPOINT,
73+
authorizePath: '/oauth/authorize1',
74+
callbackURL: process.env.ORIGIN + '/oauth/callback1',
7375
async userinfo(accessToken) {
7476
const { data: user } = await axios.get(
7577
process.env.OAUTH_USERINFO_ENDPOINT,
@@ -81,6 +83,15 @@ export default buildConfig({
8183
}
8284
},
8385
}),
86+
// Another oAuth provider
87+
oAuthPlugin({
88+
buttonLabel: 'Sign in with Alternative',
89+
// These paths must be unique per provider
90+
authorizePath: '/oauth/authorize2',
91+
callbackURL: process.env.ORIGIN + '/oauth/callback2',
92+
93+
...rest,
94+
}),
8495
],
8596
db: mongooseAdapter({
8697
url: process.env.DATABASE_URI,

src/OAuthButton.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import Button from 'payload/dist/admin/components/elements/Button'
2-
import React, { useEffect } from 'react'
2+
import React from 'react'
3+
import { ButtonProps } from './types'
34

4-
export default function OAuthButton() {
5-
useEffect(() => {
6-
setTimeout(() => {
7-
// window.location.href = '/a'
8-
}, 2000)
9-
}, [])
5+
export default function OAuthButton(props: ButtonProps) {
106
return (
117
<div style={{ marginBottom: 40 }}>
12-
<Button el="anchor" url="/oauth2/authorize">
13-
Sign in with oAuth
8+
<Button el="anchor" url={props.authorizePath}>
9+
{props.buttonLabel}
1410
</Button>
1511
</div>
1612
)

src/index.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { TextField } from 'payload/types'
1818

1919
import OAuthButton from './OAuthButton'
2020
import type { oAuthPluginOptions } from './types'
21+
import { createElement } from 'react'
2122

2223
export { OAuthButton, oAuthPluginOptions }
2324

@@ -94,20 +95,25 @@ function oAuthPluginClient(
9495
incoming: Config,
9596
options: oAuthPluginOptions
9697
): Config {
97-
const button: React.ComponentType<any> =
98-
options.components?.Button || OAuthButton
99-
return {
100-
...incoming,
101-
admin: {
102-
...incoming.admin,
103-
components: {
104-
...incoming.admin?.components,
105-
beforeLogin: (incoming.admin?.components?.beforeLogin || []).concat(
106-
button
107-
),
108-
},
109-
},
110-
}
98+
const button = options.components?.Button ?? OAuthButton
99+
return button
100+
? {
101+
...incoming,
102+
admin: {
103+
...incoming.admin,
104+
components: {
105+
...incoming.admin?.components,
106+
beforeLogin: (incoming.admin?.components?.beforeLogin || []).concat(
107+
() =>
108+
createElement(button, {
109+
authorizePath: options.authorizePath || '/oauth2/authorize',
110+
buttonLabel: options.buttonLabel || `Sign in with oAuth`,
111+
})
112+
),
113+
},
114+
},
115+
}
116+
: incoming
111117
}
112118

113119
function oAuthPluginServer(
@@ -123,7 +129,7 @@ function oAuthPluginServer(
123129
const collectionSlug = (options.userCollection?.slug as 'users') || 'users'
124130
const sub = options.subField?.name || 'sub'
125131
const oAuthStrategyCount = (incoming.custom?.oAuthStrategyCount || 0) + 1
126-
const strategyName = `oauth2-${incoming.custom?.oAuthStrategyCount}`
132+
const strategyName = `oauth2-${oAuthStrategyCount}`
127133

128134
if (options.clientID) {
129135
// Validate paths, they must be unique

src/types.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ export interface oAuthPluginOptions extends StrategyOptions {
4646
/** Which path to mount in express, defaults to the path in callbackURL */
4747
callbackPath?: string
4848

49+
/**
50+
* Text on the sign in button
51+
* @default "Sign in with oAuth"
52+
*/
53+
buttonLabel?: string
54+
4955
components?: {
50-
Button?: ComponentType<any>
56+
Button?: false | ((props: ButtonProps) => JSX.Element)
5157
}
5258
userCollection?: {
5359
/** @default "users" */
@@ -63,3 +69,10 @@ export interface oAuthPluginOptions extends StrategyOptions {
6369
*/
6470
successRedirect?: string
6571
}
72+
73+
export type ButtonProps = {
74+
/** Path that initiates the oAuth flow */
75+
authorizePath: string
76+
/** Text on the sign in button */
77+
buttonLabel: string
78+
}

0 commit comments

Comments
 (0)