-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
84 lines (72 loc) · 3.41 KB
/
index.ts
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
import type { OAuth2Adapter } from "adminforth";
export default class AdminForthAdapterFacebookOauth2 implements OAuth2Adapter {
private clientID: string;
private clientSecret: string;
constructor(options: {
clientID: string;
clientSecret: string;
}) {
this.clientID = options.clientID;
this.clientSecret = options.clientSecret;
}
getAuthUrl(): string {
const params = new URLSearchParams({
client_id: this.clientID,
response_type: 'code',
scope: 'email,public_profile',
redirect_uri: 'http://localhost:3000/oauth/callback'
});
return `https://www.facebook.com/v22.0/dialog/oauth?${params.toString()}`;
}
async getTokenFromCode(code: string, redirect_uri: string): Promise<{ email: string; }> {
// Exchange code for token
const tokenResponse = await fetch('https://graph.facebook.com/v22.0/oauth/access_token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
code,
client_id: this.clientID,
client_secret: this.clientSecret,
redirect_uri,
}),
});
const tokenData = await tokenResponse.json();
if (tokenData.error) {
console.error('Token error:', tokenData);
throw new Error(tokenData.error.message);
}
// Get user info using access token
const userResponse = await fetch(`https://graph.facebook.com/me?fields=id,email&access_token=${tokenData.access_token}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
const userData = await userResponse.json();
if (userData.error) {
throw new Error(userData.error.message);
}
return {
email: userData.email,
};
}
getIcon(): string {
return `<svg xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" version="1.1" id="svg9" viewBox="0 0 666.66668 666.66717">
<defs id="defs13">
<clipPath clipPathUnits="userSpaceOnUse" id="clipPath25">
<path d="M 0,700 H 700 V 0 H 0 Z" id="path23"/>
</clipPath>
</defs>
<g id="g17" transform="matrix(1.3333333,0,0,-1.3333333,-133.33333,799.99999)">
<g id="g19">
<g id="g21" clip-path="url(#clipPath25)">
<g id="g27" transform="translate(600,350)">
<path d="m 0,0 c 0,138.071 -111.929,250 -250,250 -138.071,0 -250,-111.929 -250,-250 0,-117.245 80.715,-215.622 189.606,-242.638 v 166.242 h -51.552 V 0 h 51.552 v 32.919 c 0,85.092 38.508,124.532 122.048,124.532 15.838,0 43.167,-3.105 54.347,-6.211 V 81.986 c -5.901,0.621 -16.149,0.932 -28.882,0.932 -40.993,0 -56.832,-15.528 -56.832,-55.9 V 0 h 81.659 l -14.028,-76.396 h -67.631 V -248.169 C -95.927,-233.218 0,-127.818 0,0" style="fill:#0866ff;fill-opacity:1;fill-rule:nonzero;stroke:none" id="path29"/>
</g>
<g id="g31" transform="translate(447.9175,273.6036)">
<path d="M 0,0 14.029,76.396 H -67.63 v 27.019 c 0,40.372 15.838,55.899 56.831,55.899 12.733,0 22.981,-0.31 28.882,-0.931 v 69.253 c -11.18,3.106 -38.509,6.212 -54.347,6.212 -83.539,0 -122.048,-39.441 -122.048,-124.533 V 76.396 h -51.552 V 0 h 51.552 v -166.242 c 19.343,-4.798 39.568,-7.362 60.394,-7.362 10.254,0 20.358,0.632 30.288,1.831 L -67.63,0 Z" style="fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none" id="path33"/>
</g>
</g>
</g>
</g>
<script xmlns=""/></svg>`;
}
}