-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#274 SSR에서 토큰 탑재 자동화 및 마이페이지 리팩토링
- Loading branch information
Showing
7 changed files
with
145 additions
and
91 deletions.
There are no files selected for viewing
This file contains 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 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,17 @@ | ||
import { fetchMyPageWithSSR, sendEmailVerification } from '@apis/mypage'; | ||
|
||
const QUERY_MANAGEMENT = { | ||
mypage: { | ||
queryKey: 'mypage', | ||
queryFn: fetchMyPageWithSSR, | ||
}, | ||
}; | ||
|
||
const MUTATION_MANAGEMENT = { | ||
email: { | ||
mutateKey: 'sendEmail', | ||
mutateFn: (email: string) => sendEmailVerification(email), | ||
}, | ||
}; | ||
|
||
export { QUERY_MANAGEMENT, MUTATION_MANAGEMENT }; |
This file contains 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,22 @@ | ||
import { MUTATION_MANAGEMENT } from '@constants/queryManagement'; | ||
import useInput from '@hooks/useInput'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import { useState } from 'react'; | ||
|
||
const useSendEmailMutation = () => { | ||
const [email, setEmail, resetEmail] = useInput(''); | ||
const [isSendVerifyEmail, setIsSendVerifyEmail] = useState<boolean>(false); | ||
|
||
const { mutate } = useMutation({ | ||
mutationKey: [MUTATION_MANAGEMENT.email.mutateKey], | ||
mutationFn: () => MUTATION_MANAGEMENT.email.mutateFn(email), | ||
onMutate: () => { | ||
alert('이메일을 보냈습니다.'); | ||
setIsSendVerifyEmail(true); | ||
}, | ||
}); | ||
|
||
return { email, setEmail, resetEmail, isSendVerifyEmail, mutate }; | ||
}; | ||
|
||
export default useSendEmailMutation; |
This file contains 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,13 @@ | ||
import { QUERY_MANAGEMENT } from '@constants/queryManagement'; | ||
import { useSuspenseQuery } from '@tanstack/react-query'; | ||
|
||
const useMyPageQuery = () => { | ||
const { data, refetch } = useSuspenseQuery({ | ||
queryKey: [QUERY_MANAGEMENT.mypage.queryKey], | ||
queryFn: QUERY_MANAGEMENT.mypage.queryFn, | ||
}); | ||
|
||
return { data, refetch }; | ||
}; | ||
|
||
export default useMyPageQuery; |
This file contains 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,22 @@ | ||
import { useState, useCallback } from 'react'; | ||
|
||
const useInput = (initialValue: string) => { | ||
const [inputValue, setInputValue] = useState<string>(initialValue); | ||
|
||
const handleValue = useCallback( | ||
(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { | ||
const { value } = e.target; | ||
|
||
setInputValue(value); | ||
}, | ||
[], | ||
); | ||
|
||
const resetValue = useCallback(() => { | ||
setInputValue(''); | ||
}, []); | ||
|
||
return [inputValue, handleValue, resetValue] as const; | ||
}; | ||
|
||
export default useInput; |
This file contains 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 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,25 @@ | ||
import authAPI from '@apis/authAPI'; | ||
import { parse } from 'cookie'; | ||
import { GetServerSidePropsContext } from 'next'; | ||
|
||
const withAuthServerSideProps = (getServerSidePropsFunction: () => Promise<any>) => { | ||
return async (context: GetServerSidePropsContext) => { | ||
const cookies = context.req.headers.cookie || ''; | ||
|
||
const accessToken = parse(cookies).accessToken; | ||
|
||
if (!accessToken) { | ||
throw new Error('401 Unauthorized'); | ||
} | ||
|
||
authAPI.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`; | ||
|
||
const res = await getServerSidePropsFunction(); | ||
|
||
authAPI.defaults.headers.common['Authorization'] = ''; | ||
|
||
return res; | ||
}; | ||
}; | ||
|
||
export default withAuthServerSideProps; |