-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: login(kakao) 코드 교체 & 훅 추가,
access, refresh 토큰 처리 기능 정상화 로그인/로그아웃은 react-kakao-login 라이브러리의 함수/타입 가져오기로 재구현
- Loading branch information
Showing
23 changed files
with
342 additions
and
241 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 |
---|---|---|
@@ -1,30 +1,12 @@ | ||
import axios, { AxiosError } from 'axios'; | ||
import { getAccessToken } from './user'; | ||
import axios from 'axios'; | ||
|
||
axios.defaults.baseURL = '/api'; | ||
axios.defaults.withCredentials = true; | ||
axios.interceptors.response.use( | ||
(response) => response, | ||
async (error: AxiosError) => { | ||
const originalRequest = error.config; | ||
switch (error.response.status) { | ||
// access 없음 | ||
// access 만료시 갱신 | ||
case 401: | ||
case 419: { | ||
await getAccessToken(); | ||
return axios(originalRequest); | ||
} | ||
// skip default | ||
} | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export * from './alarm'; | ||
export { default as getAlerts } from './alert'; | ||
export * from './event'; | ||
export { default as getLinks } from './link'; | ||
export * from './notice'; | ||
export * from './types.d'; | ||
export * from './user'; | ||
|
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 |
---|---|---|
@@ -1,41 +1,42 @@ | ||
import { QueryClient } from '@tanstack/react-query'; | ||
import axios from 'axios'; | ||
import { Dispatch } from 'react'; | ||
import { NavigateFunction } from 'react-router-dom'; | ||
import { Action } from '../../context'; | ||
import { ResponseLoginType } from './types'; | ||
|
||
export function login(id: number, kakao_accessToken: string) { | ||
console.log('login called'); | ||
return axios.post( | ||
'/login', | ||
{ | ||
id, | ||
kakao_accessToken, | ||
}, | ||
{ withCredentials: true }, | ||
).then(({ data }) => data as ResponseLoginType); | ||
return axios | ||
.post( | ||
'/login', | ||
{ | ||
id, | ||
kakao_accessToken, | ||
}, | ||
{ withCredentials: true } | ||
) | ||
.catch((error) => { | ||
throw error; | ||
}) | ||
.then(({ data }) => data as ResponseLoginType); | ||
} | ||
|
||
// eslint-disable-next-line max-len | ||
export const kakaoLogout = async (client: QueryClient, navigate: NavigateFunction, dispatch: Dispatch<Action>) => { | ||
export const logout = async () => { | ||
console.log('logout called'); | ||
return axios.post('/logout').then((d) => { | ||
if (d.data.status !== 1) return; | ||
client.removeQueries({ queryKey: ['login'] }); | ||
client.removeQueries({ queryKey: ['userInfo'] }); | ||
client.removeQueries({ queryKey: ['getAlarms'] }); | ||
dispatch({ payload: 'logout' }); | ||
navigate('/my', { replace: true }); | ||
if (d.data.status !== 1) { | ||
console.warn('로그아웃 오류'); | ||
} | ||
}); | ||
}; | ||
|
||
export const getUserInfo = async (id: number) => { | ||
console.log('get UserInfo called'); | ||
console.log('get UserInfo called', axios.interceptors.response); | ||
return axios.get(`/users/info/${id}`).then((res) => res.data); | ||
}; | ||
|
||
export const getAccessToken = async () => { | ||
console.log('get accessToken called'); | ||
return axios.post('/getToken').then((res) => res.data); | ||
return axios.post('/getToken').then((res) => { | ||
console.log({ res }); | ||
return res.data; | ||
}); | ||
}; |
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 |
---|---|---|
@@ -1,30 +1,34 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import axios from 'axios'; | ||
import { User, UserData } from '../../context'; | ||
import { getUserInfo, login } from '../axios'; | ||
import { getUserInfo } from '../axios'; | ||
|
||
export const useLoginQuery = (id: number, kakao_accessToken: string) => { | ||
const info = useQuery({ | ||
queryKey: ['login'], | ||
queryFn: () => login(id, kakao_accessToken), | ||
enabled: !!(id && kakao_accessToken), | ||
retry: false, | ||
staleTime: Infinity, | ||
}); | ||
return info; | ||
}; | ||
// export const useLoginQuery = (isAuthenticated: boolean) => { | ||
// const id = sessionStorage.getItem('id'); | ||
// const kakaoAccessToken = sessionStorage.getItem('kakaoAccessToken'); | ||
// const info = useQuery({ | ||
// queryKey: ['login'], | ||
// queryFn: () => login(+id, kakaoAccessToken), | ||
// enabled: !isAuthenticated && Boolean(id && kakaoAccessToken), | ||
// retry: false, | ||
// staleTime: Infinity, | ||
// }); | ||
// return info; | ||
// }; | ||
|
||
export const useUserInfoQuery = (userData: UserData) => { | ||
const { userId, isAuthenticated } = userData; | ||
// const queryClient = useQueryClient(); | ||
const userInfo = useQuery({ | ||
queryKey: ['userInfo'], | ||
queryFn: () => getUserInfo(userId), | ||
enabled: isAuthenticated, | ||
enabled: isAuthenticated && Boolean((axios.interceptors.response as any).handlers.length), | ||
staleTime: Infinity, | ||
select(data) { | ||
const { bookmark, eventLike, noticeLike, noticeRead } = data; | ||
return new User(userData, bookmark, noticeRead, noticeLike, eventLike); | ||
}, | ||
retry: 1 | ||
}); | ||
return userInfo; | ||
}; |
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 |
---|---|---|
@@ -1,27 +1,20 @@ | ||
import { memo } from 'react'; | ||
import ChatYellow from '../../assets/Img/ChatYellow.png'; | ||
import useKakaoLogin from '../../hook/useKakaoLogin'; | ||
import { useKakaoAuth } from '../../hooks/useKakaoAuth'; | ||
import * as s from '../../pages/SignupPage/styles'; | ||
|
||
type LoginButtonProps = { | ||
showLogin: boolean | ||
showLogin: boolean; | ||
}; | ||
|
||
const LoginButton = memo(({ showLogin }: LoginButtonProps) => { | ||
const { kakaoLoginRef, KakaoLoginWrapper } = useKakaoLogin(); | ||
const { kakaoLogin } = useKakaoAuth(); | ||
|
||
const handleClick = () => { | ||
if (kakaoLoginRef.current) { | ||
kakaoLoginRef?.current.onButtonClick(); | ||
} | ||
kakaoLogin(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<s.Member src={ChatYellow} show={showLogin} onClick={handleClick} /> | ||
<KakaoLoginWrapper /> | ||
</> | ||
); | ||
return <s.Member src={ChatYellow} show={showLogin} onClick={handleClick} />; | ||
}); | ||
|
||
export default LoginButton; |
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
Oops, something went wrong.