forked from hackclub/tonic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
131 lines (122 loc) · 3.79 KB
/
server.js
File metadata and controls
131 lines (122 loc) · 3.79 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const express = require('express');
const morgan = require('morgan');
const { promisify } = require('util');
const exec = promisify(require('child_process').exec)
const fs = require('fs/promises');
const crypto = require('crypto');
const cookieParser = require('cookie-parser');
const app = express();
app.use(express.json());
app.use(morgan('dev'));
app.use(cookieParser());
const port = process.env.PORT || 3000;
const redirect_url = process.env.NODE_ENV === 'production'
? process.env.PRODUCTION_REDIRECT_URL
: `http://localhost:${process.env.PORT}`
// static files
app.use('/assets', express.static('assets'));
app.use('/learn', express.static('learn'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/index.css', (req, res) => {
res.sendFile(__dirname + '/index.css');
});
app.get('/style.css', (req, res) => {
res.sendFile(__dirname + '/style.css');
});
app.get('/attribution.js', (req, res) => {
res.sendFile(__dirname + '/attribution.js');
});
app.get('/index.js', (req, res) => {
res.sendFile(__dirname + '/index.js');
});
app.get("/favicon.svg", (req, res) => {
res.sendFile(__dirname + '/favicon.svg');
});
app.get('/auth', (req, res) => {
res.json({ auth: !!req.cookies.uid });
});
app.get('/auth/slack', async (req, res) => {
const R = await fetch(`https://slack.com/api/oauth.v2.access?code=${req.query.code}&client_id=${process.env.CLIENT_ID}&client_secret=${process.env.CLIENT_SECRET}&redirect_uri=${redirect_url}/auth/slack`, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST',
}).then(R => R.json());
if (R.ok && R.authed_user?.access_token) {
res.cookie('uid', R.authed_user.id, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
// sameSite: 'lax'
});
// Make internal request to /scrap
// await fetch(`${redirect_url}/scrap`, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// 'Cookie': `uid=${R.authed_user.id}`
// },
// body: JSON.stringify({
// task: 'Login',
// text_entry: '-'
// })
// });
}
res.redirect('/');
});
app.post('/scrap', async (req, res) => {
const task = req.body.task;
const text_entry = req.body.text_entry;
const R = await fetch(`https://api.airtable.com/v0/${process.env.AIRTABLE_BASE_ID}/${process.env.AIRTABLE_SCRAPS_TABLE_ID}`, {
headers: {
'Authorization': `Bearer ${process.env.AIRTABLE_PAT}`,
'Content-Type': 'application/json'
},
// method: 'PATCH',
method: 'POST',
body: JSON.stringify({
// performUpsert: { fieldsToMergeOn: ['Slack ID'] },
records: [
{
fields: {
'Slack ID': req.cookies.uid,
'Task': task,
'Text Entry': text_entry,
},
},
],
}),
}).then(R => R.json());
console.log(R);
if (R.error) {
res.status(500).json({ success: false })
} else {
res.status(200).json({ success: true });
}
});
app.get('/scraps', async (req, res) => {
const R = await fetch(`https://api.airtable.com/v0/${process.env.AIRTABLE_BASE_ID}/Scraps?fields[]=Task&filterByFormula={Slack ID}="${req.cookies.uid}"`, {
headers: {
'Authorization': `Bearer ${process.env.AIRTABLE_PAT}`,
'Content-Type': 'application/json'
},
method: 'GET',
}).then(R => R.json());
console.log(R);
if (R.error) {
res.status(500).json({ success: false })
} else {
res.status(200).json({ success: true, ...R });
}
});
app.get('/auth/logout', (req, res) => {
res.cookie('uid', '', {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
// sameSite: 'lax',
expires: new Date(0)
});
res.redirect('/');
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});