Open
Conversation
Feature/register
Feature/vote
Feature/vote
feat: axiosprivate api
[Feature/auth] auth 관련 코드 전체 리팩토링
Feature/auth
feat: useDecodeAccessToken
fix: token error
fix: persist login uselocalstorage
…복귀 로직 추가, 모든 기능에 대한 테스트 완료
Member
|
과제하시느라 고생 많으셨습니다. 리드미를 자세히 작성해 주셔서 다른 팀에게도 많은 도움이 될 것 같네요! 이따 발표도 기대하고 있겠습니다! 그럼 스터디 시간에 뵐게요~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Deployment
https://react-vote-15th-pokedon.vercel.app/
TODO
Promise.then()보단async/await를 사용한다.구현 사항
뱅키즈 프로젝트에 바로 이식 하여 사용할 것을 계획하여, 다소 복잡하지만 JWT를 활용한 Auth 관련 코드를 정석에 가깝게 작성했습니다.
0. boilerplate 세팅
1. 회원가입 페이지 구현 (Form Validation, Axios User Registration Form Submit)
사용자 이름, 비밀번호, 비밀번호 확인, 이메일 각 입력에 대한 유효성 검증
access token은 encoded 상태로 memory(App state)를 통해 관리, refresh token은 http only, secure cookie를 통해 관리
데이터 반입 과정에서 발생하는 에러 메세지 랜더링
서버로 요청 전송 직전 입력에 대한 유효성 추가 검증
production mode가 아닌 경우 react-devtools, redux-devtools disable
2. 로그인 페이지 구현 (User Login and Authentication with Axios)
3. 라우팅 구현 (Protected Routes, Role-Based Authorization)
4. JWT 관리 (Login Authentication with JWT Access, Refresh Tokens with Axios interceptors)
4.1. *
useRefreshToken구현: refresh access token4.2.
useAxiosPrivate구현: 특정 권한에 따른 Axios requestusePrivateAxios는 interceptors를 포함한 axios instance를 반환하는 hook이다. 특정 권한이 요구되는 request에 대해 interceptors를 통해 request 시 header의 Authorization field에 access token을 포함하도록 한다. 또한 access token 만료에 따른 request 실패 시 access token을 갱신하고 Authorization field에 new access token을 포함하도록 한다.
useRefreshToken)5. 자동 로그인, 로그아웃 구현 (Persistent User Login Authentication with JWT Tokens)
5.1. 자동 로그인 구현
특정 권한이 필요한 페이지에 접근하는 라우팅 과정: PersistLogin -> RequireAuth -> 해당 페이지 (nested routing)
RequireAuth: 유저의 권한에 따라 특정 페이지 접근을 제한
PersistLogin: RequreAuth로 접근하기 전 app state를 확인하여, 해당 값이 비어있는 경우 (새로고침 시) 재로그인 진행
useRefreshToken) (재로그인)5.2. 로그아웃 구현
6. RTK 비동기 처리
ex. 후보자 조회 api를 통해 가져온 값을 리덕스 스토어에 저장
axios get 요청으로 값을 받아와 data를 리턴하는 함수
createAsyncThunk를 통해 thunk를 RTK에서 사용할 수 있다.slice의 extraReducers에서 thunk의 결과를 받아오는데,
createAsyncThunk에서 에러를 catch해 버리면 리듀어에서는 에러를 받지 못해 무조건 fulfilled로 처리된다. 오류가 나는 상황에선rejectWithValue를 이용하면 reject로 넘겨 처리할 수 있다.7. 메인, 투표, 투표 현황 페이지