Skip to content

Commit e718f66

Browse files
committed
Changed google login method and attempted manual redirect after login button pressed
1 parent fc23b0a commit e718f66

File tree

12 files changed

+702
-20
lines changed

12 files changed

+702
-20
lines changed

backend/user-service/package-lock.json

Lines changed: 546 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/user-service/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@
2121
},
2222
"dependencies": {
2323
"@nestjs/common": "^10.0.0",
24+
"@nestjs/config": "^3.2.3",
2425
"@nestjs/core": "^10.0.0",
26+
"@nestjs/jwt": "^10.2.0",
27+
"@nestjs/passport": "^10.0.3",
2528
"@nestjs/platform-express": "^10.0.0",
29+
"google-auth-library": "^9.14.1",
30+
"passport": "^0.7.0",
31+
"passport-google-oauth2": "^0.2.0",
32+
"passport-google-oauth20": "^2.0.0",
33+
"passport-jwt": "^4.0.1",
34+
"passport-local": "^1.0.0",
2635
"reflect-metadata": "^0.2.0",
2736
"rxjs": "^7.8.1"
2837
},
@@ -33,6 +42,10 @@
3342
"@types/express": "^4.17.17",
3443
"@types/jest": "^29.5.2",
3544
"@types/node": "^20.3.1",
45+
"@types/passport-google-oauth2": "^0.1.10",
46+
"@types/passport-google-oauth20": "^2.0.16",
47+
"@types/passport-jwt": "^4.0.1",
48+
"@types/passport-local": "^1.0.38",
3649
"@types/supertest": "^6.0.0",
3750
"@typescript-eslint/eslint-plugin": "^8.0.0",
3851
"@typescript-eslint/parser": "^8.0.0",
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import { Controller, Get } from '@nestjs/common';
1+
import { Controller, Get, Post, Req, Res } from '@nestjs/common';
2+
import { Request, Response } from 'express';
3+
import { AuthService } from './auth.service';
24

35
@Controller('auth')
46
export class AuthController {
5-
@Get('google/login')
6-
googleLogin() {
7-
return {msg:"Login"}
7+
constructor(private authService: AuthService) {}
8+
9+
@Post('/google/callback')
10+
async handleRedirect(@Req() req: Request, @Res() res: Response) {
11+
console.log("HELLLO")
12+
const {code} = req.body
13+
const tokens = await this.authService.exchangeCodeForTokens(code)
14+
console.log(tokens)
15+
res.json(tokens);
816
}
917
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { Module } from '@nestjs/common';
22
import { AuthController } from './auth.controller';
3+
import { AuthService } from './auth.service';
4+
import { ConfigModule } from '@nestjs/config';
5+
// import { GoogleStrategy } from './utils/google.strategy';
36

47
@Module({
5-
controllers: [AuthController]
8+
imports: [ConfigModule],
9+
controllers: [AuthController],
10+
providers: [AuthService]
611
})
712
export class AuthModule {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { AuthService } from './auth.service';
3+
4+
describe('AuthService', () => {
5+
let service: AuthService;
6+
7+
beforeEach(async () => {
8+
const module: TestingModule = await Test.createTestingModule({
9+
providers: [AuthService],
10+
}).compile();
11+
12+
service = module.get<AuthService>(AuthService);
13+
});
14+
15+
it('should be defined', () => {
16+
expect(service).toBeDefined();
17+
});
18+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { OAuth2Client } from 'google-auth-library';
3+
4+
@Injectable()
5+
export class AuthService {
6+
private oAuth2Client: OAuth2Client;
7+
8+
constructor() {
9+
this.oAuth2Client = new OAuth2Client('350585476549-mild3b1ggbtd57t4sduk8qlc7d8suq66.apps.googleusercontent.com',
10+
'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', 'http://localhost:3000');
11+
}
12+
13+
async exchangeCodeForTokens(code: string) {
14+
try {
15+
const { tokens } = await this.oAuth2Client.getToken(code);
16+
17+
return tokens; // Contains access_token, refresh_token, and id_token
18+
} catch (error) {
19+
20+
}
21+
}
22+
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Injectable } from "@nestjs/common";
2+
import { AuthGuard } from "@nestjs/passport";
3+
4+
@Injectable()
5+
export class GoogleAuthGuard extends AuthGuard('google') {}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// import { Injectable } from "@nestjs/common";
2+
// import { PassportStrategy } from "@nestjs/passport";
3+
// import { Profile, Strategy } from "passport-google-oauth20";
4+
// import { ConfigService } from '@nestjs/config';
5+
6+
// @Injectable()
7+
// export class GoogleStrategy extends PassportStrategy(Strategy) {
8+
// constructor(private configService: ConfigService) {
9+
// super({
10+
// clientID: configService.get<String>('GOOGLE_CLIENT_ID'),
11+
// clientSecret: configService.get<String>('GOOGLE_CLIENT_SECRET'),
12+
// callbackURL: 'http://localhost:3001/auth/google/callback',
13+
// scope: ['profile', 'email']
14+
// });
15+
// }
16+
17+
// async validate(access_token: string, refresh_token: string, profile: Profile) {
18+
// console.log(access_token);
19+
// console.log(refresh_token);
20+
// console.log(profile);
21+
// }
22+
23+
// }

backend/user-service/src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { AppModule } from './app.module';
33

44
async function bootstrap() {
55
const app = await NestFactory.create(AppModule);
6-
app.setGlobalPrefix('api')
6+
app.enableCors({
7+
origin: 'http://localhost:3000'
8+
})
79
await app.listen(3001);
810
}
911
bootstrap();

frontend/package-lock.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)