UNPKG

@adonisjs/ally

Version:

Social authentication provider for AdonisJS

126 lines (123 loc) 3.34 kB
import { E_LOCAL_SIGNUP_DISALLOWED, E_UNKNOWN_ALLY_PROVIDER } from "./chunk-TSIMPJ6I.js"; // src/ally_manager.ts var AllyManager = class { /** * Create a new Ally manager for the current request. * * @param config - Map of provider names to driver factory functions * @param ctx - The current HTTP context */ constructor(config, ctx) { this.config = config; this.#ctx = ctx; } config; /** * The current HTTP context bound to this manager instance. */ #ctx; /** * Cache of instantiated providers keyed by provider name. */ #driversCache = /* @__PURE__ */ new Map(); /** * Find if a provider has been configured. * * @param provider - The provider name to check. * @returns `true` when the provider exists in the manager config. * * @example * ```ts * if (ally.has(provider)) { * await ally.use(provider).redirect() * } * ``` */ has(provider) { return provider in this.config; } /** * Find if a provider allows local signup. * * @param provider - The configured provider name to inspect. * @returns `true` when the provider does not opt out of local signup. * * @example * ```ts * if (ally.allowsLocalSignup('github')) { * return ally.use('github', { intent: 'signup' }).redirect() * } * ``` */ allowsLocalSignup(provider) { if (!this.has(provider)) { throw new E_UNKNOWN_ALLY_PROVIDER([provider]); } const driver = this.use(provider); return !driver.config?.disallowLocalSignup; } /** * Returns configured provider names. * * @returns An array of configured provider names. * * @example * ```ts * const providers = ally.configuredProviderNames() * ``` */ configuredProviderNames() { return Object.keys(this.config); } /** * Returns provider names that allow local signup. * * @returns An array of configured provider names that allow signup flows. * * @example * ```ts * const signupProviders = ally.signupProviderNames() * ``` */ signupProviderNames() { return this.configuredProviderNames().filter((provider) => this.allowsLocalSignup(provider)); } /** * Get a driver instance for the specified social provider. The driver * instance is cached for the duration of the HTTP request. * * @param provider - The name of the social provider (e.g., 'github', 'google') * @param options - Additional options used to qualify the provider usage. * @returns The instantiated social authentication driver. * * @example * ```ts * const github = ally.use('github') * await github.redirect() * * const signupDriver = ally.use('github', { intent: 'signup' }) * await signupDriver.redirect() * ``` */ use(provider, options) { if (this.#driversCache.has(provider)) { return this.#driversCache.get(provider); } if (!this.has(String(provider))) { throw new E_UNKNOWN_ALLY_PROVIDER([provider]); } const driver = this.config[provider]; if (options?.intent === "signup" && !this.allowsLocalSignup(provider)) { throw new E_LOCAL_SIGNUP_DISALLOWED([provider]); } const driverInstance = driver(this.#ctx); this.#driversCache.set(provider, driverInstance); return driverInstance; } }; export { AllyManager };