Skip to content
Merged
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
9 changes: 0 additions & 9 deletions src/auth/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,4 @@ export class OAuthController {
async appleLogin(@Query('code') code: string): Promise<LoginOutput> {
return this.oauthService.appleLogin(code);
}

@ApiOperation({
summary: '카카오 로그인 요청',
description: 'accessToken을 받아 카카오 로그인을 요청합니다.',
})
@Post('kakao')
async createOneWithKakao(@Body() kakaoLoginRequest: KakaoLoginRequest) {
return this.oauthService.createOneWithKakao(kakaoLoginRequest);
}
}
63 changes: 23 additions & 40 deletions src/auth/oauth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ export class OAuthService {
) {}

// OAuth Login
async oauthLogin(email: string): Promise<LoginOutput> {
async oauthLogin(email: string, provider: PROVIDER): Promise<LoginOutput> {
try {
const user: User = await this.userRepository.findOneByOrFail({ email });
const user: User = await this.userRepository.findOneByOrFail({
email,
provider,
});
if (user) {
const payload: Payload = this.jwtService.createPayload(
user.email,
Expand Down Expand Up @@ -98,9 +101,12 @@ export class OAuthService {
throw new BadRequestException('Please Agree to share your email');
}

const user = await this.userRepository.findOneByEmail(email);
const user = await this.userRepository.findOneByEmailAndProvider(
email,
PROVIDER.KAKAO,
);
if (user) {
return this.oauthLogin(user.email);
return this.oauthLogin(user.email, PROVIDER.KAKAO);
}

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

return this.oauthLogin(newUser.email);
return this.oauthLogin(newUser.email, PROVIDER.KAKAO);
} catch (e) {
throw e;
}
}

async createOneWithKakao({ authorizationToken }: KakaoLoginDto) {
const { userInfo } =
await this.oauthUtil.getKakaoUserInfo(authorizationToken);

const email = userInfo.kakao_account.email;
if (!email) {
throw new BadRequestException('Please Agree to share your email');
}

const user = await this.userRepository.findOneByEmail(email);

if (user) {
return this.oauthLogin(user.email);
}

// 회원가입인 경우 기본 카테고리 생성 작업 진행
const newUser = User.of({
email,
name: userInfo.kakao_account.profile.nickname,
profileImage: userInfo.kakao_account.profile?.profile_image_url,
password: this.encodePasswordFromEmail(email, process.env.KAKAO_JS_KEY),
provider: PROVIDER.KAKAO,
});

await this.userRepository.createOne(newUser);
await this.categoryRepository.createDefaultCategories(newUser);
return this.oauthLogin(newUser.email);
}

// Login with Google account info
async googleOauth({
email,
name,
picture,
}: googleUserInfo): Promise<LoginOutput> {
const user = await this.userRepository.findOneByEmail(email);
const user = await this.userRepository.findOneByEmailAndProvider(
email,
PROVIDER.GOOGLE,
);

if (user) {
return this.oauthLogin(user.email);
return this.oauthLogin(user.email, PROVIDER.GOOGLE);
}

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

return this.oauthLogin(newUser.email);
return this.oauthLogin(newUser.email, PROVIDER.GOOGLE);
}

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

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

const user = await this.userRepository.findOneByEmail(email);
const user = await this.userRepository.findOneByEmailAndProvider(
email,
PROVIDER.APPLE,
);

if (user) {
return this.oauthLogin(user.email);
return this.oauthLogin(user.email, PROVIDER.APPLE);
}

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

return this.oauthLogin(newUser.email);
return this.oauthLogin(newUser.email, PROVIDER.APPLE);
}
}
7 changes: 4 additions & 3 deletions src/categories/category.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,10 @@ export class CategoryService {
});
});

const { title, siteName, description } = await getLinkInfo(encodeURI(link));

const content = await getLinkContent(link);
const [{ title, siteName, description }, content] = await Promise.all([
getLinkInfo(encodeURI(link)),
getLinkContent(link),
]);

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

Expand Down
1 change: 0 additions & 1 deletion src/contents/util/content.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class OGCrawler {
this.userAgent =
options.userAgent ||
'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';
this.maxRedirects = options.maxRedirects || 5;
this.cookies =
options.cookies || 'CONSENT=YES+cb; Path=/; Domain=.youtube.com';
this.proxy = options.proxy;
Expand Down
2 changes: 1 addition & 1 deletion src/users/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class User extends CoreEntity {
name: string;

@ApiProperty({ example: 'ex@g.com', description: 'User Email' })
@Column({ unique: true })
@Column({ type: 'varchar' })
@IsEmail()
email: string;

Expand Down
8 changes: 8 additions & 0 deletions src/users/repository/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DataSource, Repository } from 'typeorm';
import { User } from '../entities/user.entity';
import { Injectable } from '@nestjs/common';
import { GetOrCreateAccountBodyDto } from '../dtos/get-or-create-account.dto';
import { PROVIDER } from '../constant/provider.constant';

@Injectable()
export class UserRepository extends Repository<User> {
Expand Down Expand Up @@ -44,6 +45,13 @@ export class UserRepository extends Repository<User> {
return this.findOne({ where: { email } });
}

async findOneByEmailAndProvider(
email: string,
provider: PROVIDER,
): Promise<User | null> {
return this.findOne({ where: { email, provider } });
}

async createOne(user: Partial<User>): Promise<User> {
return this.save(user);
}
Expand Down