Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/roe.dev. This is the code and content for my personal website, built in Nuxt. roe.dev
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import type { RuntimeConfig } from 'nuxt/schema'
export default defineEventHandler(async event => { const config = useRuntimeConfig(event) const slug = getRouterParam(event, 'slug') const repo = slug && config.invites?.map?.[slug as keyof RuntimeConfig['invites']['map']] if (!repo) { throw createError({ statusCode: 404 }) }
const { code } = getQuery(event)
if (!code) { throw createError({ statusCode: 422, statusMessage: 'Missing authorisation code.', }) }
const { access_token, ...rest } = await $fetch<{ access_token: string }>( 'https://github.com/login/oauth/access_token', { method: 'POST', body: { client_id: config.public.githubClientId, client_secret: config.github.clientSecret, code, }, }, ).catch(err => { console.error('access', err) return {} as { access_token?: string } })
if (!access_token) { throw createError({ statusCode: 422, statusMessage: 'Authorisation code invalid.', }) }
console.log(access_token, config, rest)
const username = await query(access_token, `{ viewer { login } }`) .then(r => r.data?.viewer.login) .then(r => encodeURIComponent(r)) .catch(err => { console.error('viewer', err) return null })
if (!username) { throw createError({ statusCode: 422, statusMessage: 'Access code invalid.', }) }
try { const res = await $fetch( `https://api.github.com/repos/${repo}/collaborators/${username}`, { method: 'PUT', body: { permission: 'push' }, headers: { Accept: 'application/vnd.github+json', Authorization: `Bearer ${config.github.inviteToken}`, }, }, )
console.log({ res }) } catch (err) { console.log('could not add collaborator', err) } return sendRedirect(event, `https://github.com/${repo}/invitations`)})