diff --git a/src/index.js b/src/index.js index c19278b..2dc529c 100644 --- a/src/index.js +++ b/src/index.js @@ -12,8 +12,24 @@ const PORT = process.env.PORT || 1234 const app = express() app.get('/', async (req, res) => { + const formUrl = 'https://airtable.com/shrNMxeoANyxtVY8U' + try { const code = req.query.code + + if (!code) { + // If there isn't any code the user might be coming to us directly instead of redirecting from GitHub's OAuth + // Let's send this wayward traveler back to the right path + + console.log( + 'Got request without a code! Redirecting them to hack.af/pack' + ) + res.redirect(302, 'https://hack.af/pack') + return + } + + console.log(`Got request with code '${code}`) + const ghResponse = await request( 'POST https://github.com/login/oauth/access_token', { @@ -32,12 +48,22 @@ app.get('/', async (req, res) => { headers: { Authorization: `token ${accessToken}` }, }) - res.redirect( - 302, - `https://airtable.com/shrNMxeoANyxtVY8U?prefill_GitHub%20Username=${ghUser.data.login}` - ) + const username = ghUser.data && ghUser.data.login + if (username) { + console.log( + `GitHub tells me that this user is https://github.com/${username}. I'll redirect them to the Airtable` + ) + res.redirect(302, `${formUrl}?prefill_GitHub%20Username=${username}`) + } else { + console.log( + "GitHub doesn't recognize this user. I'll just send them to the Airtable with nothing prefilled" + ) + res.redirect(302, formUrl) + } } catch (e) { console.error(e) + console.log("Something broke, so I'll just redirect straight to Airtable") + res.redirect(302, formUrl) } })