From 0abe98c74a9975d7b2f27dec838f03a99cf32992 Mon Sep 17 00:00:00 2001 From: Igor Zalutski Date: Thu, 31 Oct 2024 10:46:50 -0700 Subject: [PATCH] Fix auth in github callback --- src/app/api/github-callback/route.ts | 41 ++++++---------------------- src/data/user/organizations.ts | 18 ------------ 2 files changed, 8 insertions(+), 51 deletions(-) diff --git a/src/app/api/github-callback/route.ts b/src/app/api/github-callback/route.ts index 8048bf05..6397f6a9 100644 --- a/src/app/api/github-callback/route.ts +++ b/src/app/api/github-callback/route.ts @@ -1,6 +1,5 @@ -import { createSupabaseUserRouteHandlerClient } from '@/supabase-clients/user/createSupabaseUserRouteHandlerClient'; +import { getDefaultOrganization } from '@/data/user/organizations'; import { toSiteURL } from '@/utils/helpers'; -import { getSession } from '@/utils/server/verifySession'; import { NextRequest, NextResponse } from 'next/server'; // Use the environment variable for the callback URL @@ -27,7 +26,13 @@ export async function GET(request: NextRequest) { 'Trying to get org id for the following installation ID:', installationId, ); - const organizationId = await getOrganizationId(); + // always installing github app in the default organisation + // TODO install into user's current organisation + const organizationId = await getDefaultOrganization(); + if (!organizationId) { + console.error('User has no org id'); + throw new Error(`User has no org id. Installation: ${installationId}`); + } const response = await fetch( `${GITHUB_CALLBACK_URL}?${searchParams.toString()}`, { @@ -49,33 +54,3 @@ export async function GET(request: NextRequest) { return NextResponse.redirect(toSiteURL('/github_app/error')); } } - -async function getOrganizationId(): Promise { - const supabase = createSupabaseUserRouteHandlerClient(); - const { - data: { user }, - error, - } = await supabase.auth.getUser(); - if (error || !user?.id) { - console.error('Failed to get current user', error); - throw error; - } - - const session = await getSession(); - const userId = session.data.session?.user.id; - if (userId === undefined) { - console.log(); - throw Error('could not verify session'); - } - const { data: orgs, error: errOrg } = await supabase - .from('organization_members') - .select('*') - .eq('member_id', userId); - - if (errOrg || !orgs[0]) { - console.error('Failed to get org'); - throw error; - } - - return orgs[0].organization_id; -} diff --git a/src/data/user/organizations.ts b/src/data/user/organizations.ts index db6eb071..8179df2c 100644 --- a/src/data/user/organizations.ts +++ b/src/data/user/organizations.ts @@ -493,24 +493,6 @@ export const getDefaultOrganization = async () => { return data.default_organization; }; -export const getDefaultOrganizationId = async () => { - const supabaseClient = createSupabaseUserServerComponentClient(); - const { data, error } = await supabaseClient - .from('user_profiles') - .select('default_organization') - .single(); - - if (error) { - throw error; - } - - if (!data.default_organization) { - return null; - } - - return data.default_organization; -}; - export async function setDefaultOrganization( organizationId: string, ): Promise {