Skip to content

Commit 779a65a

Browse files
committed
create long term access
1 parent e6f8a0f commit 779a65a

8 files changed

Lines changed: 21 additions & 11 deletions

File tree

backend/src/chat/chat.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import { UserService } from 'src/user/user.service';
1111
import { PubSub } from 'graphql-subscriptions';
1212
import { JwtCacheModule } from 'src/jwt-cache/jwt-cache.module';
1313
import { UploadModule } from 'src/upload/upload.module';
14+
import { GitHubModule } from 'src/github/github.module';
1415

1516
@Module({
1617
imports: [
1718
TypeOrmModule.forFeature([Chat, User]),
1819
AuthModule,
1920
JwtCacheModule,
2021
UploadModule,
22+
GitHubModule,
2123
],
2224
controllers: [ChatController],
2325
providers: [

backend/src/github/github.module.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module } from '@nestjs/common';
1+
import { forwardRef, Module } from '@nestjs/common';
22
import { TypeOrmModule } from '@nestjs/typeorm';
33
import { AuthModule } from '../auth/auth.module';
44
import { ProjectGuard } from '../guard/project.guard';
@@ -14,18 +14,19 @@ import { ProjectPackages } from 'src/project/project-packages.model';
1414
import { GitHuController } from './github.controller';
1515
import { ProjectService } from 'src/project/project.service';
1616
import { ConfigModule, ConfigService } from '@nestjs/config';
17-
import { UserService } from 'src/user/user.service';
17+
import { UserModule } from 'src/user/user.module';
1818

1919
@Module({
2020
imports: [
2121
TypeOrmModule.forFeature([Project, Chat, User, ProjectPackages]),
2222
AuthModule,
2323
AppConfigModule,
2424
UploadModule,
25-
ConfigModule
25+
ConfigModule,
26+
forwardRef(() => UserModule),
2627
],
2728
controllers: [GitHuController],
28-
providers: [ProjectService, ProjectGuard, GitHubAppService, GitHubService, ConfigService, ChatService, UserService],
29+
providers: [ProjectService, ProjectGuard, GitHubAppService, GitHubService, ConfigService, ChatService],
2930
exports: [GitHubService],
3031
})
3132
export class GitHubModule {}

backend/src/github/githubApp.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class GitHubAppService {
6767
await this.userRepo.update(
6868
{ githubInstallationId: installationId },
6969
{ githubInstallationId: null,
70-
githubCode: null
70+
githubAccessToken: null
7171
}
7272
);
7373

backend/src/project/project.service.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,7 @@ export class ProjectService {
808808
user.githubInstallationId,
809809
);
810810

811-
const githubCode = user.githubCode;
812-
const userOAuthToken = await this.gitHubService.exchangeOAuthCodeForToken(githubCode);
811+
const userOAuthToken = user.githubAccessToken;
813812

814813
// 4) Create the repo if the project doesn’t have it yet
815814
if (!project.githubRepoName || !project.githubOwner) {

backend/src/user/user.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class User extends SystemBaseModel {
8484

8585
@Field({ nullable: true })
8686
@Column({ nullable: true })
87-
githubCode?: string;
87+
githubAccessToken?: string;
8888

8989
/**
9090
* This field is maintained for API compatibility but is no longer actively used.

backend/src/user/user.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module } from '@nestjs/common';
1+
import { forwardRef, Module } from '@nestjs/common';
22
import { UserResolver } from './user.resolver';
33
import { UserService } from './user.service';
44
import { DateScalar } from 'src/common/scalar/date.scalar';
@@ -8,6 +8,7 @@ import { JwtModule } from '@nestjs/jwt';
88
import { AuthModule } from 'src/auth/auth.module';
99
import { MailModule } from 'src/mail/mail.module';
1010
import { UploadModule } from 'src/upload/upload.module';
11+
import { GitHubModule } from 'src/github/github.module';
1112

1213
@Module({
1314
imports: [
@@ -16,6 +17,7 @@ import { UploadModule } from 'src/upload/upload.module';
1617
AuthModule,
1718
MailModule,
1819
UploadModule,
20+
forwardRef(() => GitHubModule),
1921
],
2022
providers: [UserResolver, UserService, DateScalar],
2123
exports: [UserService],

backend/src/user/user.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import { InjectRepository } from '@nestjs/typeorm';
55
import { FileUpload } from 'graphql-upload-minimal';
66
import { UploadService } from '../upload/upload.service';
77
import { validateAndBufferFile } from 'src/common/security/file_check';
8+
import { GitHubService } from 'src/github/github.service';
89

910
@Injectable()
1011
export class UserService {
1112
constructor(
1213
@InjectRepository(User)
1314
private userRepository: Repository<User>,
1415
private readonly uploadService: UploadService,
16+
private readonly gitHubService: GitHubService,
1517
) {}
1618

1719
// Method to get all chats of a user
@@ -72,8 +74,12 @@ export class UserService {
7274
}
7375

7476
console.log(`Binding GitHub installation ID ${installationId} to user ${githubCode}`);
77+
78+
//First request to GitHub to exchange the code for an access token (Wont expire)
79+
const accessToken = await this.gitHubService.exchangeOAuthCodeForToken(githubCode);
80+
7581
user.githubInstallationId = installationId;
76-
user.githubCode = githubCode;
82+
user.githubAccessToken = accessToken;
7783

7884
await this.userRepository.save(user);
7985

frontend/src/graphql/schema.gql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ type User {
246246
chats: [Chat!]!
247247
createdAt: Date!
248248
email: String!
249-
githubCode: String
249+
githubAccessToken: String
250250
githubInstallationId: String
251251
id: ID!
252252
isActive: Boolean!

0 commit comments

Comments
 (0)