Skip to content

Commit

Permalink
Get org id from cookies; remove cookie hack
Browse files Browse the repository at this point in the history
  • Loading branch information
ZIJ committed Aug 20, 2024
1 parent c06b572 commit b2c689c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Link from "next/link";
export default function GithubAppError() {
return (
<>
<h1>Something went wrong</h1>
<p>GitHub App installation failed. You can now close this tab or <Link href="/">go to dashboard</Link></p>
<h1>Success</h1>
<p>GitHub App installed successfully. You can now close this tab or <Link href="/">go to dashboard</Link></p>
</>
)
}
22 changes: 0 additions & 22 deletions src/app/(dynamic-pages)/(login-pages)/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,6 @@ export async function GET(request: Request) {
}
}

// HACK_ALERT!!!
// cookie is probably set on 'next.digger.dev' we have to change it to `.digger.dev`
const cookieKey = `sb-${process.env.SUPABASE_PROJECT_REF}-auth-token`;
const cookieStore = cookies();
const currentCookieValue = cookieStore.get(cookieKey)?.value;
// get domain of current reques
const domain = new URL(request.url).hostname;
if (
domain.includes('next.digger.dev') &&
currentCookieValue &&
!isDevelopment
) {
// set cookie to .digger.dev
cookieStore.set(cookieKey, currentCookieValue, {
domain: '.digger.dev',
secure: true,
path: '/',
sameSite: 'lax',
httpOnly: true,
});
}

let redirectTo = new URL('/dashboard', requestUrl.origin);

if (next) {
Expand Down
52 changes: 25 additions & 27 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 { toSiteURL } from '@/utils/helpers';
import retry from 'async-retry';
import { NextRequest, NextResponse } from 'next/server';

// Use the environment variable for the callback URL
Expand All @@ -27,7 +26,7 @@ export async function GET(request: NextRequest) {
'Trying to get org id for the following installation ID:',
installationId,
);
const organizationId = await getOrganizationId(installationId);
const organizationId = await getOrganizationId();
const response = await fetch(
`${AUTH_SERVICE_URL}?${searchParams.toString()}`,
{
Expand All @@ -50,32 +49,31 @@ export async function GET(request: NextRequest) {
}
}

async function getOrganizationId(installationId: string): Promise<string> {
async function getOrganizationId(): Promise<string> {
const supabase = createSupabaseUserRouteHandlerClient();
return retry(
async (bail) => {
const { data, error } = await supabase
.from('github_app_installation_links')
.select('organization_id')
.eq('github_installation_id', installationId)
.single();

if (error) {
if (error.code === '404') bail(new Error('Organization not found'));
throw error;
}
const {
data: { user },
error,
} = await supabase.auth.getUser();
if (error || !user?.id) {
console.error('Failed to get current user', error);
throw error;
}
const { data: orgs, error: errOrg } = await supabase
.from('organization_members')
.select('*')
.eq('member_id', user.id);

if (!data?.organization_id) {
throw new Error('Organization ID not found');
}
if (errOrg || !orgs[0]) {
console.error('Failed to get org');
throw error;
}

return data.organization_id;
},
{
retries: 2,
onRetry: (error, attempt) => {
console.log(`Attempt ${attempt} failed. Retrying...`);
},
},
);
return orgs[0].organization_id;
}

/*
1. click "install and authorize" in github
2.
*/

0 comments on commit b2c689c

Please sign in to comment.