site/pages/api/join.js
2023-03-01 23:57:35 +00:00

93 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import AirtablePlus from 'airtable-plus'
const joinTable = new AirtablePlus({
apiKey: process.env.AIRTABLE_API_KEY,
baseID: 'appaqcJtn33vb59Au',
tableName: 'Join Requests'
})
async function postData(url = '', data = {}, headers = {}) {
console.log(data)
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json',
...headers
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(data)
})
return response.text()
}
export default async function handler(req, res) {
if (req.method === 'OPTIONS') {
return res.status(200).send('YIPPE YAY. YOU HAVE CLEARANCE TO PROCEED.')
}
if (req.method === 'GET') {
return res
.status(405)
.json({ error: '*GET outta here!* (Method not allowed, use POST)' })
}
if (req.method === 'PUT') {
return res.status(405).json({
error: '*PUT that request away!* (Method not allowed, use POST)'
})
}
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed, use POST' })
}
const data = req.body || {}
const open = process.env.NEXT_PUBLIC_OPEN === 'true'
const waitlist = !open
const isAdult = data.educationLevel === 'tertiary'
const secrets = (process.env.NAUGHTY || '').split(',')
for (const secret of secrets) {
if (secret === req.headers['x-forwarded-for']) {
return res.json({
status: 'success',
message: 'Youve been invited to Slack!'
})
}
}
await joinTable.create({
'Full Name': data.name,
'Email Address': data.email,
Student: !isAdult,
Reason: data.reason,
Invited: !waitlist,
Club: data.club ? data.club : '',
IP: req.headers['x-forwarded-for'] || req.socket.remoteAddress
})
if (!waitlist) {
let result = await postData(
'https://toriel.hackclub.com/slack-invite',
{
email: data.email,
ip: req.headers['x-forwarded-for'] || req.socket.remoteAddress,
continent: data.continent,
teen: !isAdult,
educationLevel: data.educationLevel,
reason: data.reason,
userAgent: req.headers['user-agent']
},
{ authorization: `Bearer ${process.env.TORIEL_KEY}` }
)
console.log(result)
res.json({ status: 'success', message: 'Youve been invited to Slack!' })
} else {
res.json({
status: 'success',
message: 'Your request will be reviewed soon.'
})
}
}