diff --git a/src/auth-strategy.ts b/src/auth-strategy.ts index ae3e511..9de0445 100644 --- a/src/auth-strategy.ts +++ b/src/auth-strategy.ts @@ -7,10 +7,10 @@ import { User, parseCookies, } from "payload"; -import { PluginTypes } from "./types"; +import { PluginOptions } from "./types"; export const createAuthStrategy = ( - pluginOptions: PluginTypes, + pluginOptions: PluginOptions, subFieldName: string, ): AuthStrategy => { const authStrategy: AuthStrategy = { diff --git a/src/authorize-endpoint.ts b/src/authorize-endpoint.ts index c283eb4..d215ab5 100644 --- a/src/authorize-endpoint.ts +++ b/src/authorize-endpoint.ts @@ -1,9 +1,9 @@ import crypto from "crypto"; import type { Endpoint } from "payload"; -import type { PluginTypes } from "./types"; +import type { PluginOptions } from "./types"; export const createAuthorizeEndpoint = ( - pluginOptions: PluginTypes, + pluginOptions: PluginOptions, ): Endpoint => ({ method: "get", path: pluginOptions.authorizePath || "/oauth/authorize", diff --git a/src/callback-endpoint.ts b/src/callback-endpoint.ts index 209fa03..866de73 100644 --- a/src/callback-endpoint.ts +++ b/src/callback-endpoint.ts @@ -13,10 +13,10 @@ import type { } from "payload"; import { generatePayloadCookie, getFieldsToSign } from "payload"; import { defaultGetToken } from "./default-get-token"; -import type { PluginTypes } from "./types"; +import type { PluginOptions } from "./types"; export const createCallbackEndpoint = ( - pluginOptions: PluginTypes, + pluginOptions: PluginOptions, ): Endpoint[] => { const handler: PayloadHandler = async (req: PayloadRequest) => { try { @@ -48,7 +48,8 @@ export const createCallbackEndpoint = ( // ///////////////////////////////////// // shorthands // ///////////////////////////////////// - const subFieldName = pluginOptions.subFieldName || "sub"; + const subFieldName = + pluginOptions.subField?.name || pluginOptions.subFieldName || "sub"; const authCollection = (pluginOptions.authCollection || "users") as CollectionSlug; const collectionConfig = req.payload.collections[authCollection].config; diff --git a/src/index.ts b/src/index.ts index 4737969..611bd25 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export { defaultGetToken } from "./default-get-token"; export { OAuth2Plugin } from "./plugin"; -export type { PluginTypes } from "./types"; +export type { PluginOptions as PluginTypes } from "./types"; diff --git a/src/modify-auth-collection.ts b/src/modify-auth-collection.ts index ba04f61..9fe0eae 100644 --- a/src/modify-auth-collection.ts +++ b/src/modify-auth-collection.ts @@ -2,10 +2,10 @@ import { AuthStrategy, type CollectionConfig } from "payload"; import { createAuthStrategy } from "./auth-strategy"; import { createAuthorizeEndpoint } from "./authorize-endpoint"; import { createCallbackEndpoint } from "./callback-endpoint"; -import { PluginTypes } from "./types"; +import { PluginOptions } from "./types"; export const modifyAuthCollection = ( - pluginOptions: PluginTypes, + pluginOptions: PluginOptions, existingCollectionConfig: CollectionConfig, subFieldName: string, ): CollectionConfig => { diff --git a/src/plugin.ts b/src/plugin.ts index f7c68ed..ef04ea8 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,9 +1,9 @@ import type { Plugin } from "payload"; import { modifyAuthCollection } from "./modify-auth-collection"; -import type { PluginTypes } from "./types"; +import type { PluginOptions } from "./types"; export const OAuth2Plugin = - (pluginOptions: PluginTypes): Plugin => + (pluginOptions: PluginOptions): Plugin => (incomingConfig) => { let config = { ...incomingConfig }; @@ -15,7 +15,8 @@ export const OAuth2Plugin = // Modify auth collection // ///////////////////////////////////// const authCollectionSlug = pluginOptions.authCollection || "users"; - const subFieldName = pluginOptions.subFieldName || "sub"; + const subFieldName = + pluginOptions.subField?.name || pluginOptions.subFieldName || "sub"; const authCollection = config.collections?.find( (collection) => collection.slug === authCollectionSlug, ); diff --git a/src/types.ts b/src/types.ts index 487a90a..63af8ee 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,6 @@ -import type { PayloadRequest } from "payload"; +import type { PayloadRequest, TextField } from "payload"; -export interface PluginTypes { +export interface PluginOptions { /** * Enable or disable plugin * @default false @@ -64,6 +64,12 @@ export interface PluginTypes { */ subFieldName?: string; + /** + * Customize your own sub field to store the OAuth provider's user ID. + * Overrides `subFieldName` if both are provided. + */ + subField?: TextField; + /** * Client ID for the OAuth provider */