Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix auth in github callback #72

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading