Refresh Token

This commit is contained in:
yonghyon
2024-05-16 18:38:03 +09:00
parent 014bfca774
commit 1ddbfb272e
12 changed files with 96 additions and 150 deletions

View File

@@ -2,80 +2,17 @@ import axios from 'axios';
import * as HelperMethods from '../utils/helperMethods';
import {
getUrl,
URLS,
getUrl
} from './apiConfig';
import { types } from '../actions/actionTypes';
import { changeLocalSettings } from '../actions/commonActions';
import { getAuthenticationCode, getReAuthenticationCode } from '../actions/deviceActions';
// refresh-token 구현 필요
let tokenRefreshing = false;
export const tokenRefresh = (dispatch, getState, autoLogin, callback) => {
return new Promise((resolve, reject) => {
// console.log('getState ', getState());
// const {serverType} = getState().common.localSettings;
// const url = getUrl(URLS.TOKEN_REFRESH, serverType);
// const webOSVersionReal = getState().common.appStatus.webOSVersionReal;
// const {authenticationResult, tokenRequiredTime, userId} = getState().common.localSettings;
// const deviceId = getState().common.appStatus.deviceId;
// if(!userId){
// console.warn('tokenRefresh : not logged');
// if(callback){
// callback(false);
// }
// return;
// }
// const currentTime = new Date();
// const gap = (currentTime - tokenRequiredTime)/1000;
// const ExpiredIn = Number(authenticationResult.ExpiresIn);
// const limitTime = ExpiredIn - (ExpiredIn * THRESHOLD_AUTH_REFRESH);
// if(!autoLogin && (gap < limitTime || tokenRefreshing)){
// if(callback){
// callback(false);
// }
// return;
// }
// const tokenInfo =
// {
// "authenticationResult": authenticationResult,
// deviceInfo: {
// "deviceId": deviceId,
// "deviceName": "deviceName01",
// "os": "webos",
// "model": webOSVersionReal,
// "serialNumber": "xxxx-xxxx-xxxx-xxxx"
// }
// , "userId" : userId
// };
// if(useQAServerURL !== 'prd'){
// console.log('tokenInfo...', tokenInfo, gap, ExpiredIn);
// }
// try{
// tokenRefreshing = true;
// const response = await axios.put(url, tokenInfo, AUTHORIZATION);
// console.log(' tokenRefresh response....', response);
// Actions.AUTHORIZATION.headers.authorization = response.data.authenticationResult.AccessToken;
// dispatch(CommonActions.changeLocalSettings({authenticationResult:response.data.authenticationResult, tokenRequiredTime: new Date()}));
// console.log('토큰 갱신 완료...');
// if(callback){
// callback(true);
// }
// }catch(res) {
// const error = res && res.toJSON ? res.toJSON() : {};
// console.error('tokenRefresh', error, res);
// if(error.message ){
// if(error.message.indexOf('code 400')
// || error.message.indexOf('code 401')
// || error.message.indexOf('code 403')){
// dispatch(Actions.logout());
// }
// }
// if(callback){
// callback(false);
// }
// };
tokenRefreshing = false;
resolve();
});
export const setTokenRefreshing = (value) =>{
console.log('TAxios setTokenRefreshing ', value);
tokenRefreshing = value;
};
export const TAxios = (
@@ -93,12 +30,14 @@ export const TAxios = (
return new Promise((resolve) => {
const accessToken = getState().localSettings.accessToken;
if (
accessToken ||
baseUrl === URLS.GET_AUTHENTICATION_CODE ||
noTokenRefresh
noTokenRefresh ||
(!tokenRefreshing && accessToken)
) {
resolve(accessToken);
} else {
if(!accessToken && !tokenRefreshing){
dispatch(getAuthenticationCode());
}
HelperMethods.wait(100).then(() => {
resolve(checkAccessToken());
});
@@ -106,15 +45,17 @@ export const TAxios = (
});
};
const executeRequest = (accessToken, getState) => {
const executeRequest = (accessToken, dispatch, getState) => {
const httpHeader = getState().common.httpHeader;
const { mbr_no, deviceId } = getState().common.appStatus;
const refreshToken = getState().localSettings.refreshToken;
const AUTHORIZATION = { headers: { ...httpHeader } };
if (accessToken) {
AUTHORIZATION.headers["lgsp_auth"] = accessToken;
}
AUTHORIZATION.headers["dvc_id"] = deviceId;
AUTHORIZATION.headers["refresh-token"] = refreshToken;
if (typeof window === "object") {
let url = Array.isArray(baseUrl)
@@ -145,6 +86,28 @@ export const TAxios = (
axiosInstance
.then((res) => {
console.log("TAxios response", url, res);
if(res?.data?.retCode === 501){
//약관 비동의
dispatch(changeLocalSettings({accessToken: null}));
dispatch({type: types.GET_TERMS_AGREE_YN, payload: {}});
return;
}
//AccessToken 만료
if(res?.data?.retCode === 401){
dispatch(getReAuthenticationCode());
checkAccessToken().then((accessToken) => {
executeRequest(accessToken, dispatch, getState);
});
return;
}
//RefreshToken 만료
if(res?.data?.retCode === 402 ){
dispatch(getAuthenticationCode());
checkAccessToken().then((accessToken) => {
executeRequest(accessToken, dispatch, getState);
});
return;
}
if (onSuccess) onSuccess(res);
})
.catch((error) => {
@@ -156,15 +119,6 @@ export const TAxios = (
};
checkAccessToken().then((accessToken) => {
if (!noTokenRefresh) {
tokenRefresh(dispatch, getState)
.then(() => executeRequest(accessToken, getState))
.catch((e) => {
/* 토큰 갱신 실패 처리 */
console.error("tokenRefresh error", e);
});
} else {
executeRequest(accessToken, getState);
}
executeRequest(accessToken, dispatch, getState);
});
};