@stravigor/socialite 0.1.0 → 0.1.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.
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,4 +4,6 @@ export { AbstractProvider, SocialiteError } from './abstract_provider.ts'
|
|
|
4
4
|
export { GoogleProvider } from './providers/google_provider.ts'
|
|
5
5
|
export { GitHubProvider } from './providers/github_provider.ts'
|
|
6
6
|
export { DiscordProvider } from './providers/discord_provider.ts'
|
|
7
|
+
export { FacebookProvider } from './providers/facebook_provider.ts'
|
|
8
|
+
export { LinkedInProvider } from './providers/linkedin_provider.ts'
|
|
7
9
|
export type { SocialiteUser, SocialiteConfig, ProviderConfig, TokenResponse } from './types.ts'
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ExternalServiceError } from '@stravigor/core/exceptions/errors'
|
|
2
|
+
import { AbstractProvider } from '../abstract_provider.ts'
|
|
3
|
+
import type { SocialiteUser } from '../types.ts'
|
|
4
|
+
|
|
5
|
+
const API_VERSION = 'v21.0'
|
|
6
|
+
|
|
7
|
+
export class FacebookProvider extends AbstractProvider {
|
|
8
|
+
readonly name = 'Facebook'
|
|
9
|
+
|
|
10
|
+
protected getDefaultScopes(): string[] {
|
|
11
|
+
return ['email', 'public_profile']
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
protected getAuthUrl(): string {
|
|
15
|
+
return `https://www.facebook.com/${API_VERSION}/dialog/oauth`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
protected getTokenUrl(): string {
|
|
19
|
+
return `https://graph.facebook.com/${API_VERSION}/oauth/access_token`
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
protected async getUserByToken(token: string): Promise<Record<string, unknown>> {
|
|
23
|
+
const fields = 'id,name,email,picture.type(large)'
|
|
24
|
+
const response = await fetch(
|
|
25
|
+
`https://graph.facebook.com/${API_VERSION}/me?fields=${fields}&access_token=${token}`
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
if (!response.ok) {
|
|
29
|
+
throw new ExternalServiceError('Facebook', response.status, await response.text())
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return (await response.json()) as Record<string, unknown>
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
protected mapUserToObject(data: Record<string, unknown>): SocialiteUser {
|
|
36
|
+
const picture = data.picture as { data?: { url?: string } } | undefined
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
id: data.id as string,
|
|
40
|
+
name: (data.name as string) ?? null,
|
|
41
|
+
email: (data.email as string) ?? null,
|
|
42
|
+
avatar: picture?.data?.url ?? null,
|
|
43
|
+
nickname: null,
|
|
44
|
+
token: '',
|
|
45
|
+
refreshToken: null,
|
|
46
|
+
expiresIn: null,
|
|
47
|
+
approvedScopes: [],
|
|
48
|
+
raw: data,
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ExternalServiceError } from '@stravigor/core/exceptions/errors'
|
|
2
|
+
import { AbstractProvider } from '../abstract_provider.ts'
|
|
3
|
+
import type { SocialiteUser } from '../types.ts'
|
|
4
|
+
|
|
5
|
+
export class LinkedInProvider extends AbstractProvider {
|
|
6
|
+
readonly name = 'LinkedIn'
|
|
7
|
+
|
|
8
|
+
protected getDefaultScopes(): string[] {
|
|
9
|
+
return ['openid', 'profile', 'email']
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
protected getAuthUrl(): string {
|
|
13
|
+
return 'https://www.linkedin.com/oauth/v2/authorization'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
protected getTokenUrl(): string {
|
|
17
|
+
return 'https://www.linkedin.com/oauth/v2/accessToken'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
protected async getUserByToken(token: string): Promise<Record<string, unknown>> {
|
|
21
|
+
const response = await fetch('https://api.linkedin.com/v2/userinfo', {
|
|
22
|
+
headers: { Authorization: `Bearer ${token}` },
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
if (!response.ok) {
|
|
26
|
+
throw new ExternalServiceError('LinkedIn', response.status, await response.text())
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return (await response.json()) as Record<string, unknown>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected mapUserToObject(data: Record<string, unknown>): SocialiteUser {
|
|
33
|
+
return {
|
|
34
|
+
id: data.sub as string,
|
|
35
|
+
name: (data.name as string) ?? null,
|
|
36
|
+
email: (data.email as string) ?? null,
|
|
37
|
+
avatar: (data.picture as string) ?? null,
|
|
38
|
+
nickname: null,
|
|
39
|
+
token: '',
|
|
40
|
+
refreshToken: null,
|
|
41
|
+
expiresIn: null,
|
|
42
|
+
approvedScopes: [],
|
|
43
|
+
raw: data,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/socialite_manager.ts
CHANGED
|
@@ -6,6 +6,8 @@ import type { ProviderConfig, SocialiteConfig } from './types.ts'
|
|
|
6
6
|
import { GoogleProvider } from './providers/google_provider.ts'
|
|
7
7
|
import { GitHubProvider } from './providers/github_provider.ts'
|
|
8
8
|
import { DiscordProvider } from './providers/discord_provider.ts'
|
|
9
|
+
import { FacebookProvider } from './providers/facebook_provider.ts'
|
|
10
|
+
import { LinkedInProvider } from './providers/linkedin_provider.ts'
|
|
9
11
|
|
|
10
12
|
@inject
|
|
11
13
|
export default class SocialiteManager {
|
|
@@ -49,6 +51,10 @@ export default class SocialiteManager {
|
|
|
49
51
|
return new GitHubProvider(providerConfig)
|
|
50
52
|
case 'discord':
|
|
51
53
|
return new DiscordProvider(providerConfig)
|
|
54
|
+
case 'facebook':
|
|
55
|
+
return new FacebookProvider(providerConfig)
|
|
56
|
+
case 'linkedin':
|
|
57
|
+
return new LinkedInProvider(providerConfig)
|
|
52
58
|
default:
|
|
53
59
|
throw new ConfigurationError(
|
|
54
60
|
`Unknown socialite driver "${driverName}". Register it with SocialiteManager.extend().`
|