|
| 1 | +import { OAuthConfig, OAuthUserConfig } from "." |
| 2 | + |
| 3 | +interface Identifier { |
| 4 | + identifier: string |
| 5 | +} |
| 6 | + |
| 7 | +interface Element { |
| 8 | + identifiers?: Identifier[] |
| 9 | +} |
| 10 | + |
| 11 | +export interface LinkedInProfile { |
| 12 | + id: string |
| 13 | + localizedFirstName: string |
| 14 | + localizedLastName: string |
| 15 | + profilePicture: { |
| 16 | + "displayImage~": { |
| 17 | + elements?: Element[] |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export default function LinkedIn< |
| 23 | + P extends Record<string, any> = LinkedInProfile |
| 24 | +>(options: OAuthUserConfig<P>): OAuthConfig<P> { |
| 25 | + return { |
| 26 | + id: "linkedin", |
| 27 | + name: "LinkedIn", |
| 28 | + type: "oauth", |
| 29 | + authorization: { |
| 30 | + url: "https://www.linkedin.com/oauth/v2/authorization", |
| 31 | + params: { scope: "r_liteprofile r_emailaddress" }, |
| 32 | + }, |
| 33 | + token: "https://www.linkedin.com/oauth/v2/accessToken", |
| 34 | + userinfo: { |
| 35 | + url: "https://api.linkedin.com/v2/me", |
| 36 | + params: { |
| 37 | + projection: `(id,localizedFirstName,localizedLastName,profilePicture(displayImage~digitalmediaAsset:playableStreams))`, |
| 38 | + }, |
| 39 | + }, |
| 40 | + async profile(profile, tokens) { |
| 41 | + const emailResponse = await fetch( |
| 42 | + "https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))", |
| 43 | + { headers: { Authorization: `Bearer ${tokens.access_token}` } } |
| 44 | + ) |
| 45 | + const emailData = await emailResponse.json() |
| 46 | + return { |
| 47 | + id: profile.id, |
| 48 | + name: `${profile.localizedFirstName} ${profile.localizedLastName}`, |
| 49 | + email: emailData?.elements?.[0]?.["handle~"]?.emailAddress, |
| 50 | + image: |
| 51 | + profile.profilePicture?.["displayImage~"]?.elements?.[0] |
| 52 | + ?.identifiers?.[0]?.identifier, |
| 53 | + } |
| 54 | + }, |
| 55 | + options, |
| 56 | + } |
| 57 | +} |
0 commit comments