UNPKG

payload-auth-plugin-fix

Version:
55 lines (54 loc) 1.89 kB
// src/plugins/admin.ts import { EndpointFactory } from "../core/endpoints.js"; import { PayloadSession } from "../core/session/payload.js"; import { InvalidServerURL, MissingUsersCollection } from "../core/errors/consoleErrors.js"; import { buildAccountsCollection } from "../core/collections/admin/accounts.js"; import { mapProviders } from "../providers/utils.js"; var adminAuthPlugin = (pluginOptions) => (incomingConfig) => { const config = { ...incomingConfig }; if (pluginOptions.enabled === false) { return config; } if (!config.serverURL) { throw new InvalidServerURL; } if (!config.admin?.user) { throw new MissingUsersCollection; } config.admin = { ...config.admin ?? {} }; const { accounts, providers, allowSignUp, successPath } = pluginOptions; const session = new PayloadSession({ accountsCollectionSlug: accounts?.slug ?? "accounts", customersCollectionSlug: "customers" }, allowSignUp, successPath, pluginOptions.redirectFunctions); const mappedProviders = mapProviders(providers); const endpoints = new EndpointFactory(mappedProviders); config.collections = [ ...config.collections ?? [], buildAccountsCollection({ slug: accounts?.slug ?? "accounts", hidden: accounts?.hidden ?? false }, config.admin.user) ]; config.endpoints = [ ...config.endpoints ?? [], ...endpoints.payloadOAuthEndpoints({ sessionCallback: (oauthAccountInfo, scope, issuerName, basePayload) => session.createSession(oauthAccountInfo, scope, issuerName, basePayload) }) ]; if (mappedProviders["passkey"]) { config.endpoints.push(...endpoints.payloadPasskeyEndpoints({ rpID: "localhost", sessionCallback: (accountInfo, issuerName, basePayload) => session.createSession(accountInfo, "", issuerName, basePayload) })); } return config; }; export { adminAuthPlugin };