Skip to content

feat: introduce onUserNotFoundBehavior #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
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
34 changes: 23 additions & 11 deletions src/callback-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export const createCallbackEndpoint = (
const useEmailAsIdentity = pluginOptions.useEmailAsIdentity ?? false;
const excludeEmailFromJwtToken =
!useEmailAsIdentity || pluginOptions.excludeEmailFromJwtToken || false;
const onUserNotFoundBehavior =
pluginOptions.onUserNotFoundBehavior || "create";

// /////////////////////////////////////
// beforeOperation - Collection
Expand Down Expand Up @@ -117,17 +119,27 @@ export const createCallbackEndpoint = (

let user = existingUser.docs[0] as User;
if (!user) {
// Create new user if they don't exist
// Generate secure random password for OAuth users
userInfo.password = crypto.randomBytes(32).toString("hex");
userInfo.collection = authCollection;
const result = await req.payload.create({
req,
collection: authCollection,
data: userInfo,
showHiddenFields: true,
});
user = result as unknown as User;
if (onUserNotFoundBehavior === "error") {
throw new Error(
`User not found: ${useEmailAsIdentity ? userInfo.email : userInfo[subFieldName]}`,
);
} else if (onUserNotFoundBehavior === "create") {
// Create new user if they don't exist
// Generate secure random password for OAuth users
userInfo.password = crypto.randomBytes(32).toString("hex");
userInfo.collection = authCollection;
const result = await req.payload.create({
req,
collection: authCollection,
data: userInfo,
showHiddenFields: true,
});
user = result as unknown as User;
} else {
throw new Error(
`Invalid onUserNotFoundBehavior: ${onUserNotFoundBehavior}`,
);
}
} else {
// Update existing user with latest info from provider
userInfo.collection = authCollection;
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ export interface PluginOptions {
req: PayloadRequest,
) => Promise<Record<string, unknown>>;

/**
* Behavior when a user is not found in the database.
* If set to "create", a new user will be created with the information
* returned from the OAuth provider.
* If set to "error", an error will be thrown and the user will not
* be created.
* @default "create"
*/
onUserNotFoundBehavior?: "create" | "error";

/**
* Scope for the OAuth provider.
* The following are scopes for popular OAuth providers:
Expand Down