35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import { redirect, error } from '@sveltejs/kit';
|
|
import { db } from '$lib/server/db';
|
|
import { githubInstallation } from '$lib/server/db/schema';
|
|
import { decodeInstallState, getGitHubInstallation } from '$lib/server/github-app';
|
|
|
|
export const GET = async ({ url, locals }) => {
|
|
const installationId = url.searchParams.get('installation_id');
|
|
const state = decodeInstallState(url.searchParams.get('state') ?? undefined);
|
|
|
|
if (!installationId) throw error(400, 'Missing installation_id');
|
|
if (!state.userId) throw error(400, 'Missing install state');
|
|
if (!locals.user || locals.user.id !== state.userId) throw redirect(302, '/auth/login');
|
|
|
|
const installation = await getGitHubInstallation(installationId);
|
|
const accountLogin = installation.account?.login ?? 'github';
|
|
|
|
await db
|
|
.insert(githubInstallation)
|
|
.values({
|
|
userId: locals.user.id,
|
|
installationId,
|
|
accountLogin
|
|
})
|
|
.onConflictDoUpdate({
|
|
target: githubInstallation.installationId,
|
|
set: {
|
|
userId: locals.user.id,
|
|
accountLogin,
|
|
updatedAt: new Date()
|
|
}
|
|
});
|
|
|
|
throw redirect(303, state.redirect);
|
|
};
|