-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub-provider.js
More file actions
62 lines (58 loc) · 1.95 KB
/
Copy pathgithub-provider.js
File metadata and controls
62 lines (58 loc) · 1.95 KB
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
// GitHub requires a descriptive User-Agent on API requests.
const GITHUB_API = "https://api.github.com";
const USER_AGENT = "codebar-auth";
/**
* GET a GitHub API endpoint with the user's access token.
* Returns parsed JSON, or null on a non-2xx response.
*/
async function githubFetch(path, accessToken) {
try {
const res = await fetch(`${GITHUB_API}${path}`, {
headers: {
Authorization: `Bearer ${accessToken}`,
"User-Agent": USER_AGENT,
Accept: "application/vnd.github+json",
},
});
if (!res.ok) return null;
return res.json();
} catch {
// Fail soft: a network error here must not turn the OAuth callback into a
// 500. Better Auth's default getUserInfo returns null on failure too.
return null;
}
}
/**
* Build the better-auth GitHub user from GitHub's profile + email endpoints.
*
* Resolves the email the same way the planner's legacy omniauth-github
* integration did: prefer the primary and verified address from
* /user/emails. Better Auth's default prioritises /user.email (the public
* profile email), which routinely differs from the primary account email and
* made returning users appear to be new signups.
*
* Returns the shape better-auth's github provider getUserInfo hook expects:
* `{ user, data }`.
*/
export async function getGithubUserInfo({ accessToken }) {
const [profile, emails] = await Promise.all([
githubFetch("/user", accessToken),
githubFetch("/user/emails", accessToken),
]);
const primary = emails?.find((e) => e.primary && e.verified);
const email = primary?.email || profile?.email || emails?.[0]?.email || "";
const emailVerified =
primary !== undefined
? true
: (emails?.some((e) => e.email === email && e.verified) ?? false);
return {
user: {
id: String(profile?.id ?? ""),
name: profile?.name || profile?.login || "",
email,
image: profile?.avatar_url,
emailVerified,
},
data: profile,
};
}