@better-auth/core 1.4.12-beta.2 → 1.4.13

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 (185) hide show
  1. package/.turbo/turbo-build.log +172 -35
  2. package/dist/api/index.d.mts +178 -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 +31 -0
  6. package/dist/context/global.d.mts +7 -0
  7. package/dist/context/global.mjs +37 -0
  8. package/dist/context/index.d.mts +5 -53
  9. package/dist/context/index.mjs +5 -2
  10. package/dist/context/request-state.d.mts +27 -0
  11. package/dist/context/request-state.mjs +49 -0
  12. package/dist/context/transaction.d.mts +16 -0
  13. package/dist/context/transaction.mjs +52 -0
  14. package/dist/db/adapter/factory.d.mts +27 -0
  15. package/dist/db/adapter/factory.mjs +738 -0
  16. package/dist/db/adapter/get-default-field-name.d.mts +18 -0
  17. package/dist/db/adapter/get-default-field-name.mjs +38 -0
  18. package/dist/db/adapter/get-default-model-name.d.mts +12 -0
  19. package/dist/db/adapter/get-default-model-name.mjs +32 -0
  20. package/dist/db/adapter/get-field-attributes.d.mts +29 -0
  21. package/dist/db/adapter/get-field-attributes.mjs +39 -0
  22. package/dist/db/adapter/get-field-name.d.mts +18 -0
  23. package/dist/db/adapter/get-field-name.mjs +33 -0
  24. package/dist/db/adapter/get-id-field.d.mts +39 -0
  25. package/dist/db/adapter/get-id-field.mjs +68 -0
  26. package/dist/db/adapter/get-model-name.d.mts +12 -0
  27. package/dist/db/adapter/get-model-name.mjs +23 -0
  28. package/dist/db/adapter/index.d.mts +513 -1
  29. package/dist/db/adapter/index.mjs +8 -970
  30. package/dist/db/adapter/types.d.mts +139 -0
  31. package/dist/db/adapter/utils.d.mts +7 -0
  32. package/dist/db/adapter/utils.mjs +38 -0
  33. package/dist/db/get-tables.d.mts +8 -0
  34. package/dist/{get-tables-CMc_Emww.mjs → db/get-tables.mjs} +1 -1
  35. package/dist/db/index.d.mts +10 -2
  36. package/dist/db/index.mjs +7 -60
  37. package/dist/db/plugin.d.mts +12 -0
  38. package/dist/db/schema/account.d.mts +26 -0
  39. package/dist/db/schema/account.mjs +19 -0
  40. package/dist/db/schema/rate-limit.d.mts +14 -0
  41. package/dist/db/schema/rate-limit.mjs +11 -0
  42. package/dist/db/schema/session.d.mts +21 -0
  43. package/dist/db/schema/session.mjs +14 -0
  44. package/dist/db/schema/shared.d.mts +10 -0
  45. package/dist/db/schema/shared.mjs +11 -0
  46. package/dist/db/schema/user.d.mts +20 -0
  47. package/dist/db/schema/user.mjs +13 -0
  48. package/dist/db/schema/verification.d.mts +19 -0
  49. package/dist/db/schema/verification.mjs +12 -0
  50. package/dist/db/type.d.mts +143 -0
  51. package/dist/env/color-depth.d.mts +4 -0
  52. package/dist/env/color-depth.mjs +88 -0
  53. package/dist/env/env-impl.d.mts +32 -0
  54. package/dist/env/env-impl.mjs +82 -0
  55. package/dist/env/index.d.mts +4 -2
  56. package/dist/env/index.mjs +3 -1
  57. package/dist/{index-BRBu0-5h.d.mts → env/logger.d.mts} +1 -35
  58. package/dist/env/logger.mjs +81 -0
  59. package/dist/error/codes.d.mts +48 -0
  60. package/dist/{error-DP1xOn7P.mjs → error/codes.mjs} +3 -14
  61. package/dist/error/index.d.mts +5 -48
  62. package/dist/error/index.mjs +12 -3
  63. package/dist/index.d.mts +8 -2
  64. package/dist/oauth2/client-credentials-token.d.mts +36 -0
  65. package/dist/oauth2/client-credentials-token.mjs +54 -0
  66. package/dist/oauth2/create-authorization-url.d.mts +45 -0
  67. package/dist/oauth2/create-authorization-url.mjs +42 -0
  68. package/dist/oauth2/index.d.mts +8 -2
  69. package/dist/oauth2/index.mjs +6 -2
  70. package/dist/oauth2/oauth-provider.d.mts +194 -0
  71. package/dist/oauth2/refresh-access-token.d.mts +36 -0
  72. package/dist/oauth2/refresh-access-token.mjs +58 -0
  73. package/dist/oauth2/utils.d.mts +7 -0
  74. package/dist/oauth2/utils.mjs +27 -0
  75. package/dist/oauth2/validate-authorization-code.d.mts +55 -0
  76. package/dist/oauth2/validate-authorization-code.mjs +71 -0
  77. package/dist/oauth2/verify.d.mts +49 -0
  78. package/dist/oauth2/verify.mjs +95 -0
  79. package/dist/social-providers/apple.d.mts +119 -0
  80. package/dist/social-providers/apple.mjs +102 -0
  81. package/dist/social-providers/atlassian.d.mts +72 -0
  82. package/dist/social-providers/atlassian.mjs +83 -0
  83. package/dist/social-providers/cognito.d.mts +87 -0
  84. package/dist/social-providers/cognito.mjs +166 -0
  85. package/dist/social-providers/discord.d.mts +126 -0
  86. package/dist/social-providers/discord.mjs +64 -0
  87. package/dist/social-providers/dropbox.d.mts +71 -0
  88. package/dist/social-providers/dropbox.mjs +75 -0
  89. package/dist/social-providers/facebook.d.mts +81 -0
  90. package/dist/social-providers/facebook.mjs +120 -0
  91. package/dist/social-providers/figma.d.mts +63 -0
  92. package/dist/social-providers/figma.mjs +84 -0
  93. package/dist/social-providers/github.d.mts +104 -0
  94. package/dist/social-providers/github.mjs +80 -0
  95. package/dist/social-providers/gitlab.d.mts +125 -0
  96. package/dist/social-providers/gitlab.mjs +82 -0
  97. package/dist/social-providers/google.d.mts +99 -0
  98. package/dist/social-providers/google.mjs +109 -0
  99. package/dist/social-providers/huggingface.d.mts +85 -0
  100. package/dist/social-providers/huggingface.mjs +75 -0
  101. package/dist/social-providers/index.d.mts +1723 -1
  102. package/dist/social-providers/index.mjs +33 -2570
  103. package/dist/social-providers/kakao.d.mts +163 -0
  104. package/dist/social-providers/kakao.mjs +72 -0
  105. package/dist/social-providers/kick.d.mts +75 -0
  106. package/dist/social-providers/kick.mjs +71 -0
  107. package/dist/social-providers/line.d.mts +107 -0
  108. package/dist/social-providers/line.mjs +113 -0
  109. package/dist/social-providers/linear.d.mts +70 -0
  110. package/dist/social-providers/linear.mjs +88 -0
  111. package/dist/social-providers/linkedin.d.mts +69 -0
  112. package/dist/social-providers/linkedin.mjs +76 -0
  113. package/dist/social-providers/microsoft-entra-id.d.mts +174 -0
  114. package/dist/social-providers/microsoft-entra-id.mjs +106 -0
  115. package/dist/social-providers/naver.d.mts +104 -0
  116. package/dist/social-providers/naver.mjs +67 -0
  117. package/dist/social-providers/notion.d.mts +66 -0
  118. package/dist/social-providers/notion.mjs +75 -0
  119. package/dist/social-providers/paybin.d.mts +73 -0
  120. package/dist/social-providers/paybin.mjs +85 -0
  121. package/dist/social-providers/paypal.d.mts +131 -0
  122. package/dist/social-providers/paypal.mjs +144 -0
  123. package/dist/social-providers/polar.d.mts +76 -0
  124. package/dist/social-providers/polar.mjs +73 -0
  125. package/dist/social-providers/reddit.d.mts +64 -0
  126. package/dist/social-providers/reddit.mjs +83 -0
  127. package/dist/social-providers/roblox.d.mts +72 -0
  128. package/dist/social-providers/roblox.mjs +59 -0
  129. package/dist/social-providers/salesforce.d.mts +81 -0
  130. package/dist/social-providers/salesforce.mjs +91 -0
  131. package/dist/social-providers/slack.d.mts +85 -0
  132. package/dist/social-providers/slack.mjs +68 -0
  133. package/dist/social-providers/spotify.d.mts +65 -0
  134. package/dist/social-providers/spotify.mjs +71 -0
  135. package/dist/social-providers/tiktok.d.mts +171 -0
  136. package/dist/social-providers/tiktok.mjs +62 -0
  137. package/dist/social-providers/twitch.d.mts +81 -0
  138. package/dist/social-providers/twitch.mjs +78 -0
  139. package/dist/social-providers/twitter.d.mts +140 -0
  140. package/dist/social-providers/twitter.mjs +87 -0
  141. package/dist/social-providers/vercel.d.mts +64 -0
  142. package/dist/social-providers/vercel.mjs +61 -0
  143. package/dist/social-providers/vk.d.mts +72 -0
  144. package/dist/social-providers/vk.mjs +83 -0
  145. package/dist/social-providers/zoom.d.mts +173 -0
  146. package/dist/social-providers/zoom.mjs +72 -0
  147. package/dist/types/context.d.mts +215 -0
  148. package/dist/types/cookie.d.mts +15 -0
  149. package/dist/types/helper.d.mts +8 -0
  150. package/dist/types/index.d.mts +8 -0
  151. package/dist/types/init-options.d.mts +1266 -0
  152. package/dist/types/plugin-client.d.mts +103 -0
  153. package/dist/types/plugin.d.mts +121 -0
  154. package/dist/utils/deprecate.d.mts +10 -0
  155. package/dist/utils/deprecate.mjs +17 -0
  156. package/dist/utils/error-codes.d.mts +9 -0
  157. package/dist/utils/error-codes.mjs +7 -0
  158. package/dist/utils/id.d.mts +4 -0
  159. package/dist/utils/id.mjs +9 -0
  160. package/dist/utils/index.d.mts +5 -26
  161. package/dist/utils/index.mjs +5 -2
  162. package/dist/utils/json.d.mts +4 -0
  163. package/dist/utils/json.mjs +25 -0
  164. package/dist/utils/string.d.mts +4 -0
  165. package/dist/utils/string.mjs +7 -0
  166. package/package.json +1 -1
  167. package/src/context/endpoint-context.ts +7 -15
  168. package/src/context/global.ts +57 -0
  169. package/src/context/index.ts +1 -0
  170. package/src/context/request-state.ts +7 -12
  171. package/src/context/transaction.ts +7 -16
  172. package/src/db/adapter/factory.ts +13 -13
  173. package/src/db/adapter/get-default-model-name.ts +1 -1
  174. package/src/db/adapter/get-id-field.ts +2 -2
  175. package/src/error/index.ts +2 -3
  176. package/src/social-providers/gitlab.ts +1 -1
  177. package/src/types/context.ts +137 -131
  178. package/src/types/cookie.ts +6 -4
  179. package/src/types/index.ts +2 -1
  180. package/tsdown.config.ts +9 -0
  181. package/dist/context-BGZ8V6DD.mjs +0 -126
  182. package/dist/env-DbssmzoK.mjs +0 -245
  183. package/dist/index-zgYuzZ7O.d.mts +0 -8020
  184. package/dist/oauth2-COJkghlT.mjs +0 -326
  185. package/dist/utils-U2L7n92V.mjs +0 -59
@@ -0,0 +1,104 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "../oauth2/oauth-provider.mjs";
2
+ import "../oauth2/index.mjs";
3
+
4
+ //#region src/social-providers/github.d.ts
5
+ interface GithubProfile {
6
+ login: string;
7
+ id: string;
8
+ node_id: string;
9
+ avatar_url: string;
10
+ gravatar_id: string;
11
+ url: string;
12
+ html_url: string;
13
+ followers_url: string;
14
+ following_url: string;
15
+ gists_url: string;
16
+ starred_url: string;
17
+ subscriptions_url: string;
18
+ organizations_url: string;
19
+ repos_url: string;
20
+ events_url: string;
21
+ received_events_url: string;
22
+ type: string;
23
+ site_admin: boolean;
24
+ name: string;
25
+ company: string;
26
+ blog: string;
27
+ location: string;
28
+ email: string;
29
+ hireable: boolean;
30
+ bio: string;
31
+ twitter_username: string;
32
+ public_repos: string;
33
+ public_gists: string;
34
+ followers: string;
35
+ following: string;
36
+ created_at: string;
37
+ updated_at: string;
38
+ private_gists: string;
39
+ total_private_repos: string;
40
+ owned_private_repos: string;
41
+ disk_usage: string;
42
+ collaborators: string;
43
+ two_factor_authentication: boolean;
44
+ plan: {
45
+ name: string;
46
+ space: string;
47
+ private_repos: string;
48
+ collaborators: string;
49
+ };
50
+ }
51
+ interface GithubOptions extends ProviderOptions<GithubProfile> {
52
+ clientId: string;
53
+ }
54
+ declare const github: (options: GithubOptions) => {
55
+ id: "github";
56
+ name: string;
57
+ createAuthorizationURL({
58
+ state,
59
+ scopes,
60
+ loginHint,
61
+ codeVerifier,
62
+ redirectURI
63
+ }: {
64
+ state: string;
65
+ codeVerifier: string;
66
+ scopes?: string[] | undefined;
67
+ redirectURI: string;
68
+ display?: string | undefined;
69
+ loginHint?: string | undefined;
70
+ }): Promise<URL>;
71
+ validateAuthorizationCode: ({
72
+ code,
73
+ codeVerifier,
74
+ redirectURI
75
+ }: {
76
+ code: string;
77
+ redirectURI: string;
78
+ codeVerifier?: string | undefined;
79
+ deviceId?: string | undefined;
80
+ }) => Promise<OAuth2Tokens>;
81
+ refreshAccessToken: (refreshToken: string) => Promise<OAuth2Tokens>;
82
+ getUserInfo(token: OAuth2Tokens & {
83
+ user?: {
84
+ name?: {
85
+ firstName?: string;
86
+ lastName?: string;
87
+ };
88
+ email?: string;
89
+ } | undefined;
90
+ }): Promise<{
91
+ user: {
92
+ id: string;
93
+ name?: string;
94
+ email?: string | null;
95
+ image?: string;
96
+ emailVerified: boolean;
97
+ [key: string]: any;
98
+ };
99
+ data: any;
100
+ } | null>;
101
+ options: GithubOptions;
102
+ };
103
+ //#endregion
104
+ export { GithubOptions, GithubProfile, github };
@@ -0,0 +1,80 @@
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/github.ts
8
+ const github = (options) => {
9
+ const tokenEndpoint = "https://github.com/login/oauth/access_token";
10
+ return {
11
+ id: "github",
12
+ name: "GitHub",
13
+ createAuthorizationURL({ state, scopes, loginHint, codeVerifier, redirectURI }) {
14
+ const _scopes = options.disableDefaultScope ? [] : ["read:user", "user:email"];
15
+ if (options.scope) _scopes.push(...options.scope);
16
+ if (scopes) _scopes.push(...scopes);
17
+ return createAuthorizationURL({
18
+ id: "github",
19
+ options,
20
+ authorizationEndpoint: "https://github.com/login/oauth/authorize",
21
+ scopes: _scopes,
22
+ state,
23
+ codeVerifier,
24
+ redirectURI,
25
+ loginHint,
26
+ prompt: options.prompt
27
+ });
28
+ },
29
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
30
+ return validateAuthorizationCode({
31
+ code,
32
+ codeVerifier,
33
+ redirectURI,
34
+ options,
35
+ tokenEndpoint
36
+ });
37
+ },
38
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
39
+ return refreshAccessToken({
40
+ refreshToken,
41
+ options: {
42
+ clientId: options.clientId,
43
+ clientKey: options.clientKey,
44
+ clientSecret: options.clientSecret
45
+ },
46
+ tokenEndpoint: "https://github.com/login/oauth/access_token"
47
+ });
48
+ },
49
+ async getUserInfo(token) {
50
+ if (options.getUserInfo) return options.getUserInfo(token);
51
+ const { data: profile, error } = await betterFetch("https://api.github.com/user", { headers: {
52
+ "User-Agent": "better-auth",
53
+ authorization: `Bearer ${token.accessToken}`
54
+ } });
55
+ if (error) return null;
56
+ const { data: emails } = await betterFetch("https://api.github.com/user/emails", { headers: {
57
+ Authorization: `Bearer ${token.accessToken}`,
58
+ "User-Agent": "better-auth"
59
+ } });
60
+ if (!profile.email && emails) profile.email = (emails.find((e) => e.primary) ?? emails[0])?.email;
61
+ const emailVerified = emails?.find((e) => e.email === profile.email)?.verified ?? false;
62
+ const userMap = await options.mapProfileToUser?.(profile);
63
+ return {
64
+ user: {
65
+ id: profile.id,
66
+ name: profile.name || profile.login,
67
+ email: profile.email,
68
+ image: profile.avatar_url,
69
+ emailVerified,
70
+ ...userMap
71
+ },
72
+ data: profile
73
+ };
74
+ },
75
+ options
76
+ };
77
+ };
78
+
79
+ //#endregion
80
+ export { github };
@@ -0,0 +1,125 @@
1
+ import { OAuth2Tokens, ProviderOptions } from "../oauth2/oauth-provider.mjs";
2
+ import "../oauth2/index.mjs";
3
+
4
+ //#region src/social-providers/gitlab.d.ts
5
+ interface GitlabProfile extends Record<string, any> {
6
+ id: number;
7
+ username: string;
8
+ email: string;
9
+ name: string;
10
+ state: string;
11
+ avatar_url: string;
12
+ web_url: string;
13
+ created_at: string;
14
+ bio: string;
15
+ location?: string | undefined;
16
+ public_email: string;
17
+ skype: string;
18
+ linkedin: string;
19
+ twitter: string;
20
+ website_url: string;
21
+ organization: string;
22
+ job_title: string;
23
+ pronouns: string;
24
+ bot: boolean;
25
+ work_information?: string | undefined;
26
+ followers: number;
27
+ following: number;
28
+ local_time: string;
29
+ last_sign_in_at: string;
30
+ confirmed_at: string;
31
+ theme_id: number;
32
+ last_activity_on: string;
33
+ color_scheme_id: number;
34
+ projects_limit: number;
35
+ current_sign_in_at: string;
36
+ identities: Array<{
37
+ provider: string;
38
+ extern_uid: string;
39
+ }>;
40
+ can_create_group: boolean;
41
+ can_create_project: boolean;
42
+ two_factor_enabled: boolean;
43
+ external: boolean;
44
+ private_profile: boolean;
45
+ commit_email: string;
46
+ shared_runners_minutes_limit: number;
47
+ extra_shared_runners_minutes_limit: number;
48
+ email_verified?: boolean | undefined;
49
+ }
50
+ interface GitlabOptions extends ProviderOptions<GitlabProfile> {
51
+ clientId: string;
52
+ issuer?: string | undefined;
53
+ }
54
+ declare const gitlab: (options: GitlabOptions) => {
55
+ id: "gitlab";
56
+ name: string;
57
+ createAuthorizationURL: ({
58
+ state,
59
+ scopes,
60
+ codeVerifier,
61
+ loginHint,
62
+ redirectURI
63
+ }: {
64
+ state: string;
65
+ codeVerifier: string;
66
+ scopes?: string[] | undefined;
67
+ redirectURI: string;
68
+ display?: string | undefined;
69
+ loginHint?: string | undefined;
70
+ }) => Promise<URL>;
71
+ validateAuthorizationCode: ({
72
+ code,
73
+ redirectURI,
74
+ codeVerifier
75
+ }: {
76
+ code: string;
77
+ redirectURI: string;
78
+ codeVerifier?: string | undefined;
79
+ deviceId?: string | undefined;
80
+ }) => Promise<OAuth2Tokens>;
81
+ refreshAccessToken: (refreshToken: string) => Promise<OAuth2Tokens>;
82
+ getUserInfo(token: OAuth2Tokens & {
83
+ user?: {
84
+ name?: {
85
+ firstName?: string;
86
+ lastName?: string;
87
+ };
88
+ email?: string;
89
+ } | undefined;
90
+ }): Promise<{
91
+ user: {
92
+ id: string;
93
+ name?: string;
94
+ email?: string | null;
95
+ image?: string;
96
+ emailVerified: boolean;
97
+ [key: string]: any;
98
+ };
99
+ data: any;
100
+ } | {
101
+ user: {
102
+ id: number;
103
+ name: string;
104
+ email: string;
105
+ image: string;
106
+ emailVerified: boolean;
107
+ } | {
108
+ id: string | number;
109
+ name: string;
110
+ email: string | null;
111
+ image: string;
112
+ emailVerified: boolean;
113
+ } | {
114
+ id: string | number;
115
+ name: string;
116
+ email: string | null;
117
+ image: string;
118
+ emailVerified: boolean;
119
+ };
120
+ data: GitlabProfile;
121
+ } | null>;
122
+ options: GitlabOptions;
123
+ };
124
+ //#endregion
125
+ export { GitlabOptions, GitlabProfile, gitlab };
@@ -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
+ const 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,109 @@
1
+ import { logger } from "../env/logger.mjs";
2
+ import "../env/index.mjs";
3
+ import { 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
+ import { APIError } from "better-call";
11
+
12
+ //#region src/social-providers/google.ts
13
+ const google = (options) => {
14
+ return {
15
+ id: "google",
16
+ name: "Google",
17
+ async createAuthorizationURL({ state, scopes, codeVerifier, redirectURI, loginHint, display }) {
18
+ if (!options.clientId || !options.clientSecret) {
19
+ logger.error("Client Id and Client Secret is required for Google. Make sure to provide them in the options.");
20
+ throw new BetterAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
21
+ }
22
+ if (!codeVerifier) throw new BetterAuthError("codeVerifier is required for Google");
23
+ const _scopes = options.disableDefaultScope ? [] : [
24
+ "email",
25
+ "profile",
26
+ "openid"
27
+ ];
28
+ if (options.scope) _scopes.push(...options.scope);
29
+ if (scopes) _scopes.push(...scopes);
30
+ return await createAuthorizationURL({
31
+ id: "google",
32
+ options,
33
+ authorizationEndpoint: "https://accounts.google.com/o/oauth2/auth",
34
+ scopes: _scopes,
35
+ state,
36
+ codeVerifier,
37
+ redirectURI,
38
+ prompt: options.prompt,
39
+ accessType: options.accessType,
40
+ display: display || options.display,
41
+ loginHint,
42
+ hd: options.hd,
43
+ additionalParams: { include_granted_scopes: "true" }
44
+ });
45
+ },
46
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
47
+ return validateAuthorizationCode({
48
+ code,
49
+ codeVerifier,
50
+ redirectURI,
51
+ options,
52
+ tokenEndpoint: "https://oauth2.googleapis.com/token"
53
+ });
54
+ },
55
+ refreshAccessToken: options.refreshAccessToken ? options.refreshAccessToken : async (refreshToken) => {
56
+ return refreshAccessToken({
57
+ refreshToken,
58
+ options: {
59
+ clientId: options.clientId,
60
+ clientKey: options.clientKey,
61
+ clientSecret: options.clientSecret
62
+ },
63
+ tokenEndpoint: "https://www.googleapis.com/oauth2/v4/token"
64
+ });
65
+ },
66
+ async verifyIdToken(token, nonce) {
67
+ if (options.disableIdTokenSignIn) return false;
68
+ if (options.verifyIdToken) return options.verifyIdToken(token, nonce);
69
+ const { kid, alg: jwtAlg } = decodeProtectedHeader(token);
70
+ if (!kid || !jwtAlg) return false;
71
+ const { payload: jwtClaims } = await jwtVerify(token, await getGooglePublicKey(kid), {
72
+ algorithms: [jwtAlg],
73
+ issuer: ["https://accounts.google.com", "accounts.google.com"],
74
+ audience: options.clientId,
75
+ maxTokenAge: "1h"
76
+ });
77
+ if (nonce && jwtClaims.nonce !== nonce) return false;
78
+ return true;
79
+ },
80
+ async getUserInfo(token) {
81
+ if (options.getUserInfo) return options.getUserInfo(token);
82
+ if (!token.idToken) return null;
83
+ const user = decodeJwt(token.idToken);
84
+ const userMap = await options.mapProfileToUser?.(user);
85
+ return {
86
+ user: {
87
+ id: user.sub,
88
+ name: user.name,
89
+ email: user.email,
90
+ image: user.picture,
91
+ emailVerified: user.email_verified,
92
+ ...userMap
93
+ },
94
+ data: user
95
+ };
96
+ },
97
+ options
98
+ };
99
+ };
100
+ const getGooglePublicKey = async (kid) => {
101
+ const { data } = await betterFetch("https://www.googleapis.com/oauth2/v3/certs");
102
+ if (!data?.keys) throw new APIError("BAD_REQUEST", { message: "Keys not found" });
103
+ const jwk = data.keys.find((key) => key.kid === kid);
104
+ if (!jwk) throw new Error(`JWK with kid ${kid} not found`);
105
+ return await importJWK(jwk, jwk.alg);
106
+ };
107
+
108
+ //#endregion
109
+ 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 };