Skip to content

Commit 4f672c1

Browse files
authored
feat: Add custom subField (#42)
* fix export typo * introduce customSubField * rename types
1 parent 53cf11b commit 4f672c1

7 files changed

+23
-15
lines changed

src/auth-strategy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import {
77
User,
88
parseCookies,
99
} from "payload";
10-
import { PluginTypes } from "./types";
10+
import { PluginOptions } from "./types";
1111

1212
export const createAuthStrategy = (
13-
pluginOptions: PluginTypes,
13+
pluginOptions: PluginOptions,
1414
subFieldName: string,
1515
): AuthStrategy => {
1616
const authStrategy: AuthStrategy = {

src/authorize-endpoint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import crypto from "crypto";
22
import type { Endpoint } from "payload";
3-
import type { PluginTypes } from "./types";
3+
import type { PluginOptions } from "./types";
44

55
export const createAuthorizeEndpoint = (
6-
pluginOptions: PluginTypes,
6+
pluginOptions: PluginOptions,
77
): Endpoint => ({
88
method: "get",
99
path: pluginOptions.authorizePath || "/oauth/authorize",

src/callback-endpoint.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import type {
1313
} from "payload";
1414
import { generatePayloadCookie, getFieldsToSign } from "payload";
1515
import { defaultGetToken } from "./default-get-token";
16-
import type { PluginTypes } from "./types";
16+
import type { PluginOptions } from "./types";
1717

1818
export const createCallbackEndpoint = (
19-
pluginOptions: PluginTypes,
19+
pluginOptions: PluginOptions,
2020
): Endpoint[] => {
2121
const handler: PayloadHandler = async (req: PayloadRequest) => {
2222
try {
@@ -48,7 +48,8 @@ export const createCallbackEndpoint = (
4848
// /////////////////////////////////////
4949
// shorthands
5050
// /////////////////////////////////////
51-
const subFieldName = pluginOptions.subFieldName || "sub";
51+
const subFieldName =
52+
pluginOptions.subField?.name || pluginOptions.subFieldName || "sub";
5253
const authCollection = (pluginOptions.authCollection ||
5354
"users") as CollectionSlug;
5455
const collectionConfig = req.payload.collections[authCollection].config;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { defaultGetToken } from "./default-get-token";
22
export { OAuth2Plugin } from "./plugin";
3-
export type { PluginTypes } from "./types";
3+
export type { PluginOptions as PluginTypes } from "./types";

src/modify-auth-collection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { AuthStrategy, type CollectionConfig } from "payload";
22
import { createAuthStrategy } from "./auth-strategy";
33
import { createAuthorizeEndpoint } from "./authorize-endpoint";
44
import { createCallbackEndpoint } from "./callback-endpoint";
5-
import { PluginTypes } from "./types";
5+
import { PluginOptions } from "./types";
66

77
export const modifyAuthCollection = (
8-
pluginOptions: PluginTypes,
8+
pluginOptions: PluginOptions,
99
existingCollectionConfig: CollectionConfig,
1010
subFieldName: string,
1111
): CollectionConfig => {

src/plugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Plugin } from "payload";
22
import { modifyAuthCollection } from "./modify-auth-collection";
3-
import type { PluginTypes } from "./types";
3+
import type { PluginOptions } from "./types";
44

55
export const OAuth2Plugin =
6-
(pluginOptions: PluginTypes): Plugin =>
6+
(pluginOptions: PluginOptions): Plugin =>
77
(incomingConfig) => {
88
let config = { ...incomingConfig };
99

@@ -15,7 +15,8 @@ export const OAuth2Plugin =
1515
// Modify auth collection
1616
// /////////////////////////////////////
1717
const authCollectionSlug = pluginOptions.authCollection || "users";
18-
const subFieldName = pluginOptions.subFieldName || "sub";
18+
const subFieldName =
19+
pluginOptions.subField?.name || pluginOptions.subFieldName || "sub";
1920
const authCollection = config.collections?.find(
2021
(collection) => collection.slug === authCollectionSlug,
2122
);

src/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { PayloadRequest } from "payload";
1+
import type { PayloadRequest, TextField } from "payload";
22

3-
export interface PluginTypes {
3+
export interface PluginOptions {
44
/**
55
* Enable or disable plugin
66
* @default false
@@ -64,6 +64,12 @@ export interface PluginTypes {
6464
*/
6565
subFieldName?: string;
6666

67+
/**
68+
* Customize your own sub field to store the OAuth provider's user ID.
69+
* Overrides `subFieldName` if both are provided.
70+
*/
71+
subField?: TextField;
72+
6773
/**
6874
* Client ID for the OAuth provider
6975
*/

0 commit comments

Comments
 (0)