Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
React Native - Direct user back to login if current user token is invalid?

I have this code that authenticates the user session by taking their token and sending it to AWS-amplify to ensure their token hasn't timed out and it's a valid user

import {Auth} from 'aws-amplify';

export async function getAccessToken() {
    try {
        // const currentUser = await Auth.currentAuthenticatedUser();
        // console.log(currentUser);

        return await Auth
            .currentSession()
            .then(res => {
                let accessToken = res.getAccessToken();
                let jwt = accessToken.getJwtToken();
                // You can print them to see the full objects
                // console.log(`myAccessToken: ${JSON.stringify(accessToken)}`);
                // console.log(`myJwt: ${JSON.stringify(jwt)}`);
                return jwt
            });
    } catch (error) {
        console.log(error);
    }
}

This gets called when make ANY endpoint call, because I want to ensure the user has been validated by aws-amplify. So for example I might have some code that does something like

  function getSomething() {
    getAccessToken().then(token => {
      const config = {
        headers: {
          Authorization: `Bearer ${token}`
        }
      };


      axios
        .get(<url>, config)
        .then(response => {
          console.log(response.data)
          setData(response.data);
        })
        .catch(error => {
          console.log(JSON.stringify(error));
        });
    }).catch(error => {
      console.log(JSON.stringify(error));
    });;
  };

So every endpoint call of my app requires the access token and if it's not authenticated properly the backend will shoot back a 401. If 401 is received I want to automatically bounce users back to login for any and all screens (except login of course)

for navigation I'm using

const NavigatorTab = ({navigation}) => {

and just setting the navigation parameter.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!