Skip to content

Commit

Permalink
✨ feat : 카카오 login 시에 등록되어 있지 않으면 User 정보 등록
Browse files Browse the repository at this point in the history
  • Loading branch information
jinddings committed Nov 12, 2024
1 parent f40807b commit 94364bb
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
10 changes: 6 additions & 4 deletions BE/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ export class AuthController {
@ApiOperation({ summary: 'Kakao 로그인 API' })
@Get('/kakao')
@UseGuards(AuthGuard('kakao'))
async kakaoLogin(
@Body() authCredentialsDto: AuthCredentialsDto,
@Res() res: Response,
) {
async kakaoLogin(@Req() req: Request, @Res() res: Response) {
const authCredentialsDto: AuthCredentialsDto = {
email: req.user.email,
kakaoId: req.user.kakaoId,
};
const { accessToken, refreshToken } =
await this.authService.kakaoLoginUser(authCredentialsDto);

res.cookie('accessToken', accessToken, { httpOnly: true });
res.cookie('refreshToken', refreshToken, { httpOnly: true });
res.cookie('isRefreshToken', true, { httpOnly: true });
Expand Down
7 changes: 7 additions & 0 deletions BE/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export class AuthService {
async kakaoLoginUser(
authCredentialsDto: AuthCredentialsDto,
): Promise<{ accessToken: string; refreshToken: string }> {
const user = await this.userRepository.findOne({
where: { kakaoId: authCredentialsDto.kakaoId },
});

if (!user) {
await this.userRepository.registerKakaoUser(authCredentialsDto);
}
return this.getJWTToken(authCredentialsDto);
}

Expand Down
6 changes: 6 additions & 0 deletions BE/src/auth/strategy/kakao.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface KakaoProfile extends Profile {
id: number;
_json: {
id: number;
kakao_account: {
email: string;
};
};
}

Expand Down Expand Up @@ -44,7 +47,10 @@ export class KakaoStrategy extends PassportStrategy<Strategy>(
try {
// eslint-disable-next-line no-underscore-dangle
const kakaoId = profile._json.id;
// eslint-disable-next-line no-underscore-dangle
const { email } = profile._json.kakao_account;
const user = {
email,
kakaoId,
};
done(null, user);
Expand Down
6 changes: 3 additions & 3 deletions BE/src/auth/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export class User extends BaseEntity {
@Column({ default: false })
tutorial: boolean;

@Column({ default: -1 })
kakaoId: number;
@Column({ default: '' })
kakaoId: string;

@Column({ default: '' })
currentRefreshToken: string;
Expand All @@ -25,7 +25,7 @@ export class User extends BaseEntity {
currentRefreshTokenExpiresAt: Date;

toAuthCredentialsDto(): AuthCredentialsDto {
if (this.kakaoId === -1) {
if (this.kakaoId === '') {
return {
email: this.email,
password: this.password,
Expand Down
8 changes: 8 additions & 0 deletions BE/src/auth/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export class UserRepository extends Repository<User> {
await this.save(user);
}

async registerKakaoUser(authCredentialsDto: AuthCredentialsDto) {
const { kakaoId, email } = authCredentialsDto;
const salt: string = await bcrypt.genSalt();
const hashedPassword: string = await bcrypt.hash(String(kakaoId), salt);
const user = this.create({ email, kakaoId, password: hashedPassword });
await this.save(user);
}

async updateUserWithRefreshToken(
id: number,
{
Expand Down
1 change: 0 additions & 1 deletion BE/src/stock/order/stock-order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Injectable,
} from '@nestjs/common';
import { NotFoundError } from 'rxjs';
import { Injectable } from '@nestjs/common';
import { StockOrderRequestDto } from './dto/stock-order-request.dto';
import { StockOrderRepository } from './stock-order.repository';
import { TradeType } from './enum/trade-type';
Expand Down
3 changes: 2 additions & 1 deletion BE/src/types/express.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { UUID } from 'crypto';
declare module 'express' {
interface Request extends Req {
user: {
kakaoId?: number;
kakaoId?: string;
userId?: UUID;
email?: string;
};
}
}

0 comments on commit 94364bb

Please sign in to comment.