From 3907199a546572ddd7b3afdab0e1510bc8f24384 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 20 Dec 2022 22:39:04 +0000 Subject: [PATCH] feat: add invite link for easier onboarding (#481) --- .env.example | 4 +++ nuxt.config.ts | 9 +++++ server/routes/invite.get.ts | 66 +++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 server/routes/invite.get.ts diff --git a/.env.example b/.env.example index b99ca071..b60c2769 100644 --- a/.env.example +++ b/.env.example @@ -10,3 +10,7 @@ NUXT_STORAGE_DRIVER= NUXT_STORAGE_FS_BASE= NUXT_PUBLIC_DISABLE_VERSION_CHECK= + +NUXT_GITHUB_CLIENT_ID= +NUXT_GITHUB_CLIENT_SECRET= +NUXT_GITHUB_INVITE_TOKEN= diff --git a/nuxt.config.ts b/nuxt.config.ts index 408d5d40..d47589f8 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -71,6 +71,15 @@ export default defineNuxtConfig({ namespaceId: '', apiToken: '', }, + discord: { + inviteUrl: 'https://chat.elk.zone', + }, + github: { + // oauth flow + clientId: '', + clientSecret: '', + inviteToken: '', + }, public: { env: isCI ? isPreview ? 'staging' : 'production' : 'local', pwaEnabled: !isDevelopment || process.env.VITE_DEV_PWA === 'true', diff --git a/server/routes/invite.get.ts b/server/routes/invite.get.ts new file mode 100644 index 00000000..50aa93a3 --- /dev/null +++ b/server/routes/invite.get.ts @@ -0,0 +1,66 @@ +const query = (accessToken: string, query: string) => + $fetch<{ data: any }>('https://api.github.com/graphql', { + method: 'POST', + headers: { Authorization: `Bearer ${accessToken}` }, + body: { query }, + }) + +export default defineEventHandler(async (event) => { + const { code } = getQuery(event) + + const config = useRuntimeConfig() + + if (!code) { + const redirect = `&redirect_uri=${config.deployUrl}/invite` + const loginURL = `https://github.com/login/oauth/authorize?client_id=${config.github.clientId}${redirect}` + await sendRedirect(event, loginURL) + return + } + + const { access_token } = await $fetch<{ access_token: string }>( + 'https://github.com/login/oauth/access_token', + { + method: 'POST', + body: { + client_id: config.github.clientId, + client_secret: config.github.clientSecret, + code, + }, + }, + ) + + if (!access_token) { + throw createError({ + statusCode: 422, + statusMessage: 'Authorisation code invalid.', + }) + } + + const id = await query(access_token, '{ viewer { databaseId } }') + .then(r => r.data?.viewer.databaseId) + + if (!id) { + throw createError({ + statusCode: 422, + statusMessage: 'Access code invalid.', + }) + } + + await $fetch( + 'https://api.github.com/orgs/elk-zone/invitations', + { + method: 'POST', + body: { invitee_id: id, role: 'direct_member', team_ids: [7042932] }, + headers: { + 'Accept': 'application/vnd.github+json', + 'Authorization': `Bearer ${config.github.inviteToken}`, + 'X-GitHub-Api-Version': '2022-11-28', + }, + }, + ) + + return sendRedirect( + event, + config.discord.inviteUrl, + ) +})