Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@langchain/core": "^0.3.51",
"@langchain/openai": "^0.5.10",
"@nestjs/axios": "^4.0.0",
"@nestjs/common": "^11.0.1",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.0.1",
Expand Down
2 changes: 2 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
Expand All @@ -11,6 +12,7 @@ import { JwtStrategy } from './strategy/jwt.strategy';

@Module({
imports: [
HttpModule.register({}),
PassportModule,
UserModule,
JwtModule.register({
Expand Down
103 changes: 50 additions & 53 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import axios, { AxiosError } from 'axios';
import { catchError, firstValueFrom } from 'rxjs';
import { KakaoUserResponse, UserPayload } from '@gglk/auth/auth.interface';
import { UserService } from '@gglk/user/user.service';
import { TOKEN_TYPE } from './auth.constant';
Expand All @@ -13,69 +14,65 @@ export class AuthService {
private readonly jwtService: JwtService,
private readonly userService: UserService,
private readonly configService: ConfigService,
private readonly httpService: HttpService,
) {}

async getKakaoUserAccessToken(code: string, redirectUri: string) {
try {
const data = new URLSearchParams();
data.append('grant_type', 'authorization_code');
data.append(
'client_id',
this.configService.get<string>('KAKAO_CLIENT_ID')!,
);
data.append('code', code);
data.append('redirect_uri', redirectUri);
private get KAKAO_GRANT_TYPE() {
return 'authorization_code';
}

const response = await axios.post(
'https://kauth.kakao.com/oauth/token',
data,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
async getKakaoUserAccessToken(code: string, redirect_uri: string) {
const data = new URLSearchParams({
grant_type: this.KAKAO_GRANT_TYPE,
client_id: this.configService.get<string>('KAKAO_CLIENT_ID')!,
code,
redirect_uri,
});

const response = await firstValueFrom(
this.httpService
.post<{ access_token: string; [k: string]: unknown }>(
'https://kauth.kakao.com/oauth/token',
data,
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
},
},
},
);
const { access_token }: { access_token: string; [k: string]: unknown } =
response.data;
return access_token;
} catch (e) {
if (e instanceof AxiosError) {
console.error(
'Error while making authentication to Kakao - Get bearer token',
);
throw new KakaoOauthException();
} else {
throw e;
}
}
)
.pipe(
catchError((error) => {
console.error(error);
throw new KakaoOauthException();
}),
),
);

return response.data.access_token;
}

async getKakaoUserByAccessToken(accessToken: string) {
try {
const userRes = await axios.get<KakaoUserResponse>(
'https://kapi.kakao.com/v2/user/me',
{
const userRes = await firstValueFrom(
this.httpService
.get<KakaoUserResponse>('https://kapi.kakao.com/v2/user/me', {
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
})
.pipe(
catchError((error) => {
console.error(error);
throw new KakaoOauthException();
}),
),
);

const kakaoUser = userRes.data;
return {
id: kakaoUser.id.toString(),
name: kakaoUser.properties?.nickname ?? '',
};
} catch (e) {
if (e instanceof AxiosError) {
console.error(
'Error while making authentication to Kakao - Get user information',
);
throw new KakaoOauthException();
} else {
throw e;
}
}
const { data: kakaoUser } = userRes;

return {
id: kakaoUser.id.toString(),
name: kakaoUser.properties?.nickname ?? '',
};
}

async generateGuestToken() {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,11 @@
"@napi-rs/nice-win32-ia32-msvc" "1.0.1"
"@napi-rs/nice-win32-x64-msvc" "1.0.1"

"@nestjs/axios@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@nestjs/axios/-/axios-4.0.0.tgz#520ff7c6238e635658fe334ee62db9d1b9c5546f"
integrity sha512-1cB+Jyltu/uUPNQrpUimRHEQHrnQrpLzVj6dU3dgn6iDDDdahr10TgHFGTmw5VuJ9GzKZsCLDL78VSwJAs/9JQ==

"@nestjs/cli@^11.0.0":
version "11.0.7"
resolved "https://registry.yarnpkg.com/@nestjs/cli/-/cli-11.0.7.tgz#204a1969c2609d7cf5e98a4d65819bd8585bead7"
Expand Down
Loading