forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
95 lines (88 loc) · 2.86 KB
/
auth.ts
File metadata and controls
95 lines (88 loc) · 2.86 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import NextAuth from "next-auth";
import GitHub from "next-auth/providers/github";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
callbacks: {
signIn: async (params) => {
if (params.profile!.sponsorInfo) {
// TODO
return true;
}
return true;
return "https://github.com/sponsors/TypeCellOS"; // TODO
},
},
providers: [
// copied from https://github.com/nextauthjs/next-auth/blob/234a150e2cac3bc62a9162fa00ed7cb16a105244/packages/core/src/providers/github.ts#L2,
// but with extra sponsorship api call
GitHub({
userinfo: {
url: `https://api.github.com/user`,
async request({ tokens, provider }: any) {
const profile = await fetch(provider.userinfo?.url as URL, {
headers: {
Authorization: `Bearer ${tokens.access_token}`,
"User-Agent": "authjs",
},
}).then(async (res) => await res.json());
if (!profile.email) {
// If the user does not have a public email, get another via the GitHub API
// See https://docs.github.com/en/rest/users/emails#list-public-email-addresses-for-the-authenticated-user
const res = await fetch(`https://api.github.com/user/emails`, {
headers: {
Authorization: `Bearer ${tokens.access_token}`,
"User-Agent": "authjs",
},
});
if (res.ok) {
const emails: any[] = await res.json();
profile.email = (
emails.find((e) => e.primary) ?? emails[0]
).email;
}
}
const resSponsor = await fetch(`https://api.github.com/graphql`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${tokens.access_token}`,
},
body: JSON.stringify({
query: `{
organization(login:"TypeCellOS") {
sponsorshipForViewerAsSponsor(activeOnly:false) {
isActive,
tier {
name
monthlyPriceInDollars
}
}
}
}`,
}),
});
if (resSponsor.ok) {
const data = await resSponsor.json();
profile.sponsorInfo =
data.data.organization.sponsorshipForViewerAsSponsor;
}
return profile;
},
},
profile: (profile) => {
return {
id: profile.id.toString(),
name: profile.name ?? profile.login,
email: profile.email,
image: profile.avatar_url,
username: profile.login,
sponsorInfo: profile.sponsorsTypeCell,
};
},
}),
],
});