feat: add invite link for easier onboarding (#481)

This commit is contained in:
Daniel Roe 2022-12-20 22:39:04 +00:00 committed by GitHub
parent dfc24370d8
commit 3907199a54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 0 deletions

View file

@ -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=

View file

@ -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',

View file

@ -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,
)
})