From 72a72f105a4e3c820e9b4b0d9d15e02d4abd1620 Mon Sep 17 00:00:00 2001 From: boat46450 Date: Wed, 21 Feb 2018 23:12:20 +0700 Subject: [PATCH] EDIT feature/login catch 401 but refresh token not complete --- utils/apiAuth.js | 7 +++++++ utils/apiProblem.js | 45 ++++++++++++++++++++++++++++++++++++----- utils/apiProblemType.js | 14 +++++++++++-- utils/apiProfile.js | 11 +++++++++- utils/apiUser.js | 36 ++++++++++++++++++++++++--------- utils/apiUserRole.js | 12 ++++++++++- 6 files changed, 107 insertions(+), 18 deletions(-) diff --git a/utils/apiAuth.js b/utils/apiAuth.js index 8cf8d78..b8fc0c0 100644 --- a/utils/apiAuth.js +++ b/utils/apiAuth.js @@ -12,6 +12,13 @@ export const auth = async (facebookId, facebookToken) => { await AsyncStorage.setItem('apiToken', `${data.accessToken}`) } +export const refresh = async () => { + console.log('start refresh api token') + let { data } = await api.post('/auth/refresh', null, {Authorization: `Bearer ${await getToken()}`}) + console.log('new api token : ', data) + await AsyncStorage.setItem('apiToken', `${data.accessToken}`) +} + export const getToken = async () => ( await AsyncStorage.getItem('apiToken') ) \ No newline at end of file diff --git a/utils/apiProblem.js b/utils/apiProblem.js index d6f2f11..bd81a40 100644 --- a/utils/apiProblem.js +++ b/utils/apiProblem.js @@ -1,22 +1,57 @@ import Api from './api' -import { getToken } from './apiAuth' +import { refresh ,getToken } from './apiAuth' export async function getAll() { - let api = await Api.get('/problems/', {Authorization: `Bearer ${await getToken()}`}) + let api + try { + api = await Api.get('/problems/', {Authorization: `Bearer ${await getToken()}`}) + } + catch (error) { + if(error.status == 401) { + await refresh() + api = await Api.get('/problems/', {Authorization: `Bearer ${await getToken()}`}) + } + } return api.data } export async function get(id) { - let api = await Api.get(`/problems/${id}`, {Authorization: `Bearer ${await getToken()}`}) + let api + try { + api = await Api.get(`/problems/${id}`, {Authorization: `Bearer ${await getToken()}`}) + } + catch (error) { + if(error.status == 401) { + await refresh() + api = await Api.get(`/problems/${id}`, {Authorization: `Bearer ${await getToken()}`}) + } + } return api.data } export async function post(data) { - let api = await Api.post('/problems/', data, {Authorization: `Bearer ${await getToken()}`}) + let api + try { + api = await Api.post('/problems/', data, {Authorization: `Bearer ${await getToken()}`}) + } + catch (error) { + if(error.status == 401) { + await refresh() + api = await Api.post('/problems/', data, {Authorization: `Bearer ${await getToken()}`}) + } + } return api.data } export async function put(id, data) { - let api = await Api.put(`/problems/${id}`, data, {Authorization: `Bearer ${await getToken()}`}) + let api + try { + api = await Api.put(`/problems/${id}`, data, {Authorization: `Bearer ${await getToken()}`}) + } catch (error) { + if(error.status == 401) { + await refresh() + api = await Api.put(`/problems/${id}`, data, {Authorization: `Bearer ${await getToken()}`}) + } + } return api.data } \ No newline at end of file diff --git a/utils/apiProblemType.js b/utils/apiProblemType.js index 378eee3..d8cfd91 100644 --- a/utils/apiProblemType.js +++ b/utils/apiProblemType.js @@ -1,7 +1,17 @@ import Api from './api' -import { getToken } from './apiAuth' +import { refresh, getToken } from './apiAuth' export async function getAll() { - let api = await Api.get('/problemtypes/', {Authorization: `Bearer ${await getToken()}`}) + let api + try { + api = await Api.get('/problemtypes/', {Authorization: `Bearer ${await getToken()}`}) + } + catch (error) { + if(error.status == 401) { + console.log('in error 401') + await refresh() + api = await Api.get('/problemtypes/', {Authorization: `Bearer ${await getToken()}`}) + } + } return api.data } \ No newline at end of file diff --git a/utils/apiProfile.js b/utils/apiProfile.js index 8ea8b93..beb5d6b 100644 --- a/utils/apiProfile.js +++ b/utils/apiProfile.js @@ -2,6 +2,15 @@ import Api from './api' import { getToken } from './apiAuth' export async function get(id) { - let api = await Api.get(`/profiles/${id}`, {Authorization: `Bearer ${await getToken()}`}) + let api + try { + api = await Api.get(`/profiles/${id}`, {Authorization: `Bearer ${await getToken()}`}) + } + catch (error) { + if(error.status == 401) { + await refresh() + api = await Api.get(`/profiles/${id}`, {Authorization: `Bearer ${await getToken()}`}) + } + } return api.data } \ No newline at end of file diff --git a/utils/apiUser.js b/utils/apiUser.js index c98b39b..18a6d46 100644 --- a/utils/apiUser.js +++ b/utils/apiUser.js @@ -2,16 +2,34 @@ import Api from './api' import { getToken } from './apiAuth' export async function get(fbid, fbtoken) { - let api = await Api.post( - `/users/${fbid}`, - { - id: fbid, - accessToken: fbtoken - }, - { - Authorization: `Bearer ${await getToken()}` + let api + try { + api = await Api.post( + `/users/${fbid}`, + { + id: fbid, + accessToken: fbtoken + }, + { + Authorization: `Bearer ${await getToken()}` + } + ) + } + catch (error) { + if(error.status == 401) { + await refresh() + api = await Api.post( + `/users/${fbid}`, + { + id: fbid, + accessToken: fbtoken + }, + { + Authorization: `Bearer ${await getToken()}` + } + ) } - ) + } let data = api.data.data return data } \ No newline at end of file diff --git a/utils/apiUserRole.js b/utils/apiUserRole.js index 53a8aa7..32bb936 100644 --- a/utils/apiUserRole.js +++ b/utils/apiUserRole.js @@ -2,6 +2,16 @@ import Api from './api' import { getToken } from './apiAuth' export async function getByUserId(userId) { - let api = await Api.get(`/userroles/user_id/${userId}`, {Authorization: `Bearer ${await getToken()}`}) + let api + try { + api = await Api.get(`/userroles/user_id/${userId}`, {Authorization: `Bearer ${await getToken()}`}) + } + catch (error) { + if(error.status == 401) { + console.log('in error 401') + await refresh() + api = await Api.get(`/userroles/user_id/${userId}`, {Authorization: `Bearer ${await getToken()}`}) + } + } return api.data } \ No newline at end of file