-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
tiktok.ts
267 lines (263 loc) · 8.23 KB
/
tiktok.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**
* <div style={{backgroundColor: "#000", display: "flex", justifyContent: "space-between", color: "#fff", padding: 16}}>
* <span>Built-in <b>TikTok</b> integration.</span>
* <a href="https://www.tiktok.com/">
* <img style={{display: "block"}} src="https://authjs.dev/img/providers/tiktok.svg" height="48" />
* </a>
* </div>
*
* @module providers/tiktok
*/
import { TokenSet } from "../types.js"
import type { OAuthConfig, OAuthUserConfig } from "./index.js"
/**
* [More info](https://developers.tiktok.com/doc/tiktok-api-v2-get-user-info/)
*/
export interface TiktokProfile extends Record<string, any> {
data: {
user: {
/**
* The unique identification of the user in the current application.Open id
* for the client.
*
* To return this field, add `fields=open_id` in the user profile request's query parameter.
*/
open_id: string
/**
* The unique identification of the user across different apps for the same developer.
* For example, if a partner has X number of clients,
* it will get X number of open_id for the same TikTok user,
* but one persistent union_id for the particular user.
*
* To return this field, add `fields=union_id` in the user profile request's query parameter.
*/
union_id?: string
/**
* User's profile image.
*
* To return this field, add `fields=avatar_url` in the user profile request's query parameter.
*/
avatar_url: string
/**
* User`s profile image in 100x100 size.
*
* To return this field, add `fields=avatar_url_100` in the user profile request's query parameter.
*/
avatar_url_100?: string
/**
* User's profile image with higher resolution
*
* To return this field, add `fields=avatar_url_100` in the user profile request's query parameter.
*/
avatar_large_url?: string
/**
* User's profile name
*
* To return this field, add `fields=display_name` in the user profile request's query parameter.
*/
display_name: string
/**
* User's username.
*
* To return this field, add `fields=username` in the user profile request's query parameter.
*/
username: string
/** @note Email is currently unsupported by TikTok */
email?: string
/**
* User's bio description if there is a valid one.
*
* To return this field, add `fields=bio_description` in the user profile request's query parameter.
*/
bio_description?: string
/**
* The link to user's TikTok profile page.
*
* To return this field, add `fields=profile_deep_link` in the user profile request's query parameter.
*/
profile_deep_link?: string
/**
* Whether TikTok has provided a verified badge to the account after confirming
* that it belongs to the user it represents.
*
* To return this field, add `fields=is_verified` in the user profile request's query parameter.
*/
is_verified?: boolean
/**
* User's followers count.
*
* To return this field, add `fields=follower_count` in the user profile request's query parameter.
*/
follower_count?: number
/**
* The number of accounts that the user is following.
*
* To return this field, add `fields=following_count` in the user profile request's query parameter.
*/
following_count?: number
/**
* The total number of likes received by the user across all of their videos.
*
* To return this field, add `fields=likes_count` in the user profile request's query parameter.
*/
likes_count?: number
/**
* The total number of publicly posted videos by the user.
*
* To return this field, add `fields=video_count` in the user profile request's query parameter.
*/
video_count?: number
}
}
error: {
/**
* The error category in string.
*/
code: string
/**
* The error message in string.
*/
message: string
/**
* The error message in string.
*/
log_id: string
}
}
/**
* Add TikTok login to your page.
*
* ### Setup
*
* #### Callback URL
* ```
* https://example.com/api/auth/callback/tiktok
* ```
*
* #### Configuration
*```ts
* import { Auth } from "@auth/core"
* import TikTok from "@auth/core/providers/tiktok"
*
* const request = new Request(origin)
* const response = await Auth(request, {
* providers: [
* TikTok({ clientId: TIKTOK_CLIENT_KEY, clientSecret: TIKTOK_CLIENT_SECRET }),
* ],
* })
* ```
*
* ### Resources
* - [TikTok app console](https://developers.tiktok.com/)
* - [TikTok login kit documentation](https://developers.tiktok.com/doc/login-kit-web/)
* - [Avaliable Scopes](https://developers.tiktok.com/doc/tiktok-api-scopes/)
*
*
* ### Notes
*
* :::tip
*
* Production applications cannot use localhost URLs to sign in with TikTok. You need add the domain and Callback/Redirect url's to your TikTok app and have them review and approved by the TikTok Team.
*
* :::
*
* :::tip
*
* Email address is not supported by TikTok.
*
* :::
*
* :::tip
*
* Client_ID will be the Client Key in the TikTok Application
*
* :::
*
* By default, Auth.js assumes that the TikTok provider is
* based on the [OAuth 2](https://www.rfc-editor.org/rfc/rfc6749.html) specification.
*
* :::tip
*
* The TikTok provider comes with a [default configuration](https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/providers/tiktok.ts).
* To override the defaults for your use case, check out [customizing a built-in OAuth provider](https://authjs.dev/guides/configuring-oauth-providers).
*
* :::
*
* :::info **Disclaimer**
*
* If you think you found a bug in the default configuration, you can [open an issue](https://authjs.dev/new/provider-issue).
*
* Auth.js strictly adheres to the specification and it cannot take responsibility for any deviation from
* the spec by the provider. You can open an issue, but if the problem is non-compliance with the spec,
* we might not pursue a resolution. You can ask for more help in [Discussions](https://authjs.dev/new/github-discussions).
*
* :::
*/
export default function TikTok<P extends TiktokProfile>(
options: OAuthUserConfig<P>
): OAuthConfig<P> {
return {
id: "tiktok",
name: "TikTok",
type: "oauth",
authorization: {
url: "https://www.tiktok.com/v2/auth/authorize",
params: {
client_key: options.clientId,
scope: "user.info.profile",
response_type: "code",
},
},
token: {
async request({ params, provider }) {
const res = await fetch(`https://open.tiktokapis.com/v2/oauth/token/`, {
method: "POST",
headers: {
"Cache-Control": "no-cache",
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
client_key: provider.clientId!,
client_secret: provider.clientSecret!,
code: params.code!,
grant_type: "authorization_code",
redirect_uri: provider.callbackUrl!,
}),
}).then((res) => res.json())
const tokens: TokenSet = {
access_token: res.access_token,
expires_at: res.expires_in,
refresh_token: res.refresh_token,
scope: res.scope,
id_token: res.open_id,
token_type: res.token_type,
session_state: res.open_id,
}
return {
tokens,
}
},
},
userinfo: {
url: "https://open.tiktokapis.com/v2/user/info/?fields=open_id,avatar_url,display_name,username",
async request({ tokens, provider }) {
return await fetch(provider.userinfo?.url as URL, {
headers: { Authorization: `Bearer ${tokens.access_token}` },
}).then(async (res) => await res.json())
},
},
profile(profile) {
return {
id: profile.data.user.open_id,
name: profile.data.user.display_name,
image: profile.data.user.avatar_url,
email: profile.data.user.email || null,
}
},
style: {
bg: "#000",
text: "#fff",
},
options,
}
}