Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Handle users directly going to pack.hackclub.com
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwofford committed Oct 15, 2019
1 parent f589cb1 commit 6816111
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
{
Expand All @@ -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)
}
})

Expand Down

0 comments on commit 6816111

Please sign in to comment.