Reputation: 909
i'm using jwt token
Assume that accesstoken and refreshtoken are already saved when logging in.
getPostAPI is an API that imports post data and refreshAPI is a reissue of acesstoken.
Before the accesstoken expires, if i make a getPostAPI request, it responds well, but when the accesstoken expires, a 403 error occurs. At this time, when err is detected in catch (err) of getPost function, refreshAPI is executed through REFRESH_REQUEST, and when accesstoken is issued, it is saved in AsyncStorage, and then GETPOST_REQUEST is executed to execute getPostAPI to get a good response.
But I'm not sure if the method I used is the right way.
am i doing right way?
this is my code
function getPostAPI(data) {
return axiosInstace.post('/kakao/getpost', data);
}
function* getPost(action) {
try {
const result = yield call(getPostAPI, action.data);
yield put({
type: GETPOST_SUCCESS,
data: result.data,
});
} catch (err) {
yield put({
type: REFRESH_REQUEST,
});
yield put({
type: GETPOST_REQUEST,
data: action.data,
});
}
}
function refreshAPI() {
return axiosInstace.post('/kakao/refresh');
}
function* refresh() {
try {
const result = yield call(refreshAPI);
AsyncStorage.setItem('accesstoken', `${result.data.accessToken}`, () => {});
yield put({
type: REFRESH_SUCCESS,
data: result.data,
});
} catch (err) {
yield put({
type: REFRESH_FAILURE,
error: err.response.data,
});
}
}
Upvotes: 0
Views: 1592
Reputation: 1047
function* getPost(action) {
try {
const result = yield call(getPostAPI, action.data);
yield put({
type: GETPOST_SUCCESS,
data: result.data,
});
} catch (err) {
yield put({
type: REFRESH_REQUEST,
});
yield put({
type: GETPOST_REQUEST,
data: action.data,
});
}
}
call refresh function instead of REFRESH_REQUEST
action
and call getPost function on REFRESH_SUCCESS
action
function* getPost(action) {
try {
const result = yield call(getPostAPI, action.data);
yield put({
type: GETPOST_SUCCESS,
data: result.data,
});
} catch (err) {
yield refresh();
yield put({
type: GETPOST_REQUEST,
data: action.data,
});
}
}
function* refresh() {
try {
const result = yield call(refreshAPI);
AsyncStorage.setItem('accesstoken', `${result.data.accessToken}`, () => {});
yield put({
type: REFRESH_SUCCESS,
data: result.data,
});
yield getPost(data);
} catch (err) {
yield put({
type: REFRESH_FAILURE,
error: err.response.data,
});
}
}
Upvotes: 1