Skip to content

Commit 8e8ddee

Browse files
committed
fix bug
1 parent 5230d4c commit 8e8ddee

6 files changed

Lines changed: 32 additions & 12 deletions

File tree

backend/src/github/github.service.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/github/github.service.ts
22

3-
import { Injectable, Logger } from '@nestjs/common';
3+
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
44
import * as jwt from 'jsonwebtoken';
55
import axios from 'axios';
66
import * as fs from 'fs';
@@ -96,6 +96,11 @@ export class GitHubService {
9696
},
9797
},
9898
);
99+
100+
if (response.data.error) {
101+
console.error('GitHub OAuth error:', response.data);
102+
throw new BadRequestException(`GitHub OAuth error: ${response.data.error_description}`);
103+
}
99104

100105
const accessToken = response.data.access_token;
101106
if (!accessToken) {

backend/src/github/githubApp.service.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ export class GitHubAppService {
8383
}
8484
});
8585

86-
// only for webhooks debugging
87-
this.app.webhooks.onAny(async (event) => {
88-
this.logger.log(`onAny: Received event='${event.name}' action='${event.payload}'`);
89-
});
86+
// // only for webhooks debugging
87+
// this.app.webhooks.onAny(async (event) => {
88+
// this.logger.log(`onAny: Received event='${event.name}' action='${event.payload}'`);
89+
// });
9090
}
9191

9292
/**

backend/src/project/project.resolver.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export class ProjectsResolver {
167167

168168
@Mutation(() => Project)
169169
async syncProjectToGitHub(@Args('projectId') projectId: string, @GetUserIdFromToken() userId: string,) {
170+
// TODO: MAKE PUBLIC DYNAMIC
170171
return this.projectService.syncProjectToGitHub(userId, projectId, true /* isPublic? */);
171172
}
172173
}

backend/src/project/project.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,10 @@ export class ProjectService {
803803
throw new Error('GitHub App not installed for this user');
804804
}
805805

806-
// 3) Get the installation token
806+
// 3) Get the installation and OAUTH token
807807
const installationToken = await this.gitHubService.getInstallationToken(
808808
user.githubInstallationId,
809809
);
810-
811810
const userOAuthToken = user.githubAccessToken;
812811

813812
// 4) Create the repo if the project doesn’t have it yet
@@ -816,7 +815,7 @@ export class ProjectService {
816815
const repoName = project.projectName
817816
.replace(/\s+/g, '-')
818817
.toLowerCase() // e.g. "my-project"
819-
+ '-' + project.githubOwner; // to make it unique if needed
818+
+ '-' + "ChangeME"; // to make it unique if needed
820819

821820
const { owner, repo, htmlUrl } = await this.gitHubService.createUserRepo(
822821
repoName,

backend/src/user/user.service.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,24 @@ export class UserService {
7373
throw new BadRequestException('User already linked to a GitHub installation.');
7474
}
7575

76-
console.log(`Binding GitHub installation ID ${installationId} to user ${githubCode}`);
76+
if (!githubCode) {
77+
throw new BadRequestException('Missing GitHub OAuth code');
78+
}
79+
80+
console.log(`Binding GitHub installation ID ${installationId} to user code ${githubCode}`);
7781

7882
//First request to GitHub to exchange the code for an access token (Wont expire)
7983
const accessToken = await this.gitHubService.exchangeOAuthCodeForToken(githubCode);
8084

8185
user.githubInstallationId = installationId;
8286
user.githubAccessToken = accessToken;
8387

84-
await this.userRepository.save(user);
88+
try {
89+
await this.userRepository.save(user);
90+
} catch (error) {
91+
console.error('Error saving user:', error);
92+
throw new Error('Failed to save user with installation ID');
93+
}
8594

8695
return true;
8796
}

frontend/src/components/github-callback.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use client';
2-
import { useEffect, useState } from 'react';
2+
import { useEffect, useRef, useState } from 'react';
33
import { useRouter, useSearchParams } from 'next/navigation';
44
import { useAuthContext } from '@/providers/AuthProvider';
55
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card';
@@ -14,8 +14,12 @@ export default function GitHubCallback() {
1414

1515
const [status, setStatus] = useState<'loading' | 'success' | 'error'>('loading');
1616
const [errorMessage, setErrorMessage] = useState<string | null>(null);
17+
18+
const hasCalledBackend = useRef(false); // Add guard flag here
1719

1820
useEffect(() => {
21+
if (hasCalledBackend.current) return; // Prevent multiple calls
22+
1923
// Extract installation ID from search params
2024
const githubCode = searchParams.get('code');
2125
const installationId = searchParams.get('installation_id');
@@ -43,6 +47,8 @@ export default function GitHubCallback() {
4347
setErrorMessage('No installation ID was provided.');
4448
return;
4549
}
50+
51+
hasCalledBackend.current = true;
4652

4753
// Call the backend directly to store the installation ID
4854
const storeInstallation = async () => {
@@ -82,7 +88,7 @@ export default function GitHubCallback() {
8288

8389
// Function to handle redirect back to projects
8490
const handleContinue = () => {
85-
router.push('/projects'); // Change this to your desired redirect path
91+
router.push('/'); // Change this to your desired redirect path
8692
};
8793

8894
return (

0 commit comments

Comments
 (0)