@gatwech/core 0.0.1-beta.1

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 (109) hide show
  1. package/.turbo/turbo-build.log +42 -0
  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 -0
  5. package/dist/async_hooks/index.mjs +3 -0
  6. package/dist/async_hooks-BrLqUNsE.mjs +18 -0
  7. package/dist/context/index.d.mts +44 -0
  8. package/dist/context/index.mjs +4 -0
  9. package/dist/context-DxzHfoWW.mjs +108 -0
  10. package/dist/db/adapter/index.d.mts +3 -0
  11. package/dist/db/adapter/index.mjs +1 -0
  12. package/dist/db/index.d.mts +3 -0
  13. package/dist/db/index.mjs +61 -0
  14. package/dist/env/index.d.mts +2 -0
  15. package/dist/env/index.mjs +3 -0
  16. package/dist/env-JMSMESs1.mjs +245 -0
  17. package/dist/error/index.d.mts +35 -0
  18. package/dist/error/index.mjs +4 -0
  19. package/dist/error-41YsFQ8b.mjs +45 -0
  20. package/dist/index-CRg-E-VF.d.mts +7 -0
  21. package/dist/index-DLP-K0uY.d.mts +82 -0
  22. package/dist/index-DjrJweYn.d.mts +7312 -0
  23. package/dist/index.d.mts +3 -0
  24. package/dist/index.mjs +1 -0
  25. package/dist/oauth2/index.d.mts +3 -0
  26. package/dist/oauth2/index.mjs +3 -0
  27. package/dist/oauth2-DyI7_CNv.mjs +235 -0
  28. package/dist/social-providers/index.d.mts +3 -0
  29. package/dist/social-providers/index.mjs +2513 -0
  30. package/dist/utils/index.d.mts +9 -0
  31. package/dist/utils/index.mjs +3 -0
  32. package/dist/utils-BFQCAfNW.mjs +7 -0
  33. package/package.json +131 -0
  34. package/src/api/index.ts +57 -0
  35. package/src/async_hooks/index.ts +35 -0
  36. package/src/context/endpoint-context.ts +48 -0
  37. package/src/context/index.ts +21 -0
  38. package/src/context/request-state.test.ts +346 -0
  39. package/src/context/request-state.ts +75 -0
  40. package/src/context/transaction.ts +72 -0
  41. package/src/db/adapter/index.ts +466 -0
  42. package/src/db/index.ts +50 -0
  43. package/src/db/plugin.ts +11 -0
  44. package/src/db/schema/account.ts +34 -0
  45. package/src/db/schema/rate-limit.ts +21 -0
  46. package/src/db/schema/session.ts +17 -0
  47. package/src/db/schema/shared.ts +7 -0
  48. package/src/db/schema/user.ts +16 -0
  49. package/src/db/schema/verification.ts +15 -0
  50. package/src/db/type.ts +180 -0
  51. package/src/env/color-depth.ts +172 -0
  52. package/src/env/env-impl.ts +124 -0
  53. package/src/env/index.ts +23 -0
  54. package/src/env/logger.test.ts +33 -0
  55. package/src/env/logger.ts +145 -0
  56. package/src/error/codes.ts +31 -0
  57. package/src/error/index.ts +11 -0
  58. package/src/index.ts +1 -0
  59. package/src/oauth2/client-credentials-token.ts +102 -0
  60. package/src/oauth2/create-authorization-url.ts +85 -0
  61. package/src/oauth2/index.ts +21 -0
  62. package/src/oauth2/oauth-provider.ts +211 -0
  63. package/src/oauth2/refresh-access-token.ts +124 -0
  64. package/src/oauth2/utils.ts +36 -0
  65. package/src/oauth2/validate-authorization-code.ts +154 -0
  66. package/src/social-providers/apple.ts +213 -0
  67. package/src/social-providers/atlassian.ts +132 -0
  68. package/src/social-providers/cognito.ts +269 -0
  69. package/src/social-providers/discord.ts +169 -0
  70. package/src/social-providers/dropbox.ts +112 -0
  71. package/src/social-providers/facebook.ts +204 -0
  72. package/src/social-providers/figma.ts +115 -0
  73. package/src/social-providers/github.ts +154 -0
  74. package/src/social-providers/gitlab.ts +152 -0
  75. package/src/social-providers/google.ts +171 -0
  76. package/src/social-providers/huggingface.ts +118 -0
  77. package/src/social-providers/index.ts +124 -0
  78. package/src/social-providers/kakao.ts +178 -0
  79. package/src/social-providers/kick.ts +92 -0
  80. package/src/social-providers/line.ts +169 -0
  81. package/src/social-providers/linear.ts +120 -0
  82. package/src/social-providers/linkedin.ts +110 -0
  83. package/src/social-providers/microsoft-entra-id.ts +245 -0
  84. package/src/social-providers/naver.ts +112 -0
  85. package/src/social-providers/notion.ts +108 -0
  86. package/src/social-providers/paybin.ts +122 -0
  87. package/src/social-providers/paypal.ts +263 -0
  88. package/src/social-providers/polar.ts +107 -0
  89. package/src/social-providers/reddit.ts +122 -0
  90. package/src/social-providers/roblox.ts +110 -0
  91. package/src/social-providers/salesforce.ts +159 -0
  92. package/src/social-providers/slack.ts +111 -0
  93. package/src/social-providers/spotify.ts +93 -0
  94. package/src/social-providers/tiktok.ts +210 -0
  95. package/src/social-providers/twitch.ts +111 -0
  96. package/src/social-providers/twitter.ts +198 -0
  97. package/src/social-providers/vk.ts +126 -0
  98. package/src/social-providers/zoom.ts +217 -0
  99. package/src/types/context.ts +269 -0
  100. package/src/types/cookie.ts +7 -0
  101. package/src/types/helper.ts +6 -0
  102. package/src/types/index.ts +21 -0
  103. package/src/types/init-options.ts +1346 -0
  104. package/src/types/plugin-client.ts +100 -0
  105. package/src/types/plugin.ts +158 -0
  106. package/src/utils/error-codes.ts +51 -0
  107. package/src/utils/index.ts +1 -0
  108. package/tsconfig.json +7 -0
  109. package/tsdown.config.ts +20 -0
@@ -0,0 +1,122 @@
1
+ import { base64 } from "@gatwech/utils/base64";
2
+ import { betterFetch } from "@better-fetch/fetch";
3
+ import type { OAuthProvider, ProviderOptions } from "../oauth2";
4
+ import {
5
+ createAuthorizationURL,
6
+ getOAuth2Tokens,
7
+ refreshAccessToken,
8
+ } from "../oauth2";
9
+
10
+ export interface RedditProfile {
11
+ id: string;
12
+ name: string;
13
+ icon_img: string | null;
14
+ has_verified_email: boolean;
15
+ oauth_client_id: string;
16
+ verified: boolean;
17
+ }
18
+
19
+ export interface RedditOptions extends ProviderOptions<RedditProfile> {
20
+ clientId: string;
21
+ duration?: string | undefined;
22
+ }
23
+
24
+ export const reddit = (options: RedditOptions) => {
25
+ return {
26
+ id: "reddit",
27
+ name: "Reddit",
28
+ createAuthorizationURL({ state, scopes, redirectURI }) {
29
+ const _scopes = options.disableDefaultScope ? [] : ["identity"];
30
+ if (options.scope) _scopes.push(...options.scope);
31
+ if (scopes) _scopes.push(...scopes);
32
+ return createAuthorizationURL({
33
+ id: "reddit",
34
+ options,
35
+ authorizationEndpoint: "https://www.reddit.com/api/v1/authorize",
36
+ scopes: _scopes,
37
+ state,
38
+ redirectURI,
39
+ duration: options.duration,
40
+ });
41
+ },
42
+ validateAuthorizationCode: async ({ code, redirectURI }) => {
43
+ const body = new URLSearchParams({
44
+ grant_type: "authorization_code",
45
+ code,
46
+ redirect_uri: options.redirectURI || redirectURI,
47
+ });
48
+ const headers = {
49
+ "content-type": "application/x-www-form-urlencoded",
50
+ accept: "text/plain",
51
+ "user-agent": "gatwech",
52
+ Authorization: `Basic ${base64.encode(
53
+ `${options.clientId}:${options.clientSecret}`
54
+ )}`,
55
+ };
56
+
57
+ const { data, error } = await betterFetch<object>(
58
+ "https://www.reddit.com/api/v1/access_token",
59
+ {
60
+ method: "POST",
61
+ headers,
62
+ body: body.toString(),
63
+ }
64
+ );
65
+
66
+ if (error) {
67
+ throw error;
68
+ }
69
+
70
+ return getOAuth2Tokens(data);
71
+ },
72
+
73
+ refreshAccessToken: options.refreshAccessToken
74
+ ? options.refreshAccessToken
75
+ : async (refreshToken) => {
76
+ return refreshAccessToken({
77
+ refreshToken,
78
+ options: {
79
+ clientId: options.clientId,
80
+ clientKey: options.clientKey,
81
+ clientSecret: options.clientSecret,
82
+ },
83
+ authentication: "basic",
84
+ tokenEndpoint: "https://www.reddit.com/api/v1/access_token",
85
+ });
86
+ },
87
+ async getUserInfo(token) {
88
+ if (options.getUserInfo) {
89
+ return options.getUserInfo(token);
90
+ }
91
+
92
+ const { data: profile, error } = await betterFetch<RedditProfile>(
93
+ "https://oauth.reddit.com/api/v1/me",
94
+ {
95
+ headers: {
96
+ Authorization: `Bearer ${token.accessToken}`,
97
+ "User-Agent": "gatwech",
98
+ },
99
+ }
100
+ );
101
+
102
+ if (error) {
103
+ return null;
104
+ }
105
+
106
+ const userMap = await options.mapProfileToUser?.(profile);
107
+
108
+ return {
109
+ user: {
110
+ id: profile.id,
111
+ name: profile.name,
112
+ email: profile.oauth_client_id,
113
+ emailVerified: profile.has_verified_email,
114
+ image: profile.icon_img?.split("?")[0]!,
115
+ ...userMap,
116
+ },
117
+ data: profile,
118
+ };
119
+ },
120
+ options,
121
+ } satisfies OAuthProvider<RedditProfile>;
122
+ };
@@ -0,0 +1,110 @@
1
+ import { betterFetch } from "@better-fetch/fetch";
2
+ import type { OAuthProvider, ProviderOptions } from "../oauth2";
3
+ import { refreshAccessToken, validateAuthorizationCode } from "../oauth2";
4
+
5
+ export interface RobloxProfile extends Record<string, any> {
6
+ /** the user's id */
7
+ sub: string;
8
+ /** the user's username */
9
+ preferred_username: string;
10
+ /** the user's display name, will return the same value as the preferred_username if not set */
11
+ nickname: string;
12
+ /** the user's display name, again, will return the same value as the preferred_username if not set */
13
+ name: string;
14
+ /** the account creation date as a unix timestamp in seconds */
15
+ created_at: number;
16
+ /** the user's profile URL */
17
+ profile: string;
18
+ /** the user's avatar URL */
19
+ picture: string;
20
+ }
21
+
22
+ export interface RobloxOptions extends ProviderOptions<RobloxProfile> {
23
+ clientId: string;
24
+ prompt?:
25
+ | (
26
+ | "none"
27
+ | "consent"
28
+ | "login"
29
+ | "select_account"
30
+ | "select_account consent"
31
+ )
32
+ | undefined;
33
+ }
34
+
35
+ export const roblox = (options: RobloxOptions) => {
36
+ return {
37
+ id: "roblox",
38
+ name: "Roblox",
39
+ createAuthorizationURL({ state, scopes, redirectURI }) {
40
+ const _scopes = options.disableDefaultScope ? [] : ["openid", "profile"];
41
+ if (options.scope) _scopes.push(...options.scope);
42
+ if (scopes) _scopes.push(...scopes);
43
+ return new URL(
44
+ `https://apis.roblox.com/oauth/v1/authorize?scope=${_scopes.join(
45
+ "+",
46
+ )}&response_type=code&client_id=${
47
+ options.clientId
48
+ }&redirect_uri=${encodeURIComponent(
49
+ options.redirectURI || redirectURI,
50
+ )}&state=${state}&prompt=${options.prompt || "select_account consent"}`,
51
+ );
52
+ },
53
+ validateAuthorizationCode: async ({ code, redirectURI }) => {
54
+ return validateAuthorizationCode({
55
+ code,
56
+ redirectURI: options.redirectURI || redirectURI,
57
+ options,
58
+ tokenEndpoint: "https://apis.roblox.com/oauth/v1/token",
59
+ authentication: "post",
60
+ });
61
+ },
62
+ refreshAccessToken: options.refreshAccessToken
63
+ ? options.refreshAccessToken
64
+ : async (refreshToken) => {
65
+ return refreshAccessToken({
66
+ refreshToken,
67
+ options: {
68
+ clientId: options.clientId,
69
+ clientKey: options.clientKey,
70
+ clientSecret: options.clientSecret,
71
+ },
72
+ tokenEndpoint: "https://apis.roblox.com/oauth/v1/token",
73
+ });
74
+ },
75
+ async getUserInfo(token) {
76
+ if (options.getUserInfo) {
77
+ return options.getUserInfo(token);
78
+ }
79
+ const { data: profile, error } = await betterFetch<RobloxProfile>(
80
+ "https://apis.roblox.com/oauth/v1/userinfo",
81
+ {
82
+ headers: {
83
+ authorization: `Bearer ${token.accessToken}`,
84
+ },
85
+ },
86
+ );
87
+
88
+ if (error) {
89
+ return null;
90
+ }
91
+
92
+ const userMap = await options.mapProfileToUser?.(profile);
93
+
94
+ return {
95
+ user: {
96
+ id: profile.sub,
97
+ name: profile.nickname || profile.preferred_username || "",
98
+ image: profile.picture,
99
+ email: profile.preferred_username || null, // Roblox does not provide email
100
+ emailVerified: true,
101
+ ...userMap,
102
+ },
103
+ data: {
104
+ ...profile,
105
+ },
106
+ };
107
+ },
108
+ options,
109
+ } satisfies OAuthProvider<RobloxProfile>;
110
+ };
@@ -0,0 +1,159 @@
1
+ import { betterFetch } from "@better-fetch/fetch";
2
+ import { logger } from "../env";
3
+ import { PayvoAuthError } from "../error";
4
+ import type { OAuthProvider, ProviderOptions } from "../oauth2";
5
+ import {
6
+ createAuthorizationURL,
7
+ refreshAccessToken,
8
+ validateAuthorizationCode,
9
+ } from "../oauth2";
10
+
11
+ export interface SalesforceProfile {
12
+ sub: string;
13
+ user_id: string;
14
+ organization_id: string;
15
+ preferred_username?: string | undefined;
16
+ email: string;
17
+ email_verified?: boolean | undefined;
18
+ name: string;
19
+ given_name?: string | undefined;
20
+ family_name?: string | undefined;
21
+ zoneinfo?: string | undefined;
22
+ photos?:
23
+ | {
24
+ picture?: string;
25
+ thumbnail?: string;
26
+ }
27
+ | undefined;
28
+ }
29
+
30
+ export interface SalesforceOptions extends ProviderOptions<SalesforceProfile> {
31
+ clientId: string;
32
+ environment?: ("sandbox" | "production") | undefined;
33
+ loginUrl?: string | undefined;
34
+ /**
35
+ * Override the redirect URI if auto-detection fails.
36
+ * Should match the Callback URL configured in your Salesforce Connected App.
37
+ * @example "http://localhost:3000/api/v1/auth/callback/salesforce"
38
+ */
39
+ redirectURI?: string | undefined;
40
+ }
41
+
42
+ export const salesforce = (options: SalesforceOptions) => {
43
+ const environment = options.environment ?? "production";
44
+ const isSandbox = environment === "sandbox";
45
+ const authorizationEndpoint = options.loginUrl
46
+ ? `https://${options.loginUrl}/services/oauth2/authorize`
47
+ : isSandbox
48
+ ? "https://test.salesforce.com/services/oauth2/authorize"
49
+ : "https://login.salesforce.com/services/oauth2/authorize";
50
+
51
+ const tokenEndpoint = options.loginUrl
52
+ ? `https://${options.loginUrl}/services/oauth2/token`
53
+ : isSandbox
54
+ ? "https://test.salesforce.com/services/oauth2/token"
55
+ : "https://login.salesforce.com/services/oauth2/token";
56
+
57
+ const userInfoEndpoint = options.loginUrl
58
+ ? `https://${options.loginUrl}/services/oauth2/userinfo`
59
+ : isSandbox
60
+ ? "https://test.salesforce.com/services/oauth2/userinfo"
61
+ : "https://login.salesforce.com/services/oauth2/userinfo";
62
+
63
+ return {
64
+ id: "salesforce",
65
+ name: "Salesforce",
66
+
67
+ async createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
68
+ if (!options.clientId || !options.clientSecret) {
69
+ logger.error(
70
+ "Client Id and Client Secret are required for Salesforce. Make sure to provide them in the options."
71
+ );
72
+ throw new PayvoAuthError("CLIENT_ID_AND_SECRET_REQUIRED");
73
+ }
74
+ if (!codeVerifier) {
75
+ throw new PayvoAuthError("codeVerifier is required for Salesforce");
76
+ }
77
+
78
+ const _scopes = options.disableDefaultScope
79
+ ? []
80
+ : ["openid", "email", "profile"];
81
+ if (options.scope) _scopes.push(...options.scope);
82
+ if (scopes) _scopes.push(...scopes);
83
+
84
+ return createAuthorizationURL({
85
+ id: "salesforce",
86
+ options,
87
+ authorizationEndpoint,
88
+ scopes: _scopes,
89
+ state,
90
+ codeVerifier,
91
+ redirectURI: options.redirectURI || redirectURI,
92
+ });
93
+ },
94
+
95
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
96
+ return validateAuthorizationCode({
97
+ code,
98
+ codeVerifier,
99
+ redirectURI: options.redirectURI || redirectURI,
100
+ options,
101
+ tokenEndpoint,
102
+ });
103
+ },
104
+
105
+ refreshAccessToken: options.refreshAccessToken
106
+ ? options.refreshAccessToken
107
+ : async (refreshToken) => {
108
+ return refreshAccessToken({
109
+ refreshToken,
110
+ options: {
111
+ clientId: options.clientId,
112
+ clientSecret: options.clientSecret,
113
+ },
114
+ tokenEndpoint,
115
+ });
116
+ },
117
+
118
+ async getUserInfo(token) {
119
+ if (options.getUserInfo) {
120
+ return options.getUserInfo(token);
121
+ }
122
+
123
+ try {
124
+ const { data: user } = await betterFetch<SalesforceProfile>(
125
+ userInfoEndpoint,
126
+ {
127
+ headers: {
128
+ Authorization: `Bearer ${token.accessToken}`,
129
+ },
130
+ }
131
+ );
132
+
133
+ if (!user) {
134
+ logger.error("Failed to fetch user info from Salesforce");
135
+ return null;
136
+ }
137
+
138
+ const userMap = await options.mapProfileToUser?.(user);
139
+
140
+ return {
141
+ user: {
142
+ id: user.user_id,
143
+ name: user.name,
144
+ email: user.email,
145
+ image: user.photos?.picture || user.photos?.thumbnail,
146
+ emailVerified: user.email_verified ?? false,
147
+ ...userMap,
148
+ },
149
+ data: user,
150
+ };
151
+ } catch (error) {
152
+ logger.error("Failed to fetch user info from Salesforce:", error);
153
+ return null;
154
+ }
155
+ },
156
+
157
+ options,
158
+ } satisfies OAuthProvider<SalesforceProfile>;
159
+ };
@@ -0,0 +1,111 @@
1
+ import { betterFetch } from "@better-fetch/fetch";
2
+ import type { OAuthProvider, ProviderOptions } from "../oauth2";
3
+ import { refreshAccessToken, validateAuthorizationCode } from "../oauth2";
4
+
5
+ export interface SlackProfile extends Record<string, any> {
6
+ ok: boolean;
7
+ sub: string;
8
+ "https://slack.com/user_id": string;
9
+ "https://slack.com/team_id": string;
10
+ email: string;
11
+ email_verified: boolean;
12
+ date_email_verified: number;
13
+ name: string;
14
+ picture: string;
15
+ given_name: string;
16
+ family_name: string;
17
+ locale: string;
18
+ "https://slack.com/team_name": string;
19
+ "https://slack.com/team_domain": string;
20
+ "https://slack.com/user_image_24": string;
21
+ "https://slack.com/user_image_32": string;
22
+ "https://slack.com/user_image_48": string;
23
+ "https://slack.com/user_image_72": string;
24
+ "https://slack.com/user_image_192": string;
25
+ "https://slack.com/user_image_512": string;
26
+ "https://slack.com/team_image_34": string;
27
+ "https://slack.com/team_image_44": string;
28
+ "https://slack.com/team_image_68": string;
29
+ "https://slack.com/team_image_88": string;
30
+ "https://slack.com/team_image_102": string;
31
+ "https://slack.com/team_image_132": string;
32
+ "https://slack.com/team_image_230": string;
33
+ "https://slack.com/team_image_default": boolean;
34
+ }
35
+
36
+ export interface SlackOptions extends ProviderOptions<SlackProfile> {
37
+ clientId: string;
38
+ }
39
+
40
+ export const slack = (options: SlackOptions) => {
41
+ return {
42
+ id: "slack",
43
+ name: "Slack",
44
+ createAuthorizationURL({ state, scopes, redirectURI }) {
45
+ const _scopes = options.disableDefaultScope
46
+ ? []
47
+ : ["openid", "profile", "email"];
48
+ if (scopes) _scopes.push(...scopes);
49
+ if (options.scope) _scopes.push(...options.scope);
50
+ const url = new URL("https://slack.com/openid/connect/authorize");
51
+ url.searchParams.set("scope", _scopes.join(" "));
52
+ url.searchParams.set("response_type", "code");
53
+ url.searchParams.set("client_id", options.clientId);
54
+ url.searchParams.set("redirect_uri", options.redirectURI || redirectURI);
55
+ url.searchParams.set("state", state);
56
+ return url;
57
+ },
58
+ validateAuthorizationCode: async ({ code, redirectURI }) => {
59
+ return validateAuthorizationCode({
60
+ code,
61
+ redirectURI,
62
+ options,
63
+ tokenEndpoint: "https://slack.com/api/openid.connect.token",
64
+ });
65
+ },
66
+ refreshAccessToken: options.refreshAccessToken
67
+ ? options.refreshAccessToken
68
+ : async (refreshToken) => {
69
+ return refreshAccessToken({
70
+ refreshToken,
71
+ options: {
72
+ clientId: options.clientId,
73
+ clientKey: options.clientKey,
74
+ clientSecret: options.clientSecret,
75
+ },
76
+ tokenEndpoint: "https://slack.com/api/openid.connect.token",
77
+ });
78
+ },
79
+ async getUserInfo(token) {
80
+ if (options.getUserInfo) {
81
+ return options.getUserInfo(token);
82
+ }
83
+ const { data: profile, error } = await betterFetch<SlackProfile>(
84
+ "https://slack.com/api/openid.connect.userInfo",
85
+ {
86
+ headers: {
87
+ authorization: `Bearer ${token.accessToken}`,
88
+ },
89
+ },
90
+ );
91
+
92
+ if (error) {
93
+ return null;
94
+ }
95
+
96
+ const userMap = await options.mapProfileToUser?.(profile);
97
+ return {
98
+ user: {
99
+ id: profile["https://slack.com/user_id"],
100
+ name: profile.name || "",
101
+ email: profile.email,
102
+ emailVerified: profile.email_verified,
103
+ image: profile.picture || profile["https://slack.com/user_image_512"],
104
+ ...userMap,
105
+ },
106
+ data: profile,
107
+ };
108
+ },
109
+ options,
110
+ } satisfies OAuthProvider<SlackProfile>;
111
+ };
@@ -0,0 +1,93 @@
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 SpotifyProfile {
10
+ id: string;
11
+ display_name: string;
12
+ email: string;
13
+ images: {
14
+ url: string;
15
+ }[];
16
+ }
17
+
18
+ export interface SpotifyOptions extends ProviderOptions<SpotifyProfile> {
19
+ clientId: string;
20
+ }
21
+
22
+ export const spotify = (options: SpotifyOptions) => {
23
+ return {
24
+ id: "spotify",
25
+ name: "Spotify",
26
+ createAuthorizationURL({ state, scopes, codeVerifier, redirectURI }) {
27
+ const _scopes = options.disableDefaultScope ? [] : ["user-read-email"];
28
+ if (options.scope) _scopes.push(...options.scope);
29
+ if (scopes) _scopes.push(...scopes);
30
+ return createAuthorizationURL({
31
+ id: "spotify",
32
+ options,
33
+ authorizationEndpoint: "https://accounts.spotify.com/authorize",
34
+ scopes: _scopes,
35
+ state,
36
+ codeVerifier,
37
+ redirectURI,
38
+ });
39
+ },
40
+ validateAuthorizationCode: async ({ code, codeVerifier, redirectURI }) => {
41
+ return validateAuthorizationCode({
42
+ code,
43
+ codeVerifier,
44
+ redirectURI,
45
+ options,
46
+ tokenEndpoint: "https://accounts.spotify.com/api/token",
47
+ });
48
+ },
49
+ refreshAccessToken: options.refreshAccessToken
50
+ ? options.refreshAccessToken
51
+ : async (refreshToken) => {
52
+ return refreshAccessToken({
53
+ refreshToken,
54
+ options: {
55
+ clientId: options.clientId,
56
+ clientKey: options.clientKey,
57
+ clientSecret: options.clientSecret,
58
+ },
59
+ tokenEndpoint: "https://accounts.spotify.com/api/token",
60
+ });
61
+ },
62
+ async getUserInfo(token) {
63
+ if (options.getUserInfo) {
64
+ return options.getUserInfo(token);
65
+ }
66
+ const { data: profile, error } = await betterFetch<SpotifyProfile>(
67
+ "https://api.spotify.com/v1/me",
68
+ {
69
+ method: "GET",
70
+ headers: {
71
+ Authorization: `Bearer ${token.accessToken}`,
72
+ },
73
+ },
74
+ );
75
+ if (error) {
76
+ return null;
77
+ }
78
+ const userMap = await options.mapProfileToUser?.(profile);
79
+ return {
80
+ user: {
81
+ id: profile.id,
82
+ name: profile.display_name,
83
+ email: profile.email,
84
+ image: profile.images[0]?.url,
85
+ emailVerified: false,
86
+ ...userMap,
87
+ },
88
+ data: profile,
89
+ };
90
+ },
91
+ options,
92
+ } satisfies OAuthProvider<SpotifyProfile>;
93
+ };