Skip to content
Open
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
51 changes: 18 additions & 33 deletions apps/backend/src/routes/connect.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { randomBytes } from 'node:crypto';

import { decrypt, encrypt } from '../utils/encryption.js';
import { getErrorMessage } from '../utils/error.util.js';
import { getErrorMessage, isGitHubTokenError } from '../utils/error.util.js';

import type { GitHubTokenErrorResponse, GitHubTokenResponse } from '../utils/error.util.js';
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';

const GITHUB_AUTH_URL = 'https://github.com/login/oauth/authorize';
Expand Down Expand Up @@ -30,14 +31,10 @@ export async function connectRoutes(app: FastifyInstance): Promise<void> {
// ─── Status ───

app.get('/status', {
preHandler: [async (request, reply) => {
const server = request.server as any;
if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return }
if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return }
try { await request.jwtVerify() } catch { reply.status(401).send({ error: 'Unauthorized' }) }
}],
// eslint-disable-next-line @typescript-eslint/unbound-method
preHandler: [app.authenticate],
}, async (request: FastifyRequest, _reply: FastifyReply) => {
const userId = (request.user as any).id;
const userId = request.user.id;

const tokens = await app.prisma.oAuthToken.findMany({
where: { userId },
Expand All @@ -50,14 +47,10 @@ export async function connectRoutes(app: FastifyInstance): Promise<void> {
// ─── GitHub Connect ───

app.get('/github', {
preHandler: [async (request, reply) => {
const server = request.server as any;
if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return }
if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return }
try { await request.jwtVerify() } catch { reply.status(401).send({ error: 'Unauthorized' }) }
}],
// eslint-disable-next-line @typescript-eslint/unbound-method
preHandler: [app.authenticate],
}, async (request: FastifyRequest, reply: FastifyReply) => {
const userId = (request.user as any).id;
const userId = request.user.id;
const nonce = generateState();

// Store nonce in Redis with 10-minute TTL.
Expand Down Expand Up @@ -127,10 +120,10 @@ export async function connectRoutes(app: FastifyInstance): Promise<void> {
}),
});

const tokenData = (await tokenRes.json()) as any;
const tokenData = (await tokenRes.json()) as GitHubTokenResponse | GitHubTokenErrorResponse;

if (tokenData.error) {
app.log.error('GitHub connect token error:', tokenData);
if (isGitHubTokenError(tokenData)) {
app.log.error(tokenData, 'GitHub connect token error:');
return reply.redirect(`${process.env.PUBLIC_APP_URL}/settings?error=connect_failed`);
}

Expand Down Expand Up @@ -174,14 +167,10 @@ export async function connectRoutes(app: FastifyInstance): Promise<void> {
});

app.get('/github/autodiscover', {
preHandler: [async (request, reply) => {
const server = request.server as any;
if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return }
if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return }
try { await request.jwtVerify() } catch { reply.status(401).send({ error: 'Unauthorized' }) }
}],
// eslint-disable-next-line @typescript-eslint/unbound-method
preHandler: [app.authenticate],
}, async (request: FastifyRequest, reply: FastifyReply) => {
const userId = (request.user as any).id;
const userId = request.user.id;
const cacheKey = `github:autodiscover:${userId}`;

if (app.redis) {
Expand Down Expand Up @@ -262,15 +251,11 @@ export async function connectRoutes(app: FastifyInstance): Promise<void> {

// ─── Disconnect ───

app.delete('/:platform', {
preHandler: [async (request, reply) => {
const server = request.server as any;
if (typeof server?.authenticate === 'function') { await server.authenticate(request, reply); return }
if (typeof (app as any).authenticate === 'function') { await (app as any).authenticate(request, reply); return }
try { await request.jwtVerify() } catch { reply.status(401).send({ error: 'Unauthorized' }) }
}],
app.delete<{ Params: { platform: string } }>('/:platform', {
// eslint-disable-next-line @typescript-eslint/unbound-method
preHandler: [app.authenticate],
}, async (request: FastifyRequest<{ Params: { platform: string } }>, reply: FastifyReply) => {
const userId = (request.user as any).id;
const userId = request.user.id;
const { platform } = request.params;

const SUPPORTED_PLATFORMS = ['github', 'google', 'twitter', 'linkedin'];
Expand Down