-
Notifications
You must be signed in to change notification settings - Fork 2
FIx, Feat: 로그인 기능 버그 수정, 권한별 접근 페이지 기능 추가 #56
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { useEffect, useState } from 'react'; | ||
| import { useNavigate, Outlet } from 'react-router'; | ||
|
|
||
| import useAuthStore from '@/stores/authStore'; | ||
|
|
||
| export default function PrivateRoute() { | ||
| const isLoggedIn = useAuthStore((state) => state.isLoggedIn); | ||
| const navigate = useNavigate(); | ||
| const [shouldRender, setShouldRender] = useState(false); | ||
|
|
||
| useEffect(() => { | ||
| if (!isLoggedIn) { | ||
| navigate('/login', { replace: true }); | ||
| } else { | ||
| setShouldRender(true); | ||
| } | ||
| }, [isLoggedIn, navigate]); | ||
|
|
||
| if (!shouldRender) { | ||
| return null; | ||
| } | ||
|
|
||
| return <Outlet />; | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| /* eslint-disable @typescript-eslint/no-unused-expressions */ | ||
| import { useEffect } from 'react'; | ||
| import { useNavigate } from 'react-router'; | ||
|
|
||
|
|
@@ -10,73 +9,75 @@ const AuthCallbackPage = () => { | |
| const redirectURL = new URLSearchParams(window.location.search).get('redirect'); | ||
|
|
||
| const login = useAuthStore((state) => state.login); | ||
| const logout = useAuthStore((state) => state.logout); | ||
| const setAccessToken = useAuthStore((state) => state.setAccessToken); | ||
| const setZipCode = useAuthStore((state) => state.setZipCode); | ||
|
|
||
| const navigate = useNavigate(); | ||
|
|
||
| const handleError = (error: unknown) => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error 타입으로도 지정할 수 있다고 하네용22 |
||
| console.error('AuthCallback Error:', error); | ||
| logout(); | ||
| navigate('/login', { replace: true }); | ||
| }; | ||
|
|
||
| const setUserInfo = async (stateToken: string) => { | ||
| try { | ||
| const response = await getUserToken(stateToken); | ||
| if (!response) throw new Error('Error Fetching userInfo'); | ||
| console.log(response); | ||
| if (!response) throw new Error('Error fetching user token'); | ||
|
|
||
| const userInfo = response.data; | ||
| if (userInfo) { | ||
| login(); | ||
| userInfo.accessToken && setAccessToken(userInfo.accessToken); | ||
|
|
||
| if (redirectURL == 'home') { | ||
| const zipCodeResponse = await getMydata(); | ||
| if (!zipCodeResponse) throw new Error('Error Fetching userInfo'); | ||
| const zipCode = zipCodeResponse.data.data.zipCode; | ||
| zipCode && setZipCode(zipCode); | ||
|
|
||
| console.log( | ||
| 'isLoggedIn', | ||
| useAuthStore.getState().isLoggedIn, | ||
| 'access', | ||
| useAuthStore.getState().accessToken, | ||
| 'zipCode', | ||
| useAuthStore.getState().zipCode, | ||
| ); | ||
| } else if (redirectURL === 'onboarding') { | ||
| const createZipCodeResponse = await postZipCode(); | ||
| if (!createZipCodeResponse) throw new Error('Error creating ZipCode'); | ||
| const zipCode = createZipCodeResponse.data.data.zipCode; | ||
| console.log(createZipCodeResponse); | ||
| const newAccessToken = createZipCodeResponse.headers['Authorization']; | ||
| setZipCode(zipCode); | ||
| setAccessToken(newAccessToken); | ||
| console.log( | ||
| 'isLoggedIn', | ||
| useAuthStore.getState().isLoggedIn, | ||
| 'access', | ||
| useAuthStore.getState().accessToken, | ||
| 'zipCode', | ||
| useAuthStore.getState().zipCode, | ||
| ); | ||
| } | ||
| } else { | ||
| navigate('/login'); | ||
| if (!userInfo) throw new Error('Invalid user info'); | ||
|
|
||
| login(); | ||
| if (userInfo.accessToken) setAccessToken(userInfo.accessToken); | ||
|
|
||
| switch (redirectURL) { | ||
| case 'home': | ||
| { | ||
| const zipCodeResponse = await getMydata(); | ||
| if (!zipCodeResponse) throw new Error('Error fetching user data'); | ||
| setZipCode(zipCodeResponse.data.data.zipCode); | ||
| } | ||
| break; | ||
|
|
||
| case 'onboarding': | ||
| { | ||
| const createZipCodeResponse = await postZipCode(); | ||
| if (!createZipCodeResponse) throw new Error('Error creating ZipCode'); | ||
|
|
||
| setZipCode(createZipCodeResponse.data.data.zipCode); | ||
| const newAccessToken = createZipCodeResponse.headers['authorization']?.split(' ')[1]; | ||
| if (!newAccessToken) throw new Error('Missing new access token'); | ||
|
|
||
| setAccessToken(newAccessToken); | ||
| } | ||
| break; | ||
|
|
||
| default: | ||
| navigate('/notFound'); | ||
| return; | ||
| } | ||
| navigate(redirectURL === 'onboarding' ? '/onboarding' : '/'); | ||
| } catch (error) { | ||
| console.error(error); | ||
| handleError(error); | ||
| } | ||
| }; | ||
|
|
||
| const redirection = () => { | ||
| if (redirectURL === 'onboarding') navigate('/onboarding'); | ||
| else if (redirectURL === 'home') navigate('/'); | ||
| else navigate('/notFound'); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (stateToken) { | ||
| setUserInfo(stateToken as string); | ||
| redirection(); | ||
| } else navigate('/notFound'); | ||
| }, []); | ||
| return <></>; | ||
| if (!stateToken) { | ||
| navigate('/notFound'); | ||
| return; | ||
| } | ||
|
|
||
| const fetchData = async () => { | ||
| await setUserInfo(stateToken as string); | ||
| }; | ||
|
|
||
| fetchData(); | ||
| }, [stateToken, navigate]); | ||
|
|
||
| return null; | ||
| }; | ||
|
|
||
| export default AuthCallbackPage; | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error타입으로 수정하셔도 좋을거 같습니다!!