-
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.
Merge pull request #47 from BOBpossible/feat/#10_LoginAPI
Feat/#10 login api
- Loading branch information
Showing
15 changed files
with
239 additions
and
127 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,17 +1,80 @@ | ||
import axios, {AxiosInstance} from 'axios'; | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
||
export const customAxios = (token?: string, params?: {}): AxiosInstance => { | ||
console.log(params); | ||
if (token === undefined) { | ||
return axios.create({ | ||
baseURL: 'https://bobpossible.shop', | ||
}); | ||
} else { | ||
return axios.create({ | ||
baseURL: 'https://bobpossible.shop', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
const getAccessToken = async () => { | ||
try { | ||
const value = await AsyncStorage.getItem('accessToken'); | ||
if (value !== null) { | ||
return value; | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; | ||
|
||
const getRefreshToken = async () => { | ||
try { | ||
const value = await AsyncStorage.getItem('refreshToken'); | ||
if (value !== null) { | ||
console.log(value); | ||
return value; | ||
} | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; | ||
|
||
export const customAxios = (): AxiosInstance => { | ||
const axiosInstance = axios.create({ | ||
baseURL: 'https://bobpossible.shop', | ||
}); | ||
|
||
axiosInstance.interceptors.request.use( | ||
async (config: any) => { | ||
const token = await getAccessToken(); | ||
config.headers.Authorization = `Bearer ${token}`; | ||
return config; | ||
}, | ||
function (error) { | ||
// Do something with request error | ||
// 요청 시 에러 처리 | ||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
axiosInstance.interceptors.response.use( | ||
(response) => { | ||
return response; | ||
}, | ||
async (error) => { | ||
const { | ||
config, | ||
response: {status}, | ||
} = error; | ||
if (status === 401) { | ||
const accessToken = await getAccessToken(); | ||
const refreshToken = await getRefreshToken(); | ||
const originalRequest = config; | ||
// token refresh 요청 | ||
const {data} = await axios.post( | ||
'https://bobpossible.shop/auth/token', // token refresh api | ||
null, | ||
{params: {accessToken: accessToken, refreshToken: refreshToken}}, | ||
); | ||
// 새로운 토큰 저장 | ||
console.log('토큰이 만료 되어 토큰 갱신한 데이터: ', data); | ||
const {accessToken: newAccessToken, refreshToken: newRefreshToken} = data; | ||
await AsyncStorage.multiSet([ | ||
['accessToken', newAccessToken], | ||
['refreshToken', newRefreshToken], | ||
]); | ||
axios.defaults.headers.common.Authorization = `Bearer ${newAccessToken}`; | ||
originalRequest.headers.Authorization = `Bearer ${newAccessToken}`; | ||
// 401로 요청 실패했던 요청 새로운 accessToken으로 재요청 | ||
return axios(originalRequest); | ||
} | ||
return Promise.reject(error); | ||
}, | ||
); | ||
return axiosInstance; | ||
}; |
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,3 @@ | ||
export * from './customAxios'; | ||
export * from './login'; | ||
export * from './kakaoGeocoder'; |
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,43 @@ | ||
import {customAxios} from './customAxios'; | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
|
||
const getAccessToken = async () => { | ||
const accesstoken = await AsyncStorage.getItem('accessToken'); | ||
return accesstoken; | ||
}; | ||
|
||
const getRefreshToken = async () => { | ||
const refreshtoken = await AsyncStorage.getItem('accessToken'); | ||
return refreshtoken; | ||
}; | ||
|
||
export const postToken = async () => { | ||
const accessToken = await getAccessToken(); | ||
const refreshToken = await getRefreshToken(); | ||
console.log('엑세스 토큰: ', accessToken); | ||
console.log('리프레시 토큰: ', refreshToken); | ||
try { | ||
const response = await customAxios().post('/auth/token', null, { | ||
params: {accessToken: accessToken, refreshToken: refreshToken}, | ||
}); | ||
console.log('리프레시 토큰 응답: ', response); | ||
await AsyncStorage.multiSet([ | ||
['accessToken', response.data.result.accessToken], | ||
['refreshToken', response.data.result.refreshToken], | ||
]); | ||
console.log('리프레시 토큰 성공: ', response); | ||
return response; | ||
} catch (error) { | ||
console.log('토큰 리프레시 갱신 에러: ', error); | ||
} | ||
}; | ||
|
||
//유저가 앱을 킬때 스플래시화면에서 token이 asyncStorage에 있다면 회원가입 완료 인지 아닌지 확인 해주는 api | ||
export const getRegisterStatus = async () => { | ||
try { | ||
const response = await customAxios().get('/api/v1/users/me/register-status'); | ||
return response.data.result.registerStatus; | ||
} catch (error) { | ||
console.log('register Status', error); | ||
} | ||
}; |
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,28 @@ | ||
import React, {FC} from 'react'; | ||
import {Image, Text, TouchableOpacity, View} from 'react-native'; | ||
import {DesignSystem} from '../assets/DesignSystem'; | ||
|
||
export type ConnectionErrorProps = { | ||
refetch: any; | ||
}; | ||
|
||
export const ConnectionError: FC<ConnectionErrorProps> = ({refetch}) => { | ||
return ( | ||
<View style={[DesignSystem.centerArrange, {flex: 1, marginBottom: 50}]}> | ||
<Image source={require('../assets/images/noMission/cryingBob.png')} /> | ||
<Text style={[DesignSystem.title1SB, {color: '#111111', marginBottom: 2}]}> | ||
연결에 실패했어요 | ||
</Text> | ||
<Text style={[DesignSystem.body1Lt, {color: '#94949', marginBottom: 38}]}> | ||
네트워크를 확인해주세요 | ||
</Text> | ||
<TouchableOpacity | ||
onPress={() => { | ||
refetch(); | ||
}} | ||
> | ||
<Text>새로고침</Text> | ||
</TouchableOpacity> | ||
</View> | ||
); | ||
}; |
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,20 @@ | ||
//홈 화면에 나타나는 미션 카드 정보 (완료 전과 후 포함) | ||
export type IHomeMission = { | ||
dayOfWeek: string; | ||
mission: string; | ||
missionId: number; | ||
missionStatus: string; | ||
point: number; | ||
storeCategory: string; | ||
storeId: number; | ||
storeName: string; | ||
successDate: string; | ||
}; | ||
|
||
//홈 화면을 채워줄 모든 데이터들 | ||
export type IHomeData = { | ||
dday: number; | ||
missions: IHomeMission[]; | ||
point: number; | ||
rewards: number; | ||
}; |
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,3 +1,4 @@ | ||
export * from './RegisterInterface'; | ||
export * from './createRegister'; | ||
export * from './MissionInterface'; | ||
export * from './Home'; |
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
Oops, something went wrong.