UNPKG

better-auth

Version:

The most comprehensive authentication framework for TypeScript.

1 lines 6.13 kB
{"version":3,"file":"config.mjs","names":["pluginPathMethods: Record<string, \"POST\" | \"GET\">","atomListeners: ClientAtomListener[]"],"sources":["../../src/client/config.ts"],"sourcesContent":["import type {\n\tBetterAuthClientOptions,\n\tClientAtomListener,\n} from \"@better-auth/core\";\nimport { createFetch } from \"@better-fetch/fetch\";\nimport type { WritableAtom } from \"nanostores\";\nimport { getBaseURL } from \"../utils/url\";\nimport { redirectPlugin } from \"./fetch-plugins\";\nimport { parseJSON } from \"./parser\";\nimport { getSessionAtom } from \"./session-atom\";\n\nexport const getClientConfig = (\n\toptions?: BetterAuthClientOptions | undefined,\n\tloadEnv?: boolean | undefined,\n) => {\n\t/* check if the credentials property is supported. Useful for cf workers */\n\tconst isCredentialsSupported = \"credentials\" in Request.prototype;\n\tconst baseURL =\n\t\tgetBaseURL(options?.baseURL, options?.basePath, undefined, loadEnv) ??\n\t\t\"/api/auth\";\n\tconst pluginsFetchPlugins =\n\t\toptions?.plugins\n\t\t\t?.flatMap((plugin) => plugin.fetchPlugins)\n\t\t\t.filter((pl) => pl !== undefined) || [];\n\tconst lifeCyclePlugin = {\n\t\tid: \"lifecycle-hooks\",\n\t\tname: \"lifecycle-hooks\",\n\t\thooks: {\n\t\t\tonSuccess: options?.fetchOptions?.onSuccess,\n\t\t\tonError: options?.fetchOptions?.onError,\n\t\t\tonRequest: options?.fetchOptions?.onRequest,\n\t\t\tonResponse: options?.fetchOptions?.onResponse,\n\t\t},\n\t};\n\tconst {\n\t\tonSuccess: _onSuccess,\n\t\tonError: _onError,\n\t\tonRequest: _onRequest,\n\t\tonResponse: _onResponse,\n\t\t...restOfFetchOptions\n\t} = options?.fetchOptions || {};\n\tconst $fetch = createFetch({\n\t\tbaseURL,\n\t\t...(isCredentialsSupported ? { credentials: \"include\" } : {}),\n\t\tmethod: \"GET\",\n\t\tjsonParser(text) {\n\t\t\tif (!text) {\n\t\t\t\treturn null as any;\n\t\t\t}\n\t\t\treturn parseJSON(text, {\n\t\t\t\tstrict: false,\n\t\t\t});\n\t\t},\n\t\tcustomFetchImpl: fetch,\n\t\t...restOfFetchOptions,\n\t\tplugins: [\n\t\t\tlifeCyclePlugin,\n\t\t\t...(restOfFetchOptions.plugins || []),\n\t\t\t...(options?.disableDefaultFetchPlugins ? [] : [redirectPlugin]),\n\t\t\t...pluginsFetchPlugins,\n\t\t],\n\t});\n\tconst { $sessionSignal, session } = getSessionAtom($fetch, options);\n\tconst plugins = options?.plugins || [];\n\tlet pluginsActions = {} as Record<string, any>;\n\tlet pluginsAtoms = {\n\t\t$sessionSignal,\n\t\tsession,\n\t} as Record<string, WritableAtom<any>>;\n\tlet pluginPathMethods: Record<string, \"POST\" | \"GET\"> = {\n\t\t\"/sign-out\": \"POST\",\n\t\t\"/revoke-sessions\": \"POST\",\n\t\t\"/revoke-other-sessions\": \"POST\",\n\t\t\"/delete-user\": \"POST\",\n\t};\n\tconst atomListeners: ClientAtomListener[] = [\n\t\t{\n\t\t\tsignal: \"$sessionSignal\",\n\t\t\tmatcher(path) {\n\t\t\t\tconst matchesCommonPaths =\n\t\t\t\t\tpath === \"/sign-out\" ||\n\t\t\t\t\tpath === \"/update-user\" ||\n\t\t\t\t\tpath === \"/sign-up/email\" ||\n\t\t\t\t\tpath === \"/sign-in/email\" ||\n\t\t\t\t\tpath === \"/delete-user\" ||\n\t\t\t\t\tpath === \"/verify-email\" ||\n\t\t\t\t\tpath === \"/revoke-sessions\" ||\n\t\t\t\t\tpath === \"/revoke-session\" ||\n\t\t\t\t\tpath === \"/change-email\";\n\n\t\t\t\treturn matchesCommonPaths;\n\t\t\t},\n\t\t},\n\t];\n\n\tfor (const plugin of plugins) {\n\t\tif (plugin.getAtoms) {\n\t\t\tObject.assign(pluginsAtoms, plugin.getAtoms?.($fetch));\n\t\t}\n\t\tif (plugin.pathMethods) {\n\t\t\tObject.assign(pluginPathMethods, plugin.pathMethods);\n\t\t}\n\t\tif (plugin.atomListeners) {\n\t\t\tatomListeners.push(...plugin.atomListeners);\n\t\t}\n\t}\n\n\tconst $store = {\n\t\tnotify: (\n\t\t\tsignal?: (Omit<string, \"$sessionSignal\"> | \"$sessionSignal\") | undefined,\n\t\t) => {\n\t\t\tpluginsAtoms[signal as keyof typeof pluginsAtoms]!.set(\n\t\t\t\t!pluginsAtoms[signal as keyof typeof pluginsAtoms]!.get(),\n\t\t\t);\n\t\t},\n\t\tlisten: (\n\t\t\tsignal: Omit<string, \"$sessionSignal\"> | \"$sessionSignal\",\n\t\t\tlistener: (value: boolean, oldValue?: boolean | undefined) => void,\n\t\t) => {\n\t\t\tpluginsAtoms[signal as keyof typeof pluginsAtoms]!.subscribe(listener);\n\t\t},\n\t\tatoms: pluginsAtoms,\n\t};\n\n\tfor (const plugin of plugins) {\n\t\tif (plugin.getActions) {\n\t\t\tObject.assign(\n\t\t\t\tpluginsActions,\n\t\t\t\tplugin.getActions?.($fetch, $store, options),\n\t\t\t);\n\t\t}\n\t}\n\treturn {\n\t\tget baseURL() {\n\t\t\treturn baseURL;\n\t\t},\n\t\tpluginsActions,\n\t\tpluginsAtoms,\n\t\tpluginPathMethods,\n\t\tatomListeners,\n\t\t$fetch,\n\t\t$store,\n\t};\n};\n"],"mappings":";;;;;;;AAWA,MAAa,mBACZ,SACA,YACI;CAEJ,MAAM,yBAAyB,iBAAiB,QAAQ;CACxD,MAAM,UACL,WAAW,SAAS,SAAS,SAAS,UAAU,QAAW,QAAQ,IACnE;CACD,MAAM,sBACL,SAAS,SACN,SAAS,WAAW,OAAO,aAAa,CACzC,QAAQ,OAAO,OAAO,OAAU,IAAI,EAAE;CACzC,MAAM,kBAAkB;EACvB,IAAI;EACJ,MAAM;EACN,OAAO;GACN,WAAW,SAAS,cAAc;GAClC,SAAS,SAAS,cAAc;GAChC,WAAW,SAAS,cAAc;GAClC,YAAY,SAAS,cAAc;GACnC;EACD;CACD,MAAM,EACL,WAAW,YACX,SAAS,UACT,WAAW,YACX,YA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