-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
671 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Button from "payload/dist/admin/components/elements/Button"; | ||
|
||
export default function OAuthButton() { | ||
return ( | ||
<div style={{ marginBottom: 40 }}> | ||
<Button el="anchor" url="/oauth2/authorize"> | ||
Sign in with oAuth | ||
</Button> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import OAuthButton from "./OAuthButton"; | ||
|
||
import { Config } from "payload/config"; | ||
import { TextField } from "payload/types"; | ||
|
||
import type { oAuthPluginOptions } from "./types"; | ||
|
||
export { OAuthButton, oAuthPluginOptions }; | ||
|
||
// Detect client side because some dependencies may be nullified | ||
const CLIENTSIDE = typeof document !== "undefined"; | ||
|
||
export const oAuthPlugin = | ||
(options: oAuthPluginOptions) => | ||
async (incoming: Config): Promise<Config> => { | ||
let funcToCall; | ||
if (CLIENTSIDE) { | ||
funcToCall = (await import("./oAuthClient")).default; | ||
} else { | ||
funcToCall = (await import("./oAuthServer")).default; | ||
} | ||
// Shorthands | ||
const collectionSlug = options.userCollection?.slug ?? "users"; | ||
const sub = options.subField?.name ?? "sub"; | ||
|
||
// Spread the existing config | ||
const config: Config = { | ||
...incoming, | ||
collections: (incoming.collections ?? []).map((c) => { | ||
// Let's track the oAuth id (sub) | ||
if ( | ||
c.slug === collectionSlug && | ||
!c.fields.some((f) => (f as TextField).name === sub) | ||
) { | ||
c.fields.push({ | ||
name: sub, | ||
type: "text", | ||
admin: { readOnly: true }, | ||
access: { update: () => false }, | ||
}); | ||
} | ||
return c; | ||
}), | ||
}; | ||
|
||
return funcToCall(config, options); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import OAuthButton from "./OAuthButton"; | ||
|
||
import { Config } from "payload/config"; | ||
|
||
import type { oAuthPluginOptions } from "./types"; | ||
function oAuthPluginClient( | ||
incoming: Config, | ||
options: oAuthPluginOptions, | ||
): Config { | ||
const button: React.ComponentType = options.components?.Button ?? OAuthButton; | ||
return { | ||
...incoming, | ||
admin: { | ||
...incoming.admin, | ||
components: { | ||
...incoming.admin?.components, | ||
beforeLogin: (incoming.admin?.components?.beforeLogin ?? []).concat( | ||
button, | ||
), | ||
}, | ||
}, | ||
}; | ||
} | ||
export default oAuthPluginClient; |
Oops, something went wrong.