Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/auth-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
4 changes: 2 additions & 2 deletions src/authorize-endpoint.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 4 additions & 3 deletions src/callback-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
4 changes: 2 additions & 2 deletions src/modify-auth-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
7 changes: 4 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand All @@ -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,
);
Expand Down
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
*/
Expand Down