UNPKG

@adonisjs/auth

Version:

Official authentication provider for Adonis framework

111 lines (110 loc) 3.01 kB
import { r as errors_exports } from "./errors-eDV8ejO0.js"; import { t as symbols_exports } from "./symbols-C5QEqFvJ.js"; import { n as AuthenticatorClient, r as Authenticator, t as AuthManager } from "./auth_manager-Cp_ofh4p.js"; import { presetAuth } from "@adonisjs/presets/auth"; import { configProvider } from "@adonisjs/core"; //#region configure.ts /** * Configures the auth package */ async function configure(command) { const codemods = await command.createCodemods(); let guard = command.parsedFlags.guard; /** * Prompts user to select a guard when not mentioned via * the CLI */ if (guard === void 0) guard = await command.prompt.choice("Select the auth guard you want to use", [ { name: "session", message: "Session" }, { name: "access_tokens", message: "Opaque access tokens" }, { name: "basic_auth", message: "Basic Auth" } ], { validate(value) { return !!value; } }); /** * Ensure selected or guard defined via the CLI flag is * valid */ if (![ "session", "access_tokens", "basic_auth" ].includes(guard)) { command.logger.error(`The selected guard "${guard}" is invalid. Select one from: session, access_tokens, basic_auth`); command.exitCode = 1; return; } await presetAuth(codemods, command.app, { guard, userProvider: "lucid" }); } //#endregion //#region src/define_config.ts /** * Define configuration for the auth package. The function returns * a config provider that is invoked inside the auth service * provider * * @param config - Configuration object with default guard and available guards * * @example * import { defineConfig } from '@adonisjs/auth' * import { sessionGuard, sessionUserProvider } from '@adonisjs/auth/session' * * const authConfig = defineConfig({ * default: 'web', * guards: { * web: sessionGuard({ * useRememberMeTokens: false, * provider: sessionUserProvider({ * model: () => import('#models/user') * }) * }) * } * }) */ function defineConfig(config) { return configProvider.create(async (app) => { const guardsList = Object.keys(config.guards); const guards = {}; for (let guardName of guardsList) { const guard = config.guards[guardName]; if (typeof guard === "function") guards[guardName] = guard; else guards[guardName] = await guard.resolver(guardName, app); } return { default: config.default, guards }; }); } //#endregion //#region index.ts function isModuleInstalled(moduleName) { try { import.meta.resolve(moduleName); return true; } catch (e) { return false; } } /** * @deprecated Import `withAuthFinder` from `@adonisjs/auth/mixins/lucid` instead */ let withAuthFinder; if (isModuleInstalled("@adonisjs/lucid")) { const { withAuthFinder: withAuthFinderFn } = await import("./src/mixins/lucid.js"); withAuthFinder = withAuthFinderFn; } //#endregion export { AuthManager, Authenticator, AuthenticatorClient, configure, defineConfig, errors_exports as errors, symbols_exports as symbols, withAuthFinder };