Skip to content

Commit b22be02

Browse files
committed
feat(oath): add get oauth token from code
1 parent 16b3d63 commit b22be02

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@nestjs/swagger": "^7.3.1",
3939
"@nestjs/typeorm": "^10.0.2",
4040
"aws-sdk": "^2.1603.0",
41+
"axios": "^1.7.2",
4142
"bcryptjs": "^2.4.3",
4243
"class-transformer": "^0.5.1",
4344
"class-validator": "^0.14.1",

src/core/config/environment/environments.ts

+4
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ export class EnvironmentConfigService {
5252
getMeetingGrpcUrl(): string {
5353
return this.configService.get<string>('MEETING_GRPC_URL');
5454
}
55+
56+
getOauthSecret(): string {
57+
return this.configService.get<string>('CLIENT_SECRET');
58+
}
5559
}

src/core/dtos/auth/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { LoginSocialDto } from './login.dto';
2+
export { OauthDto } from './oauth.dto'

src/core/dtos/auth/oauth.dto.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { IsString, IsNotEmpty, IsOptional } from 'class-validator';
3+
4+
export class OauthDto {
5+
@ApiProperty({ example: 'code' })
6+
@IsString()
7+
@IsNotEmpty()
8+
code: string;
9+
10+
@ApiProperty({ example: 'client-id' })
11+
@IsString()
12+
@IsNotEmpty()
13+
clientId: string;
14+
15+
@ApiProperty({ example: 'redirect_uri' })
16+
@IsString()
17+
@IsNotEmpty()
18+
redirectUri: string;
19+
}

src/features/auth/auth.controller.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99
Request,
1010
SerializeOptions,
1111
Delete,
12+
InternalServerErrorException,
13+
NotFoundException,
1214
} from '@nestjs/common';
13-
import { LoginSocialDto } from 'src/core/dtos/auth';
15+
import { LoginSocialDto, OauthDto } from 'src/core/dtos/auth';
1416
import { UserFactoryService } from '../user/user-factory.service';
1517
import { AuthUseCases } from 'src/features/auth/auth.usecase';
1618
import { AuthGuard } from '@nestjs/passport';
@@ -23,6 +25,8 @@ import {
2325
import { LoginResponseType } from 'src/features/auth/types/login-response.type';
2426
import { AwsS3Service } from '../image/aws-s3/aws-s3.service';
2527
import { ApiKeyGuard } from 'src/utils/strategies/api-key.strategy';
28+
import axios from 'axios';
29+
import { EnvironmentConfigService } from 'src/core/config/environment/environments';
2630

2731
@ApiTags('auth')
2832
@ApiSecurity('api_key', ['api_key'])
@@ -33,11 +37,36 @@ import { ApiKeyGuard } from 'src/utils/strategies/api-key.strategy';
3337
})
3438
export class AuthController {
3539
constructor(
40+
private readonly environment: EnvironmentConfigService,
3641
private authUseCases: AuthUseCases,
3742
private userFactoryService: UserFactoryService,
3843
private awsS3Service: AwsS3Service,
3944
) {}
4045

46+
@ApiOperation({ summary: 'Get Oauth Token', description: 'Get Oauth token from code' })
47+
@Post('token')
48+
async getOauthToken(@Body() oauth: OauthDto) {
49+
try {
50+
const res = await axios.post("https://oauth2.googleapis.com/token", null, {
51+
params: {
52+
client_id: oauth.clientId,
53+
client_secret: this.environment.getOauthSecret(),
54+
code: oauth.code,
55+
grant_type: 'authorization_code',
56+
redirect_uri: oauth.redirectUri,
57+
}
58+
});
59+
60+
if (res.status == 200) {
61+
return res.data;
62+
}
63+
64+
throw new NotFoundException()
65+
} catch (error) {
66+
throw new InternalServerErrorException();
67+
}
68+
}
69+
4170
@ApiOperation({ summary: 'Login', description: 'Login with Social Media' })
4271
@Post()
4372
loginWithSocial(@Body() loginWithSocial: LoginSocialDto) {

yarn.lock

+9
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,15 @@ axios@^1.6.0:
19331933
form-data "^4.0.0"
19341934
proxy-from-env "^1.1.0"
19351935

1936+
axios@^1.7.2:
1937+
version "1.7.2"
1938+
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.2.tgz#b625db8a7051fbea61c35a3cbb3a1daa7b9c7621"
1939+
integrity sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==
1940+
dependencies:
1941+
follow-redirects "^1.15.6"
1942+
form-data "^4.0.0"
1943+
proxy-from-env "^1.1.0"
1944+
19361945
babel-jest@^29.7.0:
19371946
version "29.7.0"
19381947
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.7.0.tgz#f4369919225b684c56085998ac63dbd05be020d5"

0 commit comments

Comments
 (0)