Skip to content

Commit 8ff567a

Browse files
authored
Merge pull request #390 from Quickchive/feat/get-user-by-provider
Feat/get user by provider
2 parents cfde73f + f96ded7 commit 8ff567a

File tree

6 files changed

+36
-54
lines changed

6 files changed

+36
-54
lines changed

src/auth/oauth.controller.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,4 @@ export class OAuthController {
130130
async appleLogin(@Query('code') code: string): Promise<LoginOutput> {
131131
return this.oauthService.appleLogin(code);
132132
}
133-
134-
@ApiOperation({
135-
summary: '카카오 로그인 요청',
136-
description: 'accessToken을 받아 카카오 로그인을 요청합니다.',
137-
})
138-
@Post('kakao')
139-
async createOneWithKakao(@Body() kakaoLoginRequest: KakaoLoginRequest) {
140-
return this.oauthService.createOneWithKakao(kakaoLoginRequest);
141-
}
142133
}

src/auth/oauth.service.ts

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ export class OAuthService {
3434
) {}
3535

3636
// OAuth Login
37-
async oauthLogin(email: string): Promise<LoginOutput> {
37+
async oauthLogin(email: string, provider: PROVIDER): Promise<LoginOutput> {
3838
try {
39-
const user: User = await this.userRepository.findOneByOrFail({ email });
39+
const user: User = await this.userRepository.findOneByOrFail({
40+
email,
41+
provider,
42+
});
4043
if (user) {
4144
const payload: Payload = this.jwtService.createPayload(
4245
user.email,
@@ -98,9 +101,12 @@ export class OAuthService {
98101
throw new BadRequestException('Please Agree to share your email');
99102
}
100103

101-
const user = await this.userRepository.findOneByEmail(email);
104+
const user = await this.userRepository.findOneByEmailAndProvider(
105+
email,
106+
PROVIDER.KAKAO,
107+
);
102108
if (user) {
103-
return this.oauthLogin(user.email);
109+
return this.oauthLogin(user.email, PROVIDER.KAKAO);
104110
}
105111

106112
// 회원가입인 경우 기본 카테고리 생성 작업 진행
@@ -115,51 +121,25 @@ export class OAuthService {
115121
await this.userRepository.createOne(newUser);
116122
await this.categoryRepository.createDefaultCategories(newUser);
117123

118-
return this.oauthLogin(newUser.email);
124+
return this.oauthLogin(newUser.email, PROVIDER.KAKAO);
119125
} catch (e) {
120126
throw e;
121127
}
122128
}
123129

124-
async createOneWithKakao({ authorizationToken }: KakaoLoginDto) {
125-
const { userInfo } =
126-
await this.oauthUtil.getKakaoUserInfo(authorizationToken);
127-
128-
const email = userInfo.kakao_account.email;
129-
if (!email) {
130-
throw new BadRequestException('Please Agree to share your email');
131-
}
132-
133-
const user = await this.userRepository.findOneByEmail(email);
134-
135-
if (user) {
136-
return this.oauthLogin(user.email);
137-
}
138-
139-
// 회원가입인 경우 기본 카테고리 생성 작업 진행
140-
const newUser = User.of({
141-
email,
142-
name: userInfo.kakao_account.profile.nickname,
143-
profileImage: userInfo.kakao_account.profile?.profile_image_url,
144-
password: this.encodePasswordFromEmail(email, process.env.KAKAO_JS_KEY),
145-
provider: PROVIDER.KAKAO,
146-
});
147-
148-
await this.userRepository.createOne(newUser);
149-
await this.categoryRepository.createDefaultCategories(newUser);
150-
return this.oauthLogin(newUser.email);
151-
}
152-
153130
// Login with Google account info
154131
async googleOauth({
155132
email,
156133
name,
157134
picture,
158135
}: googleUserInfo): Promise<LoginOutput> {
159-
const user = await this.userRepository.findOneByEmail(email);
136+
const user = await this.userRepository.findOneByEmailAndProvider(
137+
email,
138+
PROVIDER.GOOGLE,
139+
);
160140

161141
if (user) {
162-
return this.oauthLogin(user.email);
142+
return this.oauthLogin(user.email, PROVIDER.GOOGLE);
163143
}
164144

165145
// 회원가입인 경우 기본 카테고리 생성 작업 진행
@@ -177,7 +157,7 @@ export class OAuthService {
177157
await this.userRepository.createOne(newUser);
178158
await this.categoryRepository.createDefaultCategories(newUser);
179159

180-
return this.oauthLogin(newUser.email);
160+
return this.oauthLogin(newUser.email, PROVIDER.GOOGLE);
181161
}
182162

183163
private encodePasswordFromEmail(email: string, key?: string): string {
@@ -225,10 +205,13 @@ export class OAuthService {
225205

226206
const { sub: id, email } = this.jwtService.decode(data.id_token);
227207

228-
const user = await this.userRepository.findOneByEmail(email);
208+
const user = await this.userRepository.findOneByEmailAndProvider(
209+
email,
210+
PROVIDER.APPLE,
211+
);
229212

230213
if (user) {
231-
return this.oauthLogin(user.email);
214+
return this.oauthLogin(user.email, PROVIDER.APPLE);
232215
}
233216

234217
const newUser = User.of({
@@ -244,6 +227,6 @@ export class OAuthService {
244227
await this.userRepository.createOne(newUser);
245228
await this.categoryRepository.createDefaultCategories(newUser);
246229

247-
return this.oauthLogin(newUser.email);
230+
return this.oauthLogin(newUser.email, PROVIDER.APPLE);
248231
}
249232
}

src/categories/category.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,10 @@ export class CategoryService {
432432
});
433433
});
434434

435-
const { title, siteName, description } = await getLinkInfo(encodeURI(link));
436-
437-
const content = await getLinkContent(link);
435+
const [{ title, siteName, description }, content] = await Promise.all([
436+
getLinkInfo(encodeURI(link)),
437+
getLinkContent(link),
438+
]);
438439

439440
const question = `You are a machine tasked with auto-categorizing articles based on information obtained through web scraping.
440441

src/contents/util/content.util.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class OGCrawler {
2626
this.userAgent =
2727
options.userAgent ||
2828
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36';
29-
this.maxRedirects = options.maxRedirects || 5;
3029
this.cookies =
3130
options.cookies || 'CONSENT=YES+cb; Path=/; Domain=.youtube.com';
3231
this.proxy = options.proxy;

src/users/entities/user.entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class User extends CoreEntity {
3030
name: string;
3131

3232
@ApiProperty({ example: 'ex@g.com', description: 'User Email' })
33-
@Column({ unique: true })
33+
@Column({ type: 'varchar' })
3434
@IsEmail()
3535
email: string;
3636

src/users/repository/user.repository.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DataSource, Repository } from 'typeorm';
22
import { User } from '../entities/user.entity';
33
import { Injectable } from '@nestjs/common';
44
import { GetOrCreateAccountBodyDto } from '../dtos/get-or-create-account.dto';
5+
import { PROVIDER } from '../constant/provider.constant';
56

67
@Injectable()
78
export class UserRepository extends Repository<User> {
@@ -44,6 +45,13 @@ export class UserRepository extends Repository<User> {
4445
return this.findOne({ where: { email } });
4546
}
4647

48+
async findOneByEmailAndProvider(
49+
email: string,
50+
provider: PROVIDER,
51+
): Promise<User | null> {
52+
return this.findOne({ where: { email, provider } });
53+
}
54+
4755
async createOne(user: Partial<User>): Promise<User> {
4856
return this.save(user);
4957
}

0 commit comments

Comments
 (0)