Skip to content

Commit

Permalink
Merge pull request #72 from diggerhq/fix/github-app-installation-call…
Browse files Browse the repository at this point in the history
…back-auth

Fix auth in github callback
  • Loading branch information
ZIJ authored Oct 31, 2024
2 parents 6c10b6a + 0abe98c commit b9f8f4b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 51 deletions.
41 changes: 8 additions & 33 deletions src/app/api/github-callback/route.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()}`,
{
Expand All @@ -49,33 +54,3 @@ export async function GET(request: NextRequest) {
return NextResponse.redirect(toSiteURL('/github_app/error'));
}
}

async function getOrganizationId(): Promise<string> {
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;
}
18 changes: 0 additions & 18 deletions src/data/user/organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SAPayload> {
Expand Down

0 comments on commit b9f8f4b

Please sign in to comment.