@better-auth/core 1.4.0-beta.8 → 1.4.0

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 (125) hide show
  1. package/.turbo/turbo-build.log +41 -34
  2. package/dist/api/index.d.mts +3 -0
  3. package/dist/api/index.mjs +26 -0
  4. package/dist/async_hooks/index.d.mts +2 -10
  5. package/dist/async_hooks/index.mjs +2 -24
  6. package/dist/async_hooks-BfRfbd1J.mjs +18 -0
  7. package/dist/context/index.d.mts +54 -0
  8. package/dist/context/index.mjs +4 -0
  9. package/dist/context-DgQ9XGBl.mjs +114 -0
  10. package/dist/db/adapter/index.d.mts +3 -23
  11. package/dist/db/adapter/index.mjs +1 -1
  12. package/dist/db/index.d.mts +3 -127
  13. package/dist/db/index.mjs +46 -55
  14. package/dist/env/index.d.mts +2 -85
  15. package/dist/env/index.mjs +2 -299
  16. package/dist/env-DwlNAN_D.mjs +245 -0
  17. package/dist/error/index.d.mts +35 -0
  18. package/dist/error/index.mjs +4 -0
  19. package/dist/error-BhAKg8LX.mjs +45 -0
  20. package/dist/index-CdubV7uy.d.mts +82 -0
  21. package/dist/index-CkAWdKH8.d.mts +7352 -0
  22. package/dist/index-DgwIISs7.d.mts +7 -0
  23. package/dist/index.d.mts +3 -115
  24. package/dist/index.mjs +1 -1
  25. package/dist/oauth2/index.d.mts +3 -277
  26. package/dist/oauth2/index.mjs +2 -356
  27. package/dist/oauth2-DmgZmPEg.mjs +236 -0
  28. package/dist/social-providers/index.d.mts +3 -0
  29. package/dist/social-providers/index.mjs +2523 -0
  30. package/dist/utils/index.d.mts +9 -0
  31. package/dist/utils/index.mjs +3 -0
  32. package/dist/utils-C5EN75oV.mjs +7 -0
  33. package/package.json +90 -62
  34. package/src/api/index.ts +53 -0
  35. package/src/async_hooks/index.ts +1 -9
  36. package/src/context/endpoint-context.ts +49 -0
  37. package/src/context/index.ts +21 -0
  38. package/src/context/request-state.test.ts +94 -0
  39. package/src/context/request-state.ts +90 -0
  40. package/src/context/transaction.ts +73 -0
  41. package/src/db/adapter/index.ts +518 -8
  42. package/src/db/index.ts +12 -11
  43. package/src/db/plugin.ts +3 -3
  44. package/src/db/type.ts +55 -52
  45. package/src/env/color-depth.ts +5 -4
  46. package/src/env/env-impl.ts +2 -1
  47. package/src/env/index.ts +9 -9
  48. package/src/env/logger.test.ts +3 -2
  49. package/src/env/logger.ts +11 -9
  50. package/src/error/codes.ts +31 -0
  51. package/src/error/index.ts +11 -0
  52. package/src/index.ts +0 -2
  53. package/src/oauth2/client-credentials-token.ts +9 -9
  54. package/src/oauth2/create-authorization-url.ts +12 -12
  55. package/src/oauth2/index.ts +10 -11
  56. package/src/oauth2/oauth-provider.ts +96 -74
  57. package/src/oauth2/refresh-access-token.ts +12 -12
  58. package/src/oauth2/utils.ts +2 -0
  59. package/src/oauth2/validate-authorization-code.ts +13 -15
  60. package/src/social-providers/apple.ts +213 -0
  61. package/src/social-providers/atlassian.ts +132 -0
  62. package/src/social-providers/cognito.ts +269 -0
  63. package/src/social-providers/discord.ts +169 -0
  64. package/src/social-providers/dropbox.ts +112 -0
  65. package/src/social-providers/facebook.ts +206 -0
  66. package/src/social-providers/figma.ts +115 -0
  67. package/src/social-providers/github.ts +154 -0
  68. package/src/social-providers/gitlab.ts +155 -0
  69. package/src/social-providers/google.ts +171 -0
  70. package/src/social-providers/huggingface.ts +118 -0
  71. package/src/social-providers/index.ts +124 -0
  72. package/src/social-providers/kakao.ts +178 -0
  73. package/src/social-providers/kick.ts +93 -0
  74. package/src/social-providers/line.ts +169 -0
  75. package/src/social-providers/linear.ts +121 -0
  76. package/src/social-providers/linkedin.ts +110 -0
  77. package/src/social-providers/microsoft-entra-id.ts +259 -0
  78. package/src/social-providers/naver.ts +112 -0
  79. package/src/social-providers/notion.ts +108 -0
  80. package/src/social-providers/paybin.ts +122 -0
  81. package/src/social-providers/paypal.ts +263 -0
  82. package/src/social-providers/polar.ts +110 -0
  83. package/src/social-providers/reddit.ts +122 -0
  84. package/src/social-providers/roblox.ts +111 -0
  85. package/src/social-providers/salesforce.ts +159 -0
  86. package/src/social-providers/slack.ts +111 -0
  87. package/src/social-providers/spotify.ts +93 -0
  88. package/src/social-providers/tiktok.ts +210 -0
  89. package/src/social-providers/twitch.ts +111 -0
  90. package/src/social-providers/twitter.ts +198 -0
  91. package/src/social-providers/vk.ts +125 -0
  92. package/src/social-providers/zoom.ts +233 -0
  93. package/src/types/context.ts +270 -0
  94. package/src/types/cookie.ts +8 -0
  95. package/src/types/index.ts +21 -1
  96. package/src/types/init-options.ts +1328 -68
  97. package/src/types/plugin-client.ts +117 -0
  98. package/src/types/plugin.ts +158 -0
  99. package/src/utils/error-codes.ts +51 -0
  100. package/src/utils/index.ts +1 -0
  101. package/tsconfig.json +2 -5
  102. package/tsdown.config.ts +20 -0
  103. package/vitest.config.ts +3 -0
  104. package/build.config.ts +0 -19
  105. package/dist/async_hooks/index.cjs +0 -27
  106. package/dist/async_hooks/index.d.cts +0 -10
  107. package/dist/async_hooks/index.d.ts +0 -10
  108. package/dist/db/adapter/index.cjs +0 -2
  109. package/dist/db/adapter/index.d.cts +0 -23
  110. package/dist/db/adapter/index.d.ts +0 -23
  111. package/dist/db/index.cjs +0 -91
  112. package/dist/db/index.d.cts +0 -127
  113. package/dist/db/index.d.ts +0 -127
  114. package/dist/env/index.cjs +0 -315
  115. package/dist/env/index.d.cts +0 -85
  116. package/dist/env/index.d.ts +0 -85
  117. package/dist/index.cjs +0 -2
  118. package/dist/index.d.cts +0 -115
  119. package/dist/index.d.ts +0 -115
  120. package/dist/oauth2/index.cjs +0 -368
  121. package/dist/oauth2/index.d.cts +0 -277
  122. package/dist/oauth2/index.d.ts +0 -277
  123. package/dist/shared/core.DeNN5HMO.d.cts +0 -143
  124. package/dist/shared/core.DeNN5HMO.d.mts +0 -143
  125. package/dist/shared/core.DeNN5HMO.d.ts +0 -143
@@ -0,0 +1,171 @@
1
+ import { betterFetch } from "@better-fetch/fetch";
2
+ import { decodeJwt } from "jose";
3
+ import { logger } from "../env";
4
+ import { BetterAuthError } from "../error";
5
+ import type { OAuthProvider, ProviderOptions } from "../oauth2";
6
+ import {
7
+ createAuthorizationURL,
8
+ refreshAccessToken,
9
+ validateAuthorizationCode,
10
+ } from "../oauth2";
11
+
12
+ export interface GoogleProfile {
13
+ aud: string;
14
+ azp: string;
15
+ email: string;
16
+ email_verified: boolean;
17
+ exp: number;
18
+ /**
19
+ * The family name of the user, or last name in most
20
+ * Western languages.
21
+ */
22
+ family_name: string;
23
+ /**
24
+ * The given name of the user, or first name in most
25
+ * Western languages.
26
+ */
27
+ given_name: string;
28
+ hd?: string | undefined;
29
+ iat: number;
30
+ iss: string;
31
+ jti?: string | undefined;
32
+ locale?: string | undefined;
33
+ name: string;
34
+ nbf?: number | undefined;
35
+ picture: string;
36
+ sub: string;
37
+ }
38
+
39
+ export interface GoogleOptions extends ProviderOptions<GoogleProfile> {
40
+ clientId: string;
41
+ /**
42
+ * The access type to use for the authorization code request
43
+ */
44
+ accessType?: ("offline" | "online") | undefined;
45
+ /**
46
+ * The display mode to use for the authorization code request
47
+ */
48
+ display?: ("page" | "popup" | "touch" | "wap") | undefined;
49
+ /**
50
+ * The hosted domain of the user
51
+ */
52
+ hd?: string | undefined;
53
+ }
54
+
55
+ export const google = (options: GoogleOptions) => {
56
+ return {
57
+ id: "google",
58
+ name: "Google",
59
+ async createAuthorizationURL({
60
+ state,
61
+ scopes,
62
+ codeVerifier,
63
+ redirectURI,
64
+ loginHint,
65
+ display,
66
+ }) {
67
+ if (!options.clientId || !options.clientSecret) {
68
+ logger.error(
69
+ "Client Id and Client Secret is required for Google. Make sure to provide them in the options.",
70
+ );
71
+ throw new BetterAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
72
+ }
73
+ if (!codeVerifier) {
74
+ throw new BetterAuthError("codeVerifier is required for Google");
75
+ }
76
+ const _scopes = options.disableDefaultScope
77
+ ? []
78
+ : ["email", "profile", "openid"];
79
+ if (options.scope) _scopes.push(...options.scope);
80
+ if (scopes) _scopes.push(...scopes);
81
+ const url = await createAuthorizationURL({
82
+ id: "google",
83
+ options,
84
+ authorizationEndpoint: "https://accounts.google.com/o/oauth2/auth",
85
+ scopes: _scopes,
86
+ state,
87
+ codeVerifier,
88
+ redirectURI,
89
+ prompt: options.prompt,
90
+ accessType: options.accessType,
91
+ display: display || options.display,
92
+ loginHint,
93
+ hd: options.hd,
94
+ additionalParams: {
95
+ include_granted_scopes: "true",
96
+ },
97
+ });
98
+ return url;
99
+ },
100
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
101
+ return validateAuthorizationCode({
102
+ code,
103
+ codeVerifier,
104
+ redirectURI,
105
+ options,
106
+ tokenEndpoint: "https://oauth2.googleapis.com/token",
107
+ });
108
+ },
109
+ refreshAccessToken: options.refreshAccessToken
110
+ ? options.refreshAccessToken
111
+ : async (refreshToken) => {
112
+ return refreshAccessToken({
113
+ refreshToken,
114
+ options: {
115
+ clientId: options.clientId,
116
+ clientKey: options.clientKey,
117
+ clientSecret: options.clientSecret,
118
+ },
119
+ tokenEndpoint: "https://www.googleapis.com/oauth2/v4/token",
120
+ });
121
+ },
122
+ async verifyIdToken(token, nonce) {
123
+ if (options.disableIdTokenSignIn) {
124
+ return false;
125
+ }
126
+ if (options.verifyIdToken) {
127
+ return options.verifyIdToken(token, nonce);
128
+ }
129
+ const googlePublicKeyUrl = `https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=${token}`;
130
+ const { data: tokenInfo } = await betterFetch<{
131
+ aud: string;
132
+ iss: string;
133
+ email: string;
134
+ email_verified: boolean;
135
+ name: string;
136
+ picture: string;
137
+ sub: string;
138
+ }>(googlePublicKeyUrl);
139
+ if (!tokenInfo) {
140
+ return false;
141
+ }
142
+ const isValid =
143
+ tokenInfo.aud === options.clientId &&
144
+ (tokenInfo.iss === "https://accounts.google.com" ||
145
+ tokenInfo.iss === "accounts.google.com");
146
+ return isValid;
147
+ },
148
+ async getUserInfo(token) {
149
+ if (options.getUserInfo) {
150
+ return options.getUserInfo(token);
151
+ }
152
+ if (!token.idToken) {
153
+ return null;
154
+ }
155
+ const user = decodeJwt(token.idToken) as GoogleProfile;
156
+ const userMap = await options.mapProfileToUser?.(user);
157
+ return {
158
+ user: {
159
+ id: user.sub,
160
+ name: user.name,
161
+ email: user.email,
162
+ image: user.picture,
163
+ emailVerified: user.email_verified,
164
+ ...userMap,
165
+ },
166
+ data: user,
167
+ };
168
+ },
169
+ options,
170
+ } satisfies OAuthProvider<GoogleProfile>;
171
+ };
@@ -0,0 +1,118 @@
1
+ import { betterFetch } from "@better-fetch/fetch";
2
+ import type { OAuthProvider, ProviderOptions } from "../oauth2";
3
+ import {
4
+ createAuthorizationURL,
5
+ refreshAccessToken,
6
+ validateAuthorizationCode,
7
+ } from "../oauth2";
8
+
9
+ export interface HuggingFaceProfile {
10
+ sub: string;
11
+ name: string;
12
+ preferred_username: string;
13
+ profile: string;
14
+ picture: string;
15
+ website?: string | undefined;
16
+ email?: string | undefined;
17
+ email_verified?: boolean | undefined;
18
+ isPro: boolean;
19
+ canPay?: boolean | undefined;
20
+ orgs?:
21
+ | {
22
+ sub: string;
23
+ name: string;
24
+ picture: string;
25
+ preferred_username: string;
26
+ isEnterprise: boolean | "plus";
27
+ canPay?: boolean;
28
+ roleInOrg?: "admin" | "write" | "contributor" | "read";
29
+ pendingSSO?: boolean;
30
+ missingMFA?: boolean;
31
+ resourceGroups?: {
32
+ sub: string;
33
+ name: string;
34
+ role: "admin" | "write" | "contributor" | "read";
35
+ }[];
36
+ }
37
+ | undefined;
38
+ }
39
+
40
+ export interface HuggingFaceOptions
41
+ extends ProviderOptions<HuggingFaceProfile> {
42
+ clientId: string;
43
+ }
44
+
45
+ export const huggingface = (options: HuggingFaceOptions) => {
46
+ return {
47
+ id: "huggingface",
48
+ name: "Hugging Face",
49
+ createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
50
+ const _scopes = options.disableDefaultScope
51
+ ? []
52
+ : ["openid", "profile", "email"];
53
+ if (options.scope) _scopes.push(...options.scope);
54
+ if (scopes) _scopes.push(...scopes);
55
+ return createAuthorizationURL({
56
+ id: "huggingface",
57
+ options,
58
+ authorizationEndpoint: "https://huggingface.co/oauth/authorize",
59
+ scopes: _scopes,
60
+ state,
61
+ codeVerifier,
62
+ redirectURI,
63
+ });
64
+ },
65
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
66
+ return validateAuthorizationCode({
67
+ code,
68
+ codeVerifier,
69
+ redirectURI,
70
+ options,
71
+ tokenEndpoint: "https://huggingface.co/oauth/token",
72
+ });
73
+ },
74
+ refreshAccessToken: options.refreshAccessToken
75
+ ? options.refreshAccessToken
76
+ : async (refreshToken) => {
77
+ return refreshAccessToken({
78
+ refreshToken,
79
+ options: {
80
+ clientId: options.clientId,
81
+ clientKey: options.clientKey,
82
+ clientSecret: options.clientSecret,
83
+ },
84
+ tokenEndpoint: "https://huggingface.co/oauth/token",
85
+ });
86
+ },
87
+ async getUserInfo(token) {
88
+ if (options.getUserInfo) {
89
+ return options.getUserInfo(token);
90
+ }
91
+ const { data: profile, error } = await betterFetch<HuggingFaceProfile>(
92
+ "https://huggingface.co/oauth/userinfo",
93
+ {
94
+ method: "GET",
95
+ headers: {
96
+ Authorization: `Bearer ${token.accessToken}`,
97
+ },
98
+ },
99
+ );
100
+ if (error) {
101
+ return null;
102
+ }
103
+ const userMap = await options.mapProfileToUser?.(profile);
104
+ return {
105
+ user: {
106
+ id: profile.sub,
107
+ name: profile.name || profile.preferred_username,
108
+ email: profile.email,
109
+ image: profile.picture,
110
+ emailVerified: profile.email_verified ?? false,
111
+ ...userMap,
112
+ },
113
+ data: profile,
114
+ };
115
+ },
116
+ options,
117
+ } satisfies OAuthProvider<HuggingFaceProfile>;
118
+ };
@@ -0,0 +1,124 @@
1
+ import * as z from "zod";
2
+ import { apple } from "./apple";
3
+ import { atlassian } from "./atlassian";
4
+ import { cognito } from "./cognito";
5
+ import { discord } from "./discord";
6
+ import { dropbox } from "./dropbox";
7
+ import { facebook } from "./facebook";
8
+ import { figma } from "./figma";
9
+ import { github } from "./github";
10
+ import { gitlab } from "./gitlab";
11
+ import { google } from "./google";
12
+ import { huggingface } from "./huggingface";
13
+ import { kakao } from "./kakao";
14
+ import { kick } from "./kick";
15
+ import { line } from "./line";
16
+ import { linear } from "./linear";
17
+ import { linkedin } from "./linkedin";
18
+ import { microsoft } from "./microsoft-entra-id";
19
+ import { naver } from "./naver";
20
+ import { notion } from "./notion";
21
+ import { paybin } from "./paybin";
22
+ import { paypal } from "./paypal";
23
+ import { polar } from "./polar";
24
+ import { reddit } from "./reddit";
25
+ import { roblox } from "./roblox";
26
+ import { salesforce } from "./salesforce";
27
+ import { slack } from "./slack";
28
+ import { spotify } from "./spotify";
29
+ import { tiktok } from "./tiktok";
30
+ import { twitch } from "./twitch";
31
+ import { twitter } from "./twitter";
32
+ import { vk } from "./vk";
33
+ import { zoom } from "./zoom";
34
+
35
+ export const socialProviders = {
36
+ apple,
37
+ atlassian,
38
+ cognito,
39
+ discord,
40
+ facebook,
41
+ figma,
42
+ github,
43
+ microsoft,
44
+ google,
45
+ huggingface,
46
+ slack,
47
+ spotify,
48
+ twitch,
49
+ twitter,
50
+ dropbox,
51
+ kick,
52
+ linear,
53
+ linkedin,
54
+ gitlab,
55
+ tiktok,
56
+ reddit,
57
+ roblox,
58
+ salesforce,
59
+ vk,
60
+ zoom,
61
+ notion,
62
+ kakao,
63
+ naver,
64
+ line,
65
+ paybin,
66
+ paypal,
67
+ polar,
68
+ };
69
+
70
+ export const socialProviderList = Object.keys(socialProviders) as [
71
+ "github",
72
+ ...(keyof typeof socialProviders)[],
73
+ ];
74
+
75
+ export const SocialProviderListEnum = z
76
+ .enum(socialProviderList)
77
+ .or(z.string()) as z.ZodType<SocialProviderList[number] | (string & {})>;
78
+
79
+ export type SocialProvider = z.infer<typeof SocialProviderListEnum>;
80
+
81
+ export type SocialProviders = {
82
+ [K in SocialProviderList[number]]?: Parameters<
83
+ (typeof socialProviders)[K]
84
+ >[0] & {
85
+ enabled?: boolean | undefined;
86
+ };
87
+ };
88
+
89
+ export * from "./apple";
90
+ export * from "./atlassian";
91
+ export * from "./cognito";
92
+ export * from "./discord";
93
+ export * from "./dropbox";
94
+ export * from "./facebook";
95
+ export * from "./figma";
96
+ export * from "./github";
97
+ export * from "./gitlab";
98
+ export * from "./google";
99
+ export * from "./huggingface";
100
+ export * from "./kakao";
101
+ export * from "./kick";
102
+ export * from "./kick";
103
+ export * from "./line";
104
+ export * from "./linear";
105
+ export * from "./linkedin";
106
+ export * from "./linkedin";
107
+ export * from "./microsoft-entra-id";
108
+ export * from "./naver";
109
+ export * from "./notion";
110
+ export * from "./paybin";
111
+ export * from "./paypal";
112
+ export * from "./polar";
113
+ export * from "./reddit";
114
+ export * from "./roblox";
115
+ export * from "./salesforce";
116
+ export * from "./slack";
117
+ export * from "./spotify";
118
+ export * from "./tiktok";
119
+ export * from "./twitch";
120
+ export * from "./twitter";
121
+ export * from "./vk";
122
+ export * from "./zoom";
123
+
124
+ export type SocialProviderList = typeof socialProviderList;
@@ -0,0 +1,178 @@
1
+ import { betterFetch } from "@better-fetch/fetch";
2
+ import type { OAuthProvider, ProviderOptions } from "../oauth2";
3
+ import {
4
+ createAuthorizationURL,
5
+ refreshAccessToken,
6
+ validateAuthorizationCode,
7
+ } from "../oauth2";
8
+
9
+ interface Partner {
10
+ /** Partner-specific ID (consent required: kakaotalk_message) */
11
+ uuid?: string | undefined;
12
+ }
13
+
14
+ interface Profile {
15
+ /** Nickname (consent required: profile/nickname) */
16
+ nickname?: string | undefined;
17
+ /** Thumbnail image URL (consent required: profile/profile image) */
18
+ thumbnail_image_url?: string | undefined;
19
+ /** Profile image URL (consent required: profile/profile image) */
20
+ profile_image_url?: string | undefined;
21
+ /** Whether the profile image is the default */
22
+ is_default_image?: boolean | undefined;
23
+ /** Whether the nickname is the default */
24
+ is_default_nickname?: boolean | undefined;
25
+ }
26
+
27
+ interface KakaoAccount {
28
+ /** Consent required: profile info (nickname/profile image) */
29
+ profile_needs_agreement?: boolean | undefined;
30
+ /** Consent required: nickname */
31
+ profile_nickname_needs_agreement?: boolean | undefined;
32
+ /** Consent required: profile image */
33
+ profile_image_needs_agreement?: boolean | undefined;
34
+ /** Profile info */
35
+ profile?: Profile | undefined;
36
+ /** Consent required: name */
37
+ name_needs_agreement?: boolean | undefined;
38
+ /** Name */
39
+ name?: string | undefined;
40
+ /** Consent required: email */
41
+ email_needs_agreement?: boolean | undefined;
42
+ /** Email valid */
43
+ is_email_valid?: boolean | undefined;
44
+ /** Email verified */
45
+ is_email_verified?: boolean | undefined;
46
+ /** Email */
47
+ email?: string | undefined;
48
+ /** Consent required: age range */
49
+ age_range_needs_agreement?: boolean | undefined;
50
+ /** Age range */
51
+ age_range?: string | undefined;
52
+ /** Consent required: birth year */
53
+ birthyear_needs_agreement?: boolean | undefined;
54
+ /** Birth year (YYYY) */
55
+ birthyear?: string | undefined;
56
+ /** Consent required: birthday */
57
+ birthday_needs_agreement?: boolean | undefined;
58
+ /** Birthday (MMDD) */
59
+ birthday?: string | undefined;
60
+ /** Birthday type (SOLAR/LUNAR) */
61
+ birthday_type?: string | undefined;
62
+ /** Whether birthday is in a leap month */
63
+ is_leap_month?: boolean | undefined;
64
+ /** Consent required: gender */
65
+ gender_needs_agreement?: boolean | undefined;
66
+ /** Gender (male/female) */
67
+ gender?: string | undefined;
68
+ /** Consent required: phone number */
69
+ phone_number_needs_agreement?: boolean | undefined;
70
+ /** Phone number */
71
+ phone_number?: string | undefined;
72
+ /** Consent required: CI */
73
+ ci_needs_agreement?: boolean | undefined;
74
+ /** CI (unique identifier) */
75
+ ci?: string | undefined;
76
+ /** CI authentication time (UTC) */
77
+ ci_authenticated_at?: string | undefined;
78
+ }
79
+
80
+ export interface KakaoProfile {
81
+ /** Kakao user ID */
82
+ id: number;
83
+ /**
84
+ * Whether the user has signed up (only present if auto-connection is disabled)
85
+ * false: preregistered, true: registered
86
+ */
87
+ has_signed_up?: boolean | undefined;
88
+ /** UTC datetime when the user connected the service */
89
+ connected_at?: string | undefined;
90
+ /** UTC datetime when the user signed up via Kakao Sync */
91
+ synched_at?: string | undefined;
92
+ /** Custom user properties */
93
+ properties?: Record<string, any> | undefined;
94
+ /** Kakao account info */
95
+ kakao_account: KakaoAccount;
96
+ /** Partner info */
97
+ for_partner?: Partner | undefined;
98
+ }
99
+
100
+ export interface KakaoOptions extends ProviderOptions<KakaoProfile> {
101
+ clientId: string;
102
+ }
103
+
104
+ export const kakao = (options: KakaoOptions) => {
105
+ return {
106
+ id: "kakao",
107
+ name: "Kakao",
108
+ createAuthorizationURL({ state, scopes, redirectURI }) {
109
+ const _scopes = options.disableDefaultScope
110
+ ? []
111
+ : ["account_email", "profile_image", "profile_nickname"];
112
+ if (options.scope) _scopes.push(...options.scope);
113
+ if (scopes) _scopes.push(...scopes);
114
+ return createAuthorizationURL({
115
+ id: "kakao",
116
+ options,
117
+ authorizationEndpoint: "https://kauth.kakao.com/oauth/authorize",
118
+ scopes: _scopes,
119
+ state,
120
+ redirectURI,
121
+ });
122
+ },
123
+ validateAuthorizationCode: async ({ code, redirectURI }) => {
124
+ return validateAuthorizationCode({
125
+ code,
126
+ redirectURI,
127
+ options,
128
+ tokenEndpoint: "https://kauth.kakao.com/oauth/token",
129
+ });
130
+ },
131
+ refreshAccessToken: options.refreshAccessToken
132
+ ? options.refreshAccessToken
133
+ : async (refreshToken) => {
134
+ return refreshAccessToken({
135
+ refreshToken,
136
+ options: {
137
+ clientId: options.clientId,
138
+ clientKey: options.clientKey,
139
+ clientSecret: options.clientSecret,
140
+ },
141
+ tokenEndpoint: "https://kauth.kakao.com/oauth/token",
142
+ });
143
+ },
144
+ async getUserInfo(token) {
145
+ if (options.getUserInfo) {
146
+ return options.getUserInfo(token);
147
+ }
148
+ const { data: profile, error } = await betterFetch<KakaoProfile>(
149
+ "https://kapi.kakao.com/v2/user/me",
150
+ {
151
+ headers: {
152
+ Authorization: `Bearer ${token.accessToken}`,
153
+ },
154
+ },
155
+ );
156
+ if (error || !profile) {
157
+ return null;
158
+ }
159
+ const userMap = await options.mapProfileToUser?.(profile);
160
+ const account = profile.kakao_account || {};
161
+ const kakaoProfile = account.profile || {};
162
+ const user = {
163
+ id: String(profile.id),
164
+ name: kakaoProfile.nickname || account.name || undefined,
165
+ email: account.email,
166
+ image:
167
+ kakaoProfile.profile_image_url || kakaoProfile.thumbnail_image_url,
168
+ emailVerified: !!account.is_email_valid && !!account.is_email_verified,
169
+ ...userMap,
170
+ };
171
+ return {
172
+ user,
173
+ data: profile,
174
+ };
175
+ },
176
+ options,
177
+ } satisfies OAuthProvider<KakaoProfile>;
178
+ };
@@ -0,0 +1,93 @@
1
+ import { betterFetch } from "@better-fetch/fetch";
2
+ import type { OAuthProvider, ProviderOptions } from "../oauth2";
3
+ import { createAuthorizationURL, validateAuthorizationCode } from "../oauth2";
4
+
5
+ export interface KickProfile {
6
+ /**
7
+ * The user id of the user
8
+ */
9
+ user_id: string;
10
+ /**
11
+ * The name of the user
12
+ */
13
+ name: string;
14
+ /**
15
+ * The email of the user
16
+ */
17
+ email: string;
18
+ /**
19
+ * The picture of the user
20
+ */
21
+ profile_picture: string;
22
+ }
23
+
24
+ export interface KickOptions extends ProviderOptions<KickProfile> {
25
+ clientId: string;
26
+ }
27
+
28
+ export const kick = (options: KickOptions) => {
29
+ return {
30
+ id: "kick",
31
+ name: "Kick",
32
+ createAuthorizationURL({ state, scopes, redirectURI, codeVerifier }) {
33
+ const _scopes = options.disableDefaultScope ? [] : ["user:read"];
34
+ if (options.scope) _scopes.push(...options.scope);
35
+ if (scopes) _scopes.push(...scopes);
36
+
37
+ return createAuthorizationURL({
38
+ id: "kick",
39
+ redirectURI,
40
+ options,
41
+ authorizationEndpoint: "https://id.kick.com/oauth/authorize",
42
+ scopes: _scopes,
43
+ codeVerifier,
44
+ state,
45
+ });
46
+ },
47
+ async validateAuthorizationCode({ code, redirectURI, codeVerifier }) {
48
+ return validateAuthorizationCode({
49
+ code,
50
+ redirectURI,
51
+ options,
52
+ tokenEndpoint: "https://id.kick.com/oauth/token",
53
+ codeVerifier,
54
+ });
55
+ },
56
+ async getUserInfo(token) {
57
+ if (options.getUserInfo) {
58
+ return options.getUserInfo(token);
59
+ }
60
+
61
+ const { data, error } = await betterFetch<{
62
+ data: KickProfile[];
63
+ }>("https://api.kick.com/public/v1/users", {
64
+ method: "GET",
65
+ headers: {
66
+ Authorization: `Bearer ${token.accessToken}`,
67
+ },
68
+ });
69
+
70
+ if (error) {
71
+ return null;
72
+ }
73
+
74
+ const profile = data.data[0]!;
75
+
76
+ const userMap = await options.mapProfileToUser?.(profile);
77
+ // Kick does not provide email_verified claim.
78
+ // We default to false for security consistency.
79
+ return {
80
+ user: {
81
+ id: profile.user_id,
82
+ name: profile.name,
83
+ email: profile.email,
84
+ image: profile.profile_picture,
85
+ emailVerified: false,
86
+ ...userMap,
87
+ },
88
+ data: profile,
89
+ };
90
+ },
91
+ options,
92
+ } satisfies OAuthProvider<KickProfile>;
93
+ };