@better-auth/core 1.5.0-beta.4 → 1.5.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (174) hide show
  1. package/.turbo/turbo-build.log +170 -37
  2. package/dist/api/index.d.mts +188 -1
  3. package/dist/api/index.mjs +2 -1
  4. package/dist/context/endpoint-context.d.mts +19 -0
  5. package/dist/context/endpoint-context.mjs +27 -0
  6. package/dist/context/index.d.mts +3 -52
  7. package/dist/context/index.mjs +22 -1
  8. package/dist/context/request-state.d.mts +27 -0
  9. package/dist/context/request-state.mjs +45 -0
  10. package/dist/context/transaction.d.mts +16 -0
  11. package/dist/context/transaction.mjs +48 -0
  12. package/dist/db/adapter/factory.d.mts +27 -0
  13. package/dist/db/adapter/factory.mjs +738 -0
  14. package/dist/db/adapter/get-default-field-name.d.mts +18 -0
  15. package/dist/db/adapter/get-default-field-name.mjs +38 -0
  16. package/dist/db/adapter/get-default-model-name.d.mts +12 -0
  17. package/dist/db/adapter/get-default-model-name.mjs +32 -0
  18. package/dist/db/adapter/get-field-attributes.d.mts +29 -0
  19. package/dist/db/adapter/get-field-attributes.mjs +39 -0
  20. package/dist/db/adapter/get-field-name.d.mts +18 -0
  21. package/dist/db/adapter/get-field-name.mjs +33 -0
  22. package/dist/db/adapter/get-id-field.d.mts +39 -0
  23. package/dist/db/adapter/get-id-field.mjs +67 -0
  24. package/dist/db/adapter/get-model-name.d.mts +12 -0
  25. package/dist/db/adapter/get-model-name.mjs +23 -0
  26. package/dist/db/adapter/index.d.mts +513 -1
  27. package/dist/db/adapter/index.mjs +8 -970
  28. package/dist/db/adapter/types.d.mts +139 -0
  29. package/dist/db/adapter/utils.d.mts +7 -0
  30. package/dist/db/adapter/utils.mjs +38 -0
  31. package/dist/db/get-tables.d.mts +8 -0
  32. package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +1 -1
  33. package/dist/db/index.d.mts +10 -2
  34. package/dist/db/index.mjs +7 -60
  35. package/dist/db/plugin.d.mts +12 -0
  36. package/dist/db/schema/account.d.mts +26 -0
  37. package/dist/db/schema/account.mjs +19 -0
  38. package/dist/db/schema/rate-limit.d.mts +14 -0
  39. package/dist/db/schema/rate-limit.mjs +11 -0
  40. package/dist/db/schema/session.d.mts +21 -0
  41. package/dist/db/schema/session.mjs +14 -0
  42. package/dist/db/schema/shared.d.mts +10 -0
  43. package/dist/db/schema/shared.mjs +11 -0
  44. package/dist/db/schema/user.d.mts +20 -0
  45. package/dist/db/schema/user.mjs +13 -0
  46. package/dist/db/schema/verification.d.mts +19 -0
  47. package/dist/db/schema/verification.mjs +12 -0
  48. package/dist/db/type.d.mts +143 -0
  49. package/dist/env/color-depth.d.mts +4 -0
  50. package/dist/env/color-depth.mjs +88 -0
  51. package/dist/env/env-impl.d.mts +32 -0
  52. package/dist/env/env-impl.mjs +82 -0
  53. package/dist/env/index.d.mts +4 -2
  54. package/dist/env/index.mjs +3 -1
  55. package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +1 -35
  56. package/dist/env/logger.mjs +81 -0
  57. package/dist/error/codes.d.mts +186 -0
  58. package/dist/{error-GNtLPYaS.mjs → error/codes.mjs} +2 -29
  59. package/dist/error/index.d.mts +1 -185
  60. package/dist/error/index.mjs +28 -3
  61. package/dist/index.d.mts +7 -1
  62. package/dist/oauth2/client-credentials-token.d.mts +36 -0
  63. package/dist/oauth2/client-credentials-token.mjs +54 -0
  64. package/dist/oauth2/create-authorization-url.d.mts +45 -0
  65. package/dist/oauth2/create-authorization-url.mjs +42 -0
  66. package/dist/oauth2/index.d.mts +8 -2
  67. package/dist/oauth2/index.mjs +6 -2
  68. package/dist/oauth2/oauth-provider.d.mts +194 -0
  69. package/dist/oauth2/refresh-access-token.d.mts +36 -0
  70. package/dist/oauth2/refresh-access-token.mjs +58 -0
  71. package/dist/oauth2/utils.d.mts +7 -0
  72. package/dist/oauth2/utils.mjs +27 -0
  73. package/dist/oauth2/validate-authorization-code.d.mts +55 -0
  74. package/dist/oauth2/validate-authorization-code.mjs +71 -0
  75. package/dist/oauth2/verify.d.mts +49 -0
  76. package/dist/oauth2/verify.mjs +95 -0
  77. package/dist/social-providers/apple.d.mts +119 -0
  78. package/dist/social-providers/apple.mjs +102 -0
  79. package/dist/social-providers/atlassian.d.mts +72 -0
  80. package/dist/social-providers/atlassian.mjs +83 -0
  81. package/dist/social-providers/cognito.d.mts +87 -0
  82. package/dist/social-providers/cognito.mjs +165 -0
  83. package/dist/social-providers/discord.d.mts +126 -0
  84. package/dist/social-providers/discord.mjs +64 -0
  85. package/dist/social-providers/dropbox.d.mts +71 -0
  86. package/dist/social-providers/dropbox.mjs +75 -0
  87. package/dist/social-providers/facebook.d.mts +81 -0
  88. package/dist/social-providers/facebook.mjs +120 -0
  89. package/dist/social-providers/figma.d.mts +63 -0
  90. package/dist/social-providers/figma.mjs +84 -0
  91. package/dist/social-providers/github.d.mts +104 -0
  92. package/dist/social-providers/github.mjs +80 -0
  93. package/dist/social-providers/gitlab.d.mts +125 -0
  94. package/dist/social-providers/gitlab.mjs +82 -0
  95. package/dist/social-providers/google.d.mts +99 -0
  96. package/dist/social-providers/google.mjs +108 -0
  97. package/dist/social-providers/huggingface.d.mts +85 -0
  98. package/dist/social-providers/huggingface.mjs +75 -0
  99. package/dist/social-providers/index.d.mts +1723 -1
  100. package/dist/social-providers/index.mjs +33 -2569
  101. package/dist/social-providers/kakao.d.mts +163 -0
  102. package/dist/social-providers/kakao.mjs +72 -0
  103. package/dist/social-providers/kick.d.mts +75 -0
  104. package/dist/social-providers/kick.mjs +71 -0
  105. package/dist/social-providers/line.d.mts +107 -0
  106. package/dist/social-providers/line.mjs +113 -0
  107. package/dist/social-providers/linear.d.mts +70 -0
  108. package/dist/social-providers/linear.mjs +88 -0
  109. package/dist/social-providers/linkedin.d.mts +69 -0
  110. package/dist/social-providers/linkedin.mjs +76 -0
  111. package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
  112. package/dist/social-providers/microsoft-entra-id.mjs +106 -0
  113. package/dist/social-providers/naver.d.mts +104 -0
  114. package/dist/social-providers/naver.mjs +67 -0
  115. package/dist/social-providers/notion.d.mts +66 -0
  116. package/dist/social-providers/notion.mjs +75 -0
  117. package/dist/social-providers/paybin.d.mts +73 -0
  118. package/dist/social-providers/paybin.mjs +85 -0
  119. package/dist/social-providers/paypal.d.mts +131 -0
  120. package/dist/social-providers/paypal.mjs +144 -0
  121. package/dist/social-providers/polar.d.mts +76 -0
  122. package/dist/social-providers/polar.mjs +73 -0
  123. package/dist/social-providers/reddit.d.mts +64 -0
  124. package/dist/social-providers/reddit.mjs +83 -0
  125. package/dist/social-providers/roblox.d.mts +72 -0
  126. package/dist/social-providers/roblox.mjs +59 -0
  127. package/dist/social-providers/salesforce.d.mts +81 -0
  128. package/dist/social-providers/salesforce.mjs +91 -0
  129. package/dist/social-providers/slack.d.mts +85 -0
  130. package/dist/social-providers/slack.mjs +68 -0
  131. package/dist/social-providers/spotify.d.mts +65 -0
  132. package/dist/social-providers/spotify.mjs +71 -0
  133. package/dist/social-providers/tiktok.d.mts +171 -0
  134. package/dist/social-providers/tiktok.mjs +62 -0
  135. package/dist/social-providers/twitch.d.mts +81 -0
  136. package/dist/social-providers/twitch.mjs +78 -0
  137. package/dist/social-providers/twitter.d.mts +140 -0
  138. package/dist/social-providers/twitter.mjs +87 -0
  139. package/dist/social-providers/vercel.d.mts +64 -0
  140. package/dist/social-providers/vercel.mjs +61 -0
  141. package/dist/social-providers/vk.d.mts +72 -0
  142. package/dist/social-providers/vk.mjs +83 -0
  143. package/dist/social-providers/zoom.d.mts +173 -0
  144. package/dist/social-providers/zoom.mjs +72 -0
  145. package/dist/types/context.d.mts +246 -0
  146. package/dist/types/cookie.d.mts +23 -0
  147. package/dist/types/helper.d.mts +8 -0
  148. package/dist/types/index.d.mts +8 -0
  149. package/dist/types/init-options.d.mts +1266 -0
  150. package/dist/types/plugin-client.d.mts +110 -0
  151. package/dist/types/plugin.d.mts +124 -0
  152. package/dist/utils/deprecate.d.mts +10 -0
  153. package/dist/utils/deprecate.mjs +17 -0
  154. package/dist/utils/{index.d.mts → error-codes.d.mts} +1 -19
  155. package/dist/utils/error-codes.mjs +11 -0
  156. package/dist/utils/id.d.mts +4 -0
  157. package/dist/utils/id.mjs +9 -0
  158. package/dist/utils/json.d.mts +4 -0
  159. package/dist/utils/json.mjs +25 -0
  160. package/dist/utils/string.d.mts +4 -0
  161. package/dist/utils/string.mjs +7 -0
  162. package/package.json +9 -6
  163. package/src/db/adapter/get-id-field.ts +1 -1
  164. package/src/error/codes.ts +1 -1
  165. package/src/oauth2/create-authorization-url.ts +1 -1
  166. package/src/oauth2/oauth-provider.ts +6 -0
  167. package/tsdown.config.ts +3 -1
  168. package/dist/context-BBNwughv.mjs +0 -133
  169. package/dist/env-DbssmzoK.mjs +0 -245
  170. package/dist/index-B5x_W0dM.d.mts +0 -8054
  171. package/dist/oauth2-BjWM15hm.mjs +0 -326
  172. package/dist/utils/index.mjs +0 -4
  173. package/dist/utils-puAL36Bz.mjs +0 -63
  174. package/src/utils/index.ts +0 -5
@@ -0,0 +1,82 @@
1
+ import { createAuthorizationURL } from "../oauth2/create-authorization-url.mjs";
2
+ import { refreshAccessToken } from "../oauth2/refresh-access-token.mjs";
3
+ import { validateAuthorizationCode } from "../oauth2/validate-authorization-code.mjs";
4
+ import "../oauth2/index.mjs";
5
+ import { betterFetch } from "@better-fetch/fetch";
6
+
7
+ //#region src/social-providers/gitlab.ts
8
+ const cleanDoubleSlashes = (input = "") => {
9
+ return input.split("://").map((str) => str.replace(/\/{2,}/g, "/")).join("://");
10
+ };
11
+ const issuerToEndpoints = (issuer) => {
12
+ let baseUrl = issuer || "https://gitlab.com";
13
+ return {
14
+ authorizationEndpoint: cleanDoubleSlashes(`${baseUrl}/oauth/authorize`),
15
+ tokenEndpoint: cleanDoubleSlashes(`${baseUrl}/oauth/token`),
16
+ userinfoEndpoint: cleanDoubleSlashes(`${baseUrl}/api/v4/user`)
17
+ };
18
+ };
19
+ const gitlab = (options) => {
20
+ const { authorizationEndpoint, tokenEndpoint, userinfoEndpoint } = issuerToEndpoints(options.issuer);
21
+ const issuerId = "gitlab";
22
+ return {
23
+ id: issuerId,
24
+ name: "Gitlab",
25
+ createAuthorizationURL: async ({ state, scopes, codeVerifier, loginHint, redirectURI }) => {
26
+ const _scopes = options.disableDefaultScope ? [] : ["read_user"];
27
+ if (options.scope) _scopes.push(...options.scope);
28
+ if (scopes) _scopes.push(...scopes);
29
+ return await createAuthorizationURL({
30
+ id: issuerId,
31
+ options,
32
+ authorizationEndpoint,
33
+ scopes: _scopes,
34
+ state,
35
+ redirectURI,
36
+ codeVerifier,
37
+ loginHint
38
+ });
39
+ },
40
+ validateAuthorizationCode: async ({ code, redirectURI, codeVerifier }) => {
41
+ return validateAuthorizationCode({
42
+ code,
43
+ redirectURI,
44
+ options,
45
+ codeVerifier,
46
+ tokenEndpoint
47
+ });
48
+ },
49
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
50
+ return refreshAccessToken({
51
+ refreshToken,
52
+ options: {
53
+ clientId: options.clientId,
54
+ clientKey: options.clientKey,
55
+ clientSecret: options.clientSecret
56
+ },
57
+ tokenEndpoint
58
+ });
59
+ },
60
+ async getUserInfo(token) {
61
+ if (options.getUserInfo) return options.getUserInfo(token);
62
+ const { data: profile, error } = await betterFetch(userinfoEndpoint, { headers: { authorization: `Bearer ${token.accessToken}` } });
63
+ if (error || profile.state !== "active" || profile.locked) return null;
64
+ const userMap = await options.mapProfileToUser?.(profile);
65
+ return {
66
+ user: {
67
+ id: profile.id,
68
+ name: profile.name ?? profile.username,
69
+ email: profile.email,
70
+ image: profile.avatar_url,
71
+ emailVerified: profile.email_verified ?? false,
72
+ ...userMap
73
+ },
74
+ data: profile
75
+ };
76
+ },
77
+ options
78
+ };
79
+ };
80
+
81
+ //#endregion
82
+ export { gitlab };
@@ -0,0 +1,99 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "../oauth2/oauth-provider.mjs";
2
+ import "../oauth2/index.mjs";
3
+
4
+ //#region src/social-providers/google.d.ts
5
+ interface GoogleProfile {
6
+ aud: string;
7
+ azp: string;
8
+ email: string;
9
+ email_verified: boolean;
10
+ exp: number;
11
+ /**
12
+ * The family name of the user, or last name in most
13
+ * Western languages.
14
+ */
15
+ family_name: string;
16
+ /**
17
+ * The given name of the user, or first name in most
18
+ * Western languages.
19
+ */
20
+ given_name: string;
21
+ hd?: string | undefined;
22
+ iat: number;
23
+ iss: string;
24
+ jti?: string | undefined;
25
+ locale?: string | undefined;
26
+ name: string;
27
+ nbf?: number | undefined;
28
+ picture: string;
29
+ sub: string;
30
+ }
31
+ interface GoogleOptions extends ProviderOptions<GoogleProfile> {
32
+ clientId: string;
33
+ /**
34
+ * The access type to use for the authorization code request
35
+ */
36
+ accessType?: ("offline" | "online") | undefined;
37
+ /**
38
+ * The display mode to use for the authorization code request
39
+ */
40
+ display?: ("page" | "popup" | "touch" | "wap") | undefined;
41
+ /**
42
+ * The hosted domain of the user
43
+ */
44
+ hd?: string | undefined;
45
+ }
46
+ declare const google: (options: GoogleOptions) => {
47
+ id: "google";
48
+ name: string;
49
+ createAuthorizationURL({
50
+ state,
51
+ scopes,
52
+ codeVerifier,
53
+ redirectURI,
54
+ loginHint,
55
+ display
56
+ }: {
57
+ state: string;
58
+ codeVerifier: string;
59
+ scopes?: string[] | undefined;
60
+ redirectURI: string;
61
+ display?: string | undefined;
62
+ loginHint?: string | undefined;
63
+ }): Promise<URL>;
64
+ validateAuthorizationCode: ({
65
+ code,
66
+ codeVerifier,
67
+ redirectURI
68
+ }: {
69
+ code: string;
70
+ redirectURI: string;
71
+ codeVerifier?: string | undefined;
72
+ deviceId?: string | undefined;
73
+ }) => Promise<OAuth2Tokens>;
74
+ refreshAccessToken: (refreshToken: string) => Promise<OAuth2Tokens>;
75
+ verifyIdToken(token: string, nonce: string | undefined): Promise<boolean>;
76
+ getUserInfo(token: OAuth2Tokens & {
77
+ user?: {
78
+ name?: {
79
+ firstName?: string;
80
+ lastName?: string;
81
+ };
82
+ email?: string;
83
+ } | undefined;
84
+ }): Promise<{
85
+ user: {
86
+ id: string;
87
+ name?: string;
88
+ email?: string | null;
89
+ image?: string;
90
+ emailVerified: boolean;
91
+ [key: string]: any;
92
+ };
93
+ data: any;
94
+ } | null>;
95
+ options: GoogleOptions;
96
+ };
97
+ declare const getGooglePublicKey: (kid: string) => Promise<Uint8Array<ArrayBufferLike> | CryptoKey>;
98
+ //#endregion
99
+ export { GoogleOptions, GoogleProfile, getGooglePublicKey, google };
@@ -0,0 +1,108 @@
1
+ import { logger } from "../env/logger.mjs";
2
+ import "../env/index.mjs";
3
+ import { APIError, BetterAuthError } from "../error/index.mjs";
4
+ import { createAuthorizationURL } from "../oauth2/create-authorization-url.mjs";
5
+ import { refreshAccessToken } from "../oauth2/refresh-access-token.mjs";
6
+ import { validateAuthorizationCode } from "../oauth2/validate-authorization-code.mjs";
7
+ import "../oauth2/index.mjs";
8
+ import { betterFetch } from "@better-fetch/fetch";
9
+ import { decodeJwt, decodeProtectedHeader, importJWK, jwtVerify } from "jose";
10
+
11
+ //#region src/social-providers/google.ts
12
+ const google = (options) => {
13
+ return {
14
+ id: "google",
15
+ name: "Google",
16
+ async createAuthorizationURL({ state, scopes, codeVerifier, redirectURI, loginHint, display }) {
17
+ if (!options.clientId || !options.clientSecret) {
18
+ logger.error("Client Id and Client Secret is required for Google. Make sure to provide them in the options.");
19
+ throw new BetterAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
20
+ }
21
+ if (!codeVerifier) throw new BetterAuthError("codeVerifier is required for Google");
22
+ const _scopes = options.disableDefaultScope ? [] : [
23
+ "email",
24
+ "profile",
25
+ "openid"
26
+ ];
27
+ if (options.scope) _scopes.push(...options.scope);
28
+ if (scopes) _scopes.push(...scopes);
29
+ return await createAuthorizationURL({
30
+ id: "google",
31
+ options,
32
+ authorizationEndpoint: "https://accounts.google.com/o/oauth2/auth",
33
+ scopes: _scopes,
34
+ state,
35
+ codeVerifier,
36
+ redirectURI,
37
+ prompt: options.prompt,
38
+ accessType: options.accessType,
39
+ display: display || options.display,
40
+ loginHint,
41
+ hd: options.hd,
42
+ additionalParams: { include_granted_scopes: "true" }
43
+ });
44
+ },
45
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
46
+ return validateAuthorizationCode({
47
+ code,
48
+ codeVerifier,
49
+ redirectURI,
50
+ options,
51
+ tokenEndpoint: "https://oauth2.googleapis.com/token"
52
+ });
53
+ },
54
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
55
+ return refreshAccessToken({
56
+ refreshToken,
57
+ options: {
58
+ clientId: options.clientId,
59
+ clientKey: options.clientKey,
60
+ clientSecret: options.clientSecret
61
+ },
62
+ tokenEndpoint: "https://www.googleapis.com/oauth2/v4/token"
63
+ });
64
+ },
65
+ async verifyIdToken(token, nonce) {
66
+ if (options.disableIdTokenSignIn) return false;
67
+ if (options.verifyIdToken) return options.verifyIdToken(token, nonce);
68
+ const { kid, alg: jwtAlg } = decodeProtectedHeader(token);
69
+ if (!kid || !jwtAlg) return false;
70
+ const { payload: jwtClaims } = await jwtVerify(token, await getGooglePublicKey(kid), {
71
+ algorithms: [jwtAlg],
72
+ issuer: ["https://accounts.google.com", "accounts.google.com"],
73
+ audience: options.clientId,
74
+ maxTokenAge: "1h"
75
+ });
76
+ if (nonce && jwtClaims.nonce !== nonce) return false;
77
+ return true;
78
+ },
79
+ async getUserInfo(token) {
80
+ if (options.getUserInfo) return options.getUserInfo(token);
81
+ if (!token.idToken) return null;
82
+ const user = decodeJwt(token.idToken);
83
+ const userMap = await options.mapProfileToUser?.(user);
84
+ return {
85
+ user: {
86
+ id: user.sub,
87
+ name: user.name,
88
+ email: user.email,
89
+ image: user.picture,
90
+ emailVerified: user.email_verified,
91
+ ...userMap
92
+ },
93
+ data: user
94
+ };
95
+ },
96
+ options
97
+ };
98
+ };
99
+ const getGooglePublicKey = async (kid) => {
100
+ const { data } = await betterFetch("https://www.googleapis.com/oauth2/v3/certs");
101
+ if (!data?.keys) throw new APIError("BAD_REQUEST", { message: "Keys not found" });
102
+ const jwk = data.keys.find((key) => key.kid === kid);
103
+ if (!jwk) throw new Error(`JWK with kid ${kid} not found`);
104
+ return await importJWK(jwk, jwk.alg);
105
+ };
106
+
107
+ //#endregion
108
+ export { getGooglePublicKey, google };
@@ -0,0 +1,85 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "../oauth2/oauth-provider.mjs";
2
+ import "../oauth2/index.mjs";
3
+
4
+ //#region src/social-providers/huggingface.d.ts
5
+ interface HuggingFaceProfile {
6
+ sub: string;
7
+ name: string;
8
+ preferred_username: string;
9
+ profile: string;
10
+ picture: string;
11
+ website?: string | undefined;
12
+ email?: string | undefined;
13
+ email_verified?: boolean | undefined;
14
+ isPro: boolean;
15
+ canPay?: boolean | undefined;
16
+ orgs?: {
17
+ sub: string;
18
+ name: string;
19
+ picture: string;
20
+ preferred_username: string;
21
+ isEnterprise: boolean | "plus";
22
+ canPay?: boolean;
23
+ roleInOrg?: "admin" | "write" | "contributor" | "read";
24
+ pendingSSO?: boolean;
25
+ missingMFA?: boolean;
26
+ resourceGroups?: {
27
+ sub: string;
28
+ name: string;
29
+ role: "admin" | "write" | "contributor" | "read";
30
+ }[];
31
+ } | undefined;
32
+ }
33
+ interface HuggingFaceOptions extends ProviderOptions<HuggingFaceProfile> {
34
+ clientId: string;
35
+ }
36
+ declare const huggingface: (options: HuggingFaceOptions) => {
37
+ id: "huggingface";
38
+ name: string;
39
+ createAuthorizationURL({
40
+ state,
41
+ scopes,
42
+ codeVerifier,
43
+ redirectURI
44
+ }: {
45
+ state: string;
46
+ codeVerifier: string;
47
+ scopes?: string[] | undefined;
48
+ redirectURI: string;
49
+ display?: string | undefined;
50
+ loginHint?: string | undefined;
51
+ }): Promise<URL>;
52
+ validateAuthorizationCode: ({
53
+ code,
54
+ codeVerifier,
55
+ redirectURI
56
+ }: {
57
+ code: string;
58
+ redirectURI: string;
59
+ codeVerifier?: string | undefined;
60
+ deviceId?: string | undefined;
61
+ }) => Promise<OAuth2Tokens>;
62
+ refreshAccessToken: (refreshToken: string) => Promise<OAuth2Tokens>;
63
+ getUserInfo(token: OAuth2Tokens & {
64
+ user?: {
65
+ name?: {
66
+ firstName?: string;
67
+ lastName?: string;
68
+ };
69
+ email?: string;
70
+ } | undefined;
71
+ }): Promise<{
72
+ user: {
73
+ id: string;
74
+ name?: string;
75
+ email?: string | null;
76
+ image?: string;
77
+ emailVerified: boolean;
78
+ [key: string]: any;
79
+ };
80
+ data: any;
81
+ } | null>;
82
+ options: HuggingFaceOptions;
83
+ };
84
+ //#endregion
85
+ export { HuggingFaceOptions, HuggingFaceProfile, huggingface };
@@ -0,0 +1,75 @@
1
+ import { createAuthorizationURL } from "../oauth2/create-authorization-url.mjs";
2
+ import { refreshAccessToken } from "../oauth2/refresh-access-token.mjs";
3
+ import { validateAuthorizationCode } from "../oauth2/validate-authorization-code.mjs";
4
+ import "../oauth2/index.mjs";
5
+ import { betterFetch } from "@better-fetch/fetch";
6
+
7
+ //#region src/social-providers/huggingface.ts
8
+ const huggingface = (options) => {
9
+ return {
10
+ id: "huggingface",
11
+ name: "Hugging Face",
12
+ createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
13
+ const _scopes = options.disableDefaultScope ? [] : [
14
+ "openid",
15
+ "profile",
16
+ "email"
17
+ ];
18
+ if (options.scope) _scopes.push(...options.scope);
19
+ if (scopes) _scopes.push(...scopes);
20
+ return createAuthorizationURL({
21
+ id: "huggingface",
22
+ options,
23
+ authorizationEndpoint: "https://huggingface.co/oauth/authorize",
24
+ scopes: _scopes,
25
+ state,
26
+ codeVerifier,
27
+ redirectURI
28
+ });
29
+ },
30
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
31
+ return validateAuthorizationCode({
32
+ code,
33
+ codeVerifier,
34
+ redirectURI,
35
+ options,
36
+ tokenEndpoint: "https://huggingface.co/oauth/token"
37
+ });
38
+ },
39
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
40
+ return refreshAccessToken({
41
+ refreshToken,
42
+ options: {
43
+ clientId: options.clientId,
44
+ clientKey: options.clientKey,
45
+ clientSecret: options.clientSecret
46
+ },
47
+ tokenEndpoint: "https://huggingface.co/oauth/token"
48
+ });
49
+ },
50
+ async getUserInfo(token) {
51
+ if (options.getUserInfo) return options.getUserInfo(token);
52
+ const { data: profile, error } = await betterFetch("https://huggingface.co/oauth/userinfo", {
53
+ method: "GET",
54
+ headers: { Authorization: `Bearer ${token.accessToken}` }
55
+ });
56
+ if (error) return null;
57
+ const userMap = await options.mapProfileToUser?.(profile);
58
+ return {
59
+ user: {
60
+ id: profile.sub,
61
+ name: profile.name || profile.preferred_username,
62
+ email: profile.email,
63
+ image: profile.picture,
64
+ emailVerified: profile.email_verified ?? false,
65
+ ...userMap
66
+ },
67
+ data: profile
68
+ };
69
+ },
70
+ options
71
+ };
72
+ };
73
+
74
+ //#endregion
75
+ export { huggingface };