Skip to content

Commit

Permalink
EDIT feature/login catch 401 but refresh token not complete
Browse files Browse the repository at this point in the history
  • Loading branch information
shuikang committed Feb 21, 2018
1 parent 02fa97e commit 72a72f1
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 18 deletions.
7 changes: 7 additions & 0 deletions utils/apiAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
45 changes: 40 additions & 5 deletions utils/apiProblem.js
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 12 additions & 2 deletions utils/apiProblemType.js
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 10 additions & 1 deletion utils/apiProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
36 changes: 27 additions & 9 deletions utils/apiUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion utils/apiUserRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 72a72f1

Please sign in to comment.