Skip to content
Merged
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
14 changes: 14 additions & 0 deletions apps/backend/src/routes/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
import { randomBytes } from 'crypto';
import { encrypt } from '../utils/encryption.js';

const GITHUB_AUTH_URL = 'https://github.com/login/oauth/authorize';
const GITHUB_TOKEN_URL = 'https://github.com/login/oauth/access_token';
Expand Down Expand Up @@ -119,6 +120,19 @@ export async function authRoutes(app: FastifyInstance) {
},
});

// Save the authentication token for 'user:email read:user' so we have a basic platform connection.
// Failure here is non-fatal — the user can still authenticate; the token can be reconnected later.
try {
const encryptedToken = encrypt(tokenData.access_token);
await app.prisma.oAuthToken.upsert({
where: { userId_platform: { userId: user.id, platform: 'github' } },
update: { accessToken: encryptedToken, scopes: 'read:user user:email' },
create: { userId: user.id, platform: 'github', accessToken: encryptedToken, scopes: 'read:user user:email' },
});
} catch (err) {
app.log.error({ err, userId: user.id }, 'Failed to persist GitHub OAuth token — authentication proceeds');
}

// Generate JWT
const token = app.jwt.sign(
{ id: user.id, username: user.username },
Expand Down