-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge/feat: axios instance 생성 및 로그인/회원가입 api 연동
- Loading branch information
Showing
21 changed files
with
307 additions
and
185 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,26 @@ | ||
import { useMutation, useQuery } from 'react-query'; | ||
import instance from '../instance'; | ||
import { BaseResponse } from '../../types/BaseResponse'; | ||
import { LoginSuccess } from '../../types/auth/login'; | ||
|
||
export const socialLogin = async (social: string, code: string) => { | ||
const response = await instance.post<BaseResponse<LoginSuccess>>( | ||
`/user/signin`, | ||
{ | ||
socialType: social, | ||
code: code, | ||
}, | ||
); | ||
return response.data; | ||
}; | ||
|
||
export const useLogInDataQuery = (social: string, code: string) => { | ||
const { data: loginData, isLoading: isLoginDataLoading } = useQuery({ | ||
queryKey: ['socialLogin'], | ||
|
||
// 쿼리 함수를 설정. 이 함수는 API 호출을 담당하며, 여기서는 fetchFeedsData 함수를 호출 | ||
queryFn: () => socialLogin(social, code), | ||
}); | ||
|
||
return { loginData, isLoginDataLoading }; | ||
}; |
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,52 @@ | ||
import { useMutation } from 'react-query'; | ||
import instance from '../instance'; | ||
import { BaseResponse } from '../../types/BaseResponse'; | ||
import { LoginSuccess } from '../../types/auth/login'; | ||
import { ResponseCode } from '../../types/enum/ResponseCode'; | ||
import useRegisterStore from '../../stores/registerStore'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
export const useSignUpMutation = (prevUrl: string) => { | ||
const { setLogIn } = useRegisterStore(); | ||
const navigate = useNavigate(); | ||
|
||
const mutation = useMutation({ | ||
mutationFn: async (data: FormData) => { | ||
return await instance.post<BaseResponse<LoginSuccess>>( | ||
`/user/singup`, | ||
data, | ||
{ | ||
headers: { 'Content-Type': 'multipart/form-data' }, | ||
}, | ||
); | ||
}, | ||
onSuccess: (response) => { | ||
const respone = response.data; | ||
|
||
if (respone.code === ResponseCode.LOGGED_IN) { | ||
console.log('새로운 사용자, 회원가입 성공!'); | ||
|
||
const profileId = respone.result.profileId; | ||
const imgUrl = respone.result.imgUrl; | ||
const accessToken = respone.result.accessToken; | ||
setLogIn(profileId, imgUrl, accessToken); | ||
|
||
instance.defaults.headers.common['Authorization'] = | ||
`Bearer ${accessToken}`; //로그인 된 유저에 대하여 모든 api 호출에 accesstoken 포함시키는 코드 | ||
} else { | ||
//TODO: 회원가입 오류 | ||
} | ||
|
||
if (prevUrl === '/') navigate('/timeline'); | ||
else navigate(prevUrl); | ||
}, | ||
onError: () => { | ||
console.log('회원가입 실패'); | ||
navigate(prevUrl); | ||
}, | ||
}); | ||
|
||
return mutation; | ||
}; | ||
|
||
export default useSignUpMutation; |
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,34 @@ | ||
import axios from 'axios'; | ||
import useRegisterStore from '../stores/registerStore'; | ||
|
||
// axios 인스턴스 생성 | ||
const instance = axios.create({ | ||
baseURL: `${process.env.REACT_APP_BASE_URL}`, | ||
withCredentials: true, //쿠키를 포함한 요청을 보낼 때 필요 | ||
}); | ||
|
||
// 요청 인터셉터 | ||
instance.interceptors.request.use( | ||
(config) => { | ||
// console.log('axios config : ', config); | ||
return config; | ||
}, | ||
(error) => { | ||
// 요청 에러 처리 | ||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
// 응답 인터셉터 | ||
instance.interceptors.response.use( | ||
(response) => { | ||
console.log('response: ', response); | ||
return response; | ||
}, | ||
// (error) => { | ||
// // 응답 에러 처리 | ||
// throw new Error(' data error'); | ||
// }, | ||
); | ||
|
||
export default instance; |
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
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
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
5 changes: 0 additions & 5 deletions
5
src/components/profile_setting/UserDataInputContainer.module.scss
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.