-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.ts
79 lines (65 loc) · 2.42 KB
/
proxy.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
/* eslint-disable no-console */
import axios from 'axios';
import dotenv from 'dotenv';
import express, { Request, Response } from 'express';
dotenv.config();
const main = async () => {
const app = express();
app.get('/api/auth/proxy-expo-go', async (req: Request, res: Response) => {
// This is fragile and won't work in many cases. It's just an example. Physical devices, and android emulators will need the full IP address instead of localhost.
// This also assumes the dev server is running on port 8081.
const redirectUri = `exp://localhost:8081/--/?${new URL(req.url, 'http://a').searchParams}`;
console.log(`Redirect to expo-go app -> ${redirectUri}`);
return res.status(302).redirect(redirectUri);
});
app.get('/api/auth/proxy', async (req: Request, res: Response) => {
// const redirectUri = `${APP_SCHEME_NAME}://?${new URL(req.url, `foam://`).searchParams}`;
const redirectUri = `foam://?${new URL(req.url, 'http://a').searchParams}`;
console.info(`redirecting to app with redirect_uri -> ${redirectUri}`);
return res.status(302).redirect(redirectUri);
});
app.get(
'/api/auth/proxy/default-token',
// @ts-expect-error express type issue
async (req: Request, res: Response) => {
const { data } = await axios.post(
'https://id.twitch.tv/oauth2/token',
null,
{
params: {
client_id: process.env.EXPO_PUBLIC_TWITCH_CLIENT_ID,
client_secret: process.env.EXPO_PUBLIC_TWITCH_CLIENT_SECRET,
grant_type: 'client_credentials',
},
headers: {
'Content-Type': 'x-www-form-urlencoded',
},
},
);
console.info('serving token ->', JSON.stringify(data, null, 2));
return res.status(200).json(data);
},
);
// @ts-expect-error - express type definitions are incorrect
app.get('/api/auth/pending', async (req: Request, res: Response) => {
return res.status(200).send(`
<html>
<head>
<title>Redirecting...</title>
</head>
<body>
<h1>Redirecting...</h1>
<script>
setTimeout(() => {
window.location.href = 'foam://?${new URL(req.url, 'http://foam/').searchParams}';
}, 1000);
</script>
</body>
`);
});
const PORT = 4000;
app.listen(PORT, () => {
console.info(`Auth proxy server started on port: ${PORT}`);
});
};
main();