-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from diggerhq/feat/gh-app-callback
Add github callback route to proxy backend
- Loading branch information
Showing
6 changed files
with
111 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
src/app/(dynamic-pages)/(authenticated-pages)/github_app/error/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Link from "next/link"; | ||
|
||
export default function GithubAppError() { | ||
const appSlug = process.env.NEXT_PUBLIC_GITHUB_APP_SLUG; | ||
return ( | ||
<> | ||
<h1>Something went wrong</h1> | ||
<p>GitHub App installation failed. Maybe <Link href={`https://github.com/apps/${appSlug}/installations/new/`}>try re-install it?</Link></p> | ||
</> | ||
) | ||
} |
10 changes: 10 additions & 0 deletions
10
src/app/(dynamic-pages)/(authenticated-pages)/github_app/success/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Link from "next/link"; | ||
|
||
export default function GithubAppError() { | ||
return ( | ||
<> | ||
<h1>Success</h1> | ||
<p>GitHub App installed successfully. You can now close this tab or <Link href="/">go to dashboard</Link></p> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { createSupabaseUserRouteHandlerClient } from '@/supabase-clients/user/createSupabaseUserRouteHandlerClient'; | ||
import { toSiteURL } from '@/utils/helpers'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
// Use the environment variable for the callback URL | ||
const AUTH_SERVICE_URL = process.env.GITHUB_CALLBACK_URL; | ||
const DIGGER_WEBHOOK_SECRET = process.env.DIGGER_WEBHOOK_SECRET; | ||
|
||
if (!AUTH_SERVICE_URL) { | ||
throw new Error('GITHUB_CALLBACK_URL environment variable is not set'); | ||
} | ||
|
||
export async function GET(request: NextRequest) { | ||
const searchParams = request.nextUrl.searchParams; | ||
const installationId = searchParams.get('installation_id'); | ||
|
||
if (!installationId) { | ||
return NextResponse.json( | ||
{ error: 'Missing installation_id' }, | ||
{ status: 400 }, | ||
); | ||
} | ||
|
||
try { | ||
console.log( | ||
'Trying to get org id for the following installation ID:', | ||
installationId, | ||
); | ||
const organizationId = await getOrganizationId(); | ||
const response = await fetch( | ||
`${AUTH_SERVICE_URL}?${searchParams.toString()}`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-Digger-Org-ID': organizationId, | ||
Authorization: `Bearer ${DIGGER_WEBHOOK_SECRET}`, | ||
}, | ||
}, | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Auth service responded with status: ${response.status}`); | ||
} | ||
return NextResponse.redirect(toSiteURL('/github_app/success')); | ||
} catch (error) { | ||
console.error('Error handling GitHub App installation callback:', error); | ||
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 { data: orgs, error: errOrg } = await supabase | ||
.from('organization_members') | ||
.select('*') | ||
.eq('member_id', user.id); | ||
|
||
if (errOrg || !orgs[0]) { | ||
console.error('Failed to get org'); | ||
throw error; | ||
} | ||
|
||
return orgs[0].organization_id; | ||
} |