Skip to content

Commit

Permalink
Test token validity with medAl-Data instead of expiration date
Browse files Browse the repository at this point in the history
  • Loading branch information
quentingirard committed Jul 8, 2022
1 parent 33fbc67 commit 640e16e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Containers/Auth/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const LoginAuthContainer = () => {
__DEV__ ? 'http://195.15.219.241:8082' : '',
)

const [clientId, setClientId] = useState(__DEV__ ? '1' : '')
const [clientId, setClientId] = useState(__DEV__ ? '38' : '')

// Define references
const fadeAnim = useRef(new Animated.Value(0)).current
Expand Down
23 changes: 15 additions & 8 deletions src/Services/Auth/RefreshToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import * as Keychain from 'react-native-keychain'
import { refresh } from 'react-native-app-auth'
import axios from 'axios'

/**
* The internal imports
Expand All @@ -13,22 +14,28 @@ import { store } from '@/Store'

export default async (forceRefresh = false) => {
const bearToken = await Keychain.getInternetCredentials('accessToken')
const state = store.getState()

// Force get new token
if (forceRefresh) {
return getRefreshToken()
}

const accessTokenExpirationDate = await Keychain.getInternetCredentials(
'accessTokenExpirationDate',
)
let tokenValidity = false

// Request data to know token status (valid or expired)
await axios
.get(`${state.auth.medAlDataURL}/api/v1/tokeninfo`, {
headers: {
Authorization: `Bearer ${bearToken.password}`,
Accept: 'application/json',
},
})
.then(_tokenInfoResponse => (tokenValidity = true))
.catch(_error => (tokenValidity = false))

// Check token validity before getting new one
if (
accessTokenExpirationDate.password &&
new Date(accessTokenExpirationDate.password).getTime() >
new Date().getTime()
) {
if (tokenValidity) {
return `Bearer ${bearToken.password}`
} else {
return getRefreshToken()
Expand Down

0 comments on commit 640e16e

Please sign in to comment.