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] 유저 인증 시스템 구현 #13

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

oooppq
Copy link
Collaborator

@oooppq oooppq commented Sep 13, 2023

PR 요약

유저 인증(login, logout, refresh, private routing) 기능 추가

변경된 점

유저인증 zustand store, custom hook와, 인증을 유지해주는 컴포넌트인 AuthProvider와 특정 페이지 진입시 인증 여부에 따라 분기시켜주는 withAuth를 추가했다. 인증 시스템에 대한 자세한 것은 노션에 적어두었다.

이슈 번호

X

/login의 page.tsx에서 로그인과 redirection 과정을 모두 처리하는 것은
비효율적인 것 같아 /login/<provider>에서 redirection을 처리하도록 변경했고,
해당 경로의 page.tsx에서는 받은 code를 서버에 전달한 후 인증 정보를 받아
전역적인 auth state를 변경시켜준다. 모든 작업이 끝나면 일단은 home으로 이동시키는데,
추후에는 로그인 페이지로 이동하기 이전의 경로로 redirect 해주도록 설정해주면 좋을 것 같다.
유저 인증 정보를 state로 관리하기 위해 zustand를 사용하여 useAuthStore을 구성했다.
또한, useAuth라는 커스텀 훅을 만들어, login, logout, refresh, authFetch 함수들을 사용할 수 있도록 헀다.
authFetch는 인증이 필요한 요청을 처리하는 custom Fetch 함수다.
유저가 로그인하면 login/<provider>에서 login 요청을 서버에 보낸후 authorized된다.
처음 앱이 렌더링될 때마다 refresh 함수를 작동시킬 수 있도록 AuthProvider 컴포넌트를 만들었고, 해당 컴포넌트 안에서
refresh 함수를 통해 authorizing을 시도한다.
@oooppq oooppq added the Feature💫 새로운 기능, 컴포넌트 label Sep 13, 2023
@oooppq oooppq self-assigned this Sep 13, 2023
withAuth 함수를 정의하여 특정 페이지 진입시 auth 여부에 따라 분기 처리할 수 있도록 했다.
원하는 페이지 컴포넌트에 withAuth 함수를 씌워 export 하면 된다.한 가지 단점은 react hook을
사용하는 기능이기 때문에 해당 페이지를 client component로 선언해야 한다는 점이다.
또한, auth 관련 fetching 과정에서 loading 중인 state를 기록하기 위해 authInfo에 isLoading이란 속성을
추가했다.
변수 선언부 모으기, auth 관련 컴포넌트 commom/auth로 이동, useAuth의 파일이름 useAuth.ts로 변경, auth state 를 auth state와 user state로 변경, useEffect에 적절한 dependency 추가
일반 fetch 함수와 유사한 형태로, fetch options를 전달받도록 변경했다.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature💫 새로운 기능, 컴포넌트
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant