forked from wotschofsky/slack-to-socials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
73 lines (61 loc) · 2.19 KB
/
auth.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
import '@std/dotenv/load';
import { serve } from 'https://deno.land/[email protected]/http/server.ts';
import { TwitterApi } from 'npm:twitter-api-v2';
const clientId = Deno.env.get('TWITTER_CLIENT_ID');
const clientSecret = Deno.env.get('TWITTER_CLIENT_SECRET');
const redirectUri = 'http://localhost:8000/callback';
if (!clientId || !clientSecret) {
console.error(
'Please set TWITTER_CLIENT_ID and TWITTER_CLIENT_SECRET in your .env file'
);
Deno.exit(1);
}
const client = new TwitterApi({ clientId, clientSecret });
let codeVerifier: string;
let state: string;
async function handleRequest(request: Request): Promise<Response> {
const url = new URL(request.url);
if (url.pathname === '/') {
const {
url: authUrl,
codeVerifier: newCodeVerifier,
state: newState,
} = client.generateOAuth2AuthLink(redirectUri, {
scope: ['tweet.read', 'tweet.write', 'users.read', 'offline.access'],
});
codeVerifier = newCodeVerifier;
state = newState;
return new Response(`<a href="${authUrl}">Authorize with Twitter</a>`, {
headers: { 'content-type': 'text/html' },
});
} else if (url.pathname === '/callback') {
const code = url.searchParams.get('code');
const returnedState = url.searchParams.get('state');
if (!code || !returnedState || returnedState !== state) {
return new Response('Invalid callback', { status: 400 });
}
try {
const { accessToken, refreshToken } = await client.loginWithOAuth2({
code,
codeVerifier,
redirectUri,
});
// Here you would typically save these tokens securely
console.log('Access Token:', accessToken);
console.log('Refresh Token:', refreshToken);
return new Response(
'Authorization successful! Check your console for tokens.',
{
headers: { 'content-type': 'text/html' },
}
);
} catch (error) {
console.error('Error during token exchange:', error);
return new Response('Error during authorization', { status: 500 });
}
} else {
return new Response('Not found', { status: 404 });
}
}
console.log('Server running on http://localhost:8000');
await serve(handleRequest, { port: 8000 });