work
This commit is contained in:
34
src/routes/api/github/install/callback/+server.ts
Normal file
34
src/routes/api/github/install/callback/+server.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
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);
|
||||
};
|
||||
Reference in New Issue
Block a user