/
vk.ts
54 lines (51 loc) · 1.42 KB
/
vk.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
import type { OAuthConfig, OAuthUserConfig } from "."
export interface VkProfile {
response: Array<{
id: number
first_name: string
last_name: string
photo_100: string
can_access_closed: boolean
is_closed: boolean
}>
}
export default function VK<
P extends Record<string, any> = VkProfile
>(options: OAuthUserConfig<P>): OAuthConfig<P> {
const apiVersion = "5.126" // https://vk.com/dev/versions
return {
id: "vk",
name: "VK",
type: "oauth",
authorization: `https://oauth.vk.com/authorize?scope=email&v=${apiVersion}`,
token: {
url: `https://oauth.vk.com/access_token?v=${apiVersion}`,
async request({ client, params, checks, provider }) {
const response = await client.oauthCallback(
provider.callbackUrl,
params,
checks,
{
exchangeBody: {
client_id: options.clientId,
client_secret: options.clientSecret,
},
}
)
const { user_id, ...tokens } = response
return { tokens }
},
},
userinfo: `https://api.vk.com/method/users.get?fields=photo_100&v=${apiVersion}`,
profile(result: P) {
const profile = result.response?.[0] ?? {}
return {
id: profile.id,
name: [profile.first_name, profile.last_name].filter(Boolean).join(" "),
email: null,
image: profile.photo_100,
}
},
options,
}
}