1

in react-native (implementing Jafar´s answer) I am checking the token-validity like this:

  // navigation
  const navigation = useNavigation()

  // config request: check token expiry-validity
  const checkTokenValidity = async (next) => {
    const token = state && state.token ? state.token : ''
    let err

    const decodedToken = jwt_decode(token)
    console.log('Decoded Token-----> ', decodedToken)
    let currentDate = new Date()

    if (decodedToken.exp * 1000 < currentDate.getTime()) {
      err.message = 'Token has expired.'
      await AsyncStorage.removeItem('auth-rn')
      setState({ user: {}, token: '' })
      next(err)
    } else {
      console.log('Valid token')
    }
  }

  // run checkTokenValidity
  checkTokenValidity((err) => {
    if (err) {
      console.log(`Token error: ${err.message}`)
      alert(
        'Your session is older than 7 days and it has expired. You´ll be redirected to log-in now.'
      )
      navigation.navigate('SignIn')
    }
    return
  }) 

It actually works all fine, but it throws this WARN in the console:

 WARN  Possible Unhandled Promise Rejection (id: 0):
[InvalidTokenError: Invalid token specified: undefined is not an object (evaluating 'e.replace')]

I tried some Promise Rejection options as "try and catch" and callbacks, but the warning persists.

I would appreciate if someone has an idea what is the code exactly asking for. Thx!

0 Answers0