Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

구글 소셜 로그인을 지원하는 유저, Auth 모듈 구현 #64

Merged
merged 26 commits into from
Nov 7, 2024

Conversation

hyohyo12
Copy link
Collaborator

@hyohyo12 hyohyo12 commented Nov 6, 2024

📄 Summary

구글 OAuth Api 를 이용해서 소셜 로그인 구현
Jwt 를 통해 클라이언트로 토큰 전달
JwtGuard 구현
map.service 에서 UserRepository 주입 방식 변경

🙋🏻 More

최근 TypeORM 에서 @InjectRepository(User) 를 사용하지 못해서 직접 주입해서 사용

🕰️ Actual Time of Completion

16

close #8

Copy link
Collaborator

@Miensoap Miensoap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

린트 깨지는 건 제가 수정해서 올려놨어요!
pull 한번 하시고 리뷰 확인해 주세요~

수정 다 하시면 이거 머지하고 #60 에서 conflict 해결하고 머지할게요!

backend/src/auth/auth.controller.ts Outdated Show resolved Hide resolved
backend/src/auth/auth.service.ts Show resolved Hide resolved
backend/src/auth/auth.service.ts Show resolved Hide resolved
export class UserService {
constructor(private readonly userRepository: UserRepository) {}

async saveUser(userInfo: userInfoWithProvider) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1. 여기서 id (PK) 를 반환해서 토큰에 담는건 어떨까요?
이후 권한 확인에서 db 조회를 생략할 수 있을 것 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 알겠습니다!

Copy link
Collaborator

@koomchang koomchang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정말 고생 많으십니다.. 슬슬 끝이 보이네요

backend/src/auth/auth.service.ts Outdated Show resolved Hide resolved
backend/src/auth/auth.service.ts Show resolved Hide resolved
backend/src/auth/auth.service.ts Show resolved Hide resolved
Comment on lines 2 to 4
ADMIN,
GUEST,
MEMBER,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p2: 게스트는 유저 정보가 저장되지 않을 텐데 가지고 있을 이유가 없어보입니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 넵 정정하겠습니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아직 안 바뀐 것 같네요!

import { Controller } from '@nestjs/common';
import { UserService } from './user.service';

@Controller('user')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1: REST API에서 URL 도메인은 복수를 권장합니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오오오 몰랐던 사실이네요.. 감사합니다!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 아직..

backend/src/user/user.repository.ts Outdated Show resolved Hide resolved
backend/src/user/user.repository.ts Outdated Show resolved Hide resolved
backend/src/user/user.repository.ts Outdated Show resolved Hide resolved
Comment on lines +27 to +36
async createUser(userData: Partial<User>): Promise<User> {
const user = this.create();
Object.assign(user, {
provider: userData.provider,
oauthId: userData.oauthId,
nickname: userData.nickname,
role: userData.role,
profileImageUrl: userData.profileImageUrl,
});
return this.save(user);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1: 참고해서 바꾸면 좋을 것 같아요

//src/place/place.service.ts

async addPlace(createPlaceRequest: CreatePlaceRequest) {
    const { googlePlaceId } = createPlaceRequest;
    if (await this.placeRepository.findByGooglePlaceId(googlePlaceId)) {
      throw new PlaceAlreadyExistsException();
    }

    const place = createPlaceRequest.toEntity();
    const savedPlace = await this.placeRepository.save(place);
    return { id: savedPlace.id };
  }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createUserRequest 클래스를 만들고 나서 저장하는 방식 맞죠???

profileImageUrl: picture,
role: 'user',
},
{ conflictPaths: ['id'] },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: { conflictPaths: ['id'] }, 의 역할이 뭔가요?

Copy link
Collaborator

@Miensoap Miensoap Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1. oauthId 가 conflictPaths 가 되야 할 것 같네요.

q: { conflictPaths: ['id'] }, 의 역할이 뭔가요?

존재하지 않는다면 insert 해주는 기준입니다!

@Miensoap Miensoap added BE 백엔드 관련 이슈입니다 🌱 기능추가 새로운 기능 요청입니다 labels Nov 7, 2024
@Miensoap Miensoap added this to the week2 milestone Nov 7, 2024
@@ -0,0 +1,26 @@
import { Injectable } from '@nestjs/common';
import { UserRepository } from './user.repository';
// import { userInfoWithProvider } from './userType';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p2. 주석제거

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

놓쳤네요..

provider,
oauthId,
);
if (existingUser) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1. findByProviderAndOauthId 에서 항상 유저를 반환
( provider, oauthid를 기준으로 upsert로 저장하고 정보 반환 ) 하면
여기서 분기 처리가 필요 없을 것 같아요.

image

해당 로직을 표현하는 이름이면 더 좋겠네요
InsertResult 타입 확인해 보시고 , find 없이 저기서 꺼내올 수 있다면 사용

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

upsert 를 사용하지 않았던 이유가 처음에 member 로 role 을 정하는데 만약에 admin 이라는 role 을 가진다면 로그인 할 때마다 role 이 member 로 변하기에 선택하긴 했습니다. 다른 좋은 방법이 있을까요? upsert 를 사용하면서 role 이 로그인할 때 불변하게 되는...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 다른 다른 문제가 엮여있었군여
어드민 토큰은 따로 발급하는 등...

그럼 일단 두고 더 이야기해 보아요!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 그건 따로 생각해봐야겠습니당..ㅠㅠ 권한 페이지나 권한 부여 방식? 이런 세부사항 정해지면 수정하겠습니다!

) {}

@Post('google/signIn')
@HttpCode(201)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p2: Post 기본값이 201이라 없어도 될 것 같아요


generateJwt(payload: any): string {
return jwt.sign(payload, this.jwtSecretKey, {
expiresIn: '24h',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p2: jwt expiration도 env로 옮길 수 있으면 좋을 것 같아요

Comment on lines 2 to 4
ADMIN,
GUEST,
MEMBER,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아직 안 바뀐 것 같네요!

import { Controller } from '@nestjs/common';
import { UserService } from './user.service';

@Controller('user')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 아직..

@hyohyo12 hyohyo12 requested a review from koomchang November 7, 2024 08:26
Copy link
Collaborator

@koomchang koomchang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

Copy link
Collaborator

@Miensoap Miensoap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^^

@Miensoap Miensoap merged commit ae026d8 into develop Nov 7, 2024
2 checks passed
@hyohyo12 hyohyo12 deleted the temp/#8 branch November 11, 2024 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BE 백엔드 관련 이슈입니다 🌱 기능추가 새로운 기능 요청입니다
Projects
None yet
Development

Successfully merging this pull request may close these issues.

간편 구글 로그인 기능을 구현한다
3 participants