better-auth
Version:
The most comprehensive authentication framework for TypeScript.
1 lines • 3.3 kB
Source Map (JSON)
{"version":3,"file":"auth0.mjs","names":[],"sources":["../../../../src/plugins/generic-oauth/providers/auth0.ts"],"sourcesContent":["import type { OAuth2Tokens, OAuth2UserInfo } from \"@better-auth/core/oauth2\";\nimport { betterFetch } from \"@better-fetch/fetch\";\nimport type { BaseOAuthProviderOptions, GenericOAuthConfig } from \"../index\";\n\nexport interface Auth0Options extends BaseOAuthProviderOptions {\n\t/**\n\t * Auth0 domain (e.g., dev-xxx.eu.auth0.com)\n\t * This will be used to construct the discovery URL.\n\t */\n\tdomain: string;\n}\n\ninterface Auth0Profile {\n\tsub: string;\n\tname?: string;\n\temail?: string;\n\temail_verified?: boolean;\n\tpicture?: string;\n\tnickname?: string;\n\tgiven_name?: string;\n\tfamily_name?: string;\n}\n\n/**\n * Auth0 OAuth provider helper\n *\n * @example\n * ```ts\n * import { genericOAuth, auth0 } from \"better-auth/plugins/generic-oauth\";\n *\n * export const auth = betterAuth({\n * plugins: [\n * genericOAuth({\n * config: [\n * auth0({\n * clientId: process.env.AUTH0_CLIENT_ID,\n * clientSecret: process.env.AUTH0_CLIENT_SECRET,\n * domain: process.env.AUTH0_DOMAIN,\n * }),\n * ],\n * }),\n * ],\n * });\n * ```\n */\nexport function auth0(options: Auth0Options): GenericOAuthConfig {\n\tconst defaultScopes = [\"openid\", \"profile\", \"email\"];\n\n\t// Ensure domain doesn't have protocol prefix\n\tconst domain = options.domain.replace(/^https?:\\/\\//, \"\");\n\tconst discoveryUrl = `https://${domain}/.well-known/openid-configuration`;\n\n\tconst getUserInfo = async (\n\t\ttokens: OAuth2Tokens,\n\t): Promise<OAuth2UserInfo | null> => {\n\t\tconst userInfoUrl = `https://${domain}/userinfo`;\n\n\t\tconst { data: profile, error } = await betterFetch<Auth0Profile>(\n\t\t\tuserInfoUrl,\n\t\t\t{\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${tokens.accessToken}`,\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (error || !profile) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn {\n\t\t\tid: profile.sub,\n\t\t\tname: profile.name ?? profile.nickname ?? undefined,\n\t\t\temail: profile.email ?? undefined,\n\t\t\timage: profile.picture,\n\t\t\temailVerified: profile.email_verified ?? false,\n\t\t};\n\t};\n\n\treturn {\n\t\tproviderId: \"auth0\",\n\t\tdiscoveryUrl,\n\t\tclientId: options.clientId,\n\t\tclientSecret: options.clientSecret,\n\t\tscopes: options.scopes ?? defaultScopes,\n\t\tredirectURI: options.redirectURI,\n\t\tpkce: options.pkce,\n\t\tdisableImplicitSignUp: options.disableImplicitSignUp,\n\t\tdisableSignUp: options.disableSignUp,\n\t\toverrideUserInfo: options.overrideUserInfo,\n\t\tgetUserInfo,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,SAAgB,MAAM,SAA2C;CAChE,MAAM,gBAAgB;EAAC;EAAU;EAAW;EAAQ;CAGpD,MAAM,SAAS,QAAQ,OAAO,QAAQ,gBAAgB,GAAG;CACzD,MAAM,eAAe,WAAW,OAAO;CAEvC,MAAM,cAAc,OACnB,WACoC;EAGpC,MAAM,EAAE,MAAM,SAAS,UAAU,MAAM,YAFnB,WAAW,OAAO,YAIrC,EACC,SAAS,EACR,eAAe,UAAU,OAAO,eAChC,EACD,CACD;AAED,MAAI,SAAS,CAAC,QACb,QAAO;AAGR,SAAO;GACN,IAAI,QAAQ;GACZ,MAAM,QAAQ,QAAQ,QAAQ,YAAY;GAC1C,OAAO,QAAQ,SAAS;GACxB,OAAO,QAAQ;GACf,eAAe,QAAQ,kBAAkB;GACzC;;AAGF,QAAO;EACN,YAAY;EACZ;EACA,UAAU,QAAQ;EAClB,cAAc,QAAQ;EACtB,QAAQ,QAAQ,UAAU;EAC1B,aAAa,QAAQ;EACrB,MAAM,QAAQ;EACd,uBAAuB,QAAQ;EAC/B,eAAe,QAAQ;EACvB,kBAAkB,QAAQ;EAC1B;EACA"}