Skip to content

Commit 1d52600

Browse files
fix(provider): refactor LinkedIn provider (#2821)
1 parent 9693277 commit 1d52600

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

app/components/header.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export default function Header() {
4444
<span className={styles.signedInText}>
4545
<small>Signed in as</small>
4646
<br />
47-
<strong>{session.user.email || session.user.name}</strong>
47+
<strong>{session.user.email} </strong>
48+
{session.user.name ? `(${session.user.name})` : null}
4849
</span>
4950
<a
5051
href="/api/auth/signout"

src/providers/linkedin.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/providers/linkedin.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)