better-auth
Version:
The most comprehensive authentication framework for TypeScript.
1 lines • 3.33 kB
Source Map (JSON)
{"version":3,"file":"slack.mjs","names":[],"sources":["../../../../src/plugins/generic-oauth/providers/slack.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 SlackOptions extends BaseOAuthProviderOptions {}\n\ninterface SlackProfile {\n\tsub: string;\n\t\"https://slack.com/user_id\": string;\n\t\"https://slack.com/team_id\": string;\n\temail: string;\n\temail_verified: boolean;\n\tname: string;\n\tpicture?: string;\n\tgiven_name?: string;\n\tfamily_name?: string;\n\tlocale?: string;\n\t\"https://slack.com/team_name\"?: string;\n\t\"https://slack.com/team_domain\"?: string;\n\t\"https://slack.com/user_image_512\"?: string;\n\t[key: string]: any;\n}\n\n/**\n * Slack OAuth provider helper\n *\n * @example\n * ```ts\n * import { genericOAuth, slack } from \"better-auth/plugins/generic-oauth\";\n *\n * export const auth = betterAuth({\n * plugins: [\n * genericOAuth({\n * config: [\n * slack({\n * clientId: process.env.SLACK_CLIENT_ID,\n * clientSecret: process.env.SLACK_CLIENT_SECRET,\n * }),\n * ],\n * }),\n * ],\n * });\n * ```\n */\nexport function slack(options: SlackOptions): GenericOAuthConfig {\n\tconst defaultScopes = [\"openid\", \"profile\", \"email\"];\n\n\tconst getUserInfo = async (\n\t\ttokens: OAuth2Tokens,\n\t): Promise<OAuth2UserInfo | null> => {\n\t\tconst { data: profile, error } = await betterFetch<SlackProfile>(\n\t\t\t\"https://slack.com/api/openid.connect.userInfo\",\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[\"https://slack.com/user_id\"] ?? profile.sub,\n\t\t\tname: profile.name,\n\t\t\temail: profile.email,\n\t\t\timage: profile.picture ?? profile[\"https://slack.com/user_image_512\"],\n\t\t\temailVerified: profile.email_verified ?? false,\n\t\t};\n\t};\n\n\treturn {\n\t\tproviderId: \"slack\",\n\t\tauthorizationUrl: \"https://slack.com/openid/connect/authorize\",\n\t\ttokenUrl: \"https://slack.com/api/openid.connect.token\",\n\t\tuserInfoUrl: \"https://slack.com/api/openid.connect.userInfo\",\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":";;;;;;;;;;;;;;;;;;;;;;;;AA4CA,SAAgB,MAAM,SAA2C;CAChE,MAAM,gBAAgB;EAAC;EAAU;EAAW;EAAQ;CAEpD,MAAM,cAAc,OACnB,WACoC;EACpC,MAAM,EAAE,MAAM,SAAS,UAAU,MAAM,YACtC,iDACA,EACC,SAAS,EACR,eAAe,UAAU,OAAO,eAChC,EACD,CACD;AAED,MAAI,SAAS,CAAC,QACb,QAAO;AAGR,SAAO;GACN,IAAI,QAAQ,gCAAgC,QAAQ;GACpD,MAAM,QAAQ;GACd,OAAO,QAAQ;GACf,OAAO,QAAQ,WAAW,QAAQ;GAClC,eAAe,QAAQ,kBAAkB;GACzC;;AAGF,QAAO;EACN,YAAY;EACZ,kBAAkB;EAClB,UAAU;EACV,aAAa;EACb,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"}