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

[Feat] : Auth #8

Closed
2 of 4 tasks
wochae opened this issue Jun 2, 2023 · 6 comments
Closed
2 of 4 tasks

[Feat] : Auth #8

wochae opened this issue Jun 2, 2023 · 6 comments

Comments

@wochae
Copy link
Owner

wochae commented Jun 2, 2023

  • board 와 연결 지어서 유저와 게시물의 관계 형성
  • 게시물 생성시 유저 정보 넣어주기
  • 해당 유저의 게시물만 가져오기
  • 자신의 생성한 게시물만 삭제 가능하게 하기
@wochae wochae changed the title Auth [Feat] : Auth Jun 2, 2023
@wochae
Copy link
Owner Author

wochae commented Jun 3, 2023

유저와 게시물 데이터의 관계 형성을 위해 서로 간의 엔티티에 필드를 넣어줘야 한다.
일 대 다, 다 대 일

일 대 다 : OneToMany( 타입, 상대, 옵션 )
유저 하나 당 여러 게시글을 쓸 수 있다.
유저 객체 보드는 일 대 다 관계

보드 여러 개가 무조건 하나의 유저 ( 작성자 ) 를 가지고 있다.
보드 객체 유저는 다 대 일 관계

OneToMany
첫 번째 Type 객체의 타입을 지정하고,
두 번째 inverseSide 유저는 board.user 로 접근해야한다.
세 번째 Option은 eager : true 면 user 정보 가져올 때 board도 같이 가져온다.

wochae added a commit that referenced this issue Jun 3, 2023
@wochae
Copy link
Owner Author

wochae commented Jun 3, 2023

로그인하면 AccessToken이 발급된다. 그걸 Bearer Token 으로 넣은 상태로 새로운 board 를 post 하면 그 게시물에 작성자 아이디가 넣어진다.

@wochae
Copy link
Owner Author

wochae commented Jun 3, 2023

해당 유저의 게시물을 찾기 위한 쿼리 빌더 사용하기
물론 트센에서는 사용을 권장하진 않는다.
query.where() 는 SQL 문 중 where 절을 의미한다. 'board.userId = :userId' 와 { userId: user.id } 를 인자로 넣는데,
지금 백 로직 상에는 user.id 값을 가져와 userId에 주입한 걸 인자로 SQL where 조건절에 들어가게 되는 것이다.
createQueryBuilder에서 해당 도메인을 담으면 그걸 디비 단에서 가져오는 것 같다.
JPA 느낌이랄까

wochae added a commit that referenced this issue Jun 3, 2023
wochae added a commit that referenced this issue Jun 3, 2023
wochae added a commit that referenced this issue Jun 3, 2023
@wochae
Copy link
Owner Author

wochae commented Jun 3, 2023

삭제를 할 때, 유저 객체를 인자로 넘겨야되는데 또 지원을 안 해준단다. 그래서 두 가지 방법이 있는데
하나는 fix 저거이고 다른 하나는 아예 delete 함수 안에 들어가서 Repository.d.ts 안에서 criteria: 앞에 { id:number, user:User } 이렇게 넣어도 된다. 이게 뭔가 신선하지만 호환은 안될 것 같아서 명시적으로 user 타입을 받는 조건 주는 걸로 해결함.

@wochae
Copy link
Owner Author

wochae commented Jun 3, 2023

const result = await this.boardRepository.createQueryBuilder('board')
.delete()
.from(Board)
.where("userId = :userId", { userId: user.id})
.andWhere( "id = :id", { id: id } ).execute();

typeORM docs: 쿼리빌더 delete 메소드

@wochae
Copy link
Owner Author

wochae commented Jun 3, 2023

다음 이슈는 로그다!

@wochae wochae closed this as completed Jun 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant