Md. Parvez Alam
Md. Parvez Alam

Reputation: 4596

Remove item from async storage not working

I have below code in signout screen

 await AsyncStorage.removeItem('userToken').then(()=>{
      setTimeout(() => {
        this.props.navigation.navigate('SignIn');
      }, 3000);

On splash screen I am checking like this

const userToken = AsyncStorage.getItem('userToken').then(() => {
        this.props.navigation.navigate(userToken ? 'App' : 'SignIn');
    });

After signout Its redirect to sign-in, but when I open the app again splash screen moves to app rather than sign-in

My inital code was like

const userToken = AsyncStorage.getItem('userToken'), this.props.navigation.navigate(userToken ? 'App' : 'SignIn'); 

What I am doing wrong. Please advise.

Thanks

Upvotes: 2

Views: 2813

Answers (5)

Devansh sadhotra
Devansh sadhotra

Reputation: 1625

i have implemented the same functionality like this

async logout() {
    await AsyncStorage.clear();
    Actions.SignIn(); // for navigating back to sign in screen if using router flux
    this.props.navigation.navigate("SignIn"); // if using react-navigation
 }

Upvotes: 0

Rupesh Chaudhari
Rupesh Chaudhari

Reputation: 308

If someone is still confused or finding the solution here

Then here is the solution :

const userToken = await AsyncStorage.getItem('userToken').then(() => {
    this.props.navigation.navigate(userToken ? 'App' : 'SignIn');
});

Just add await when you are getting a value of a key from AsyncStorage

I also had this problem BTW.

Upvotes: 0

Imran
Imran

Reputation: 305

Please check the file AndroidManifest.xml and change android:allwoBackup=ture to android:allwoBackup=false. Hope this will work. When backup is true it always backup login information.

Upvotes: 0

Jebin Benny
Jebin Benny

Reputation: 951

Did you check the Async data result?

eg:

On splash screen

AsyncStorage.getItem("userToken").then(value => {
      if(value) {
       let token = JSON.parse(value);
       this.props.navigation.navigate(token ? 'App' : 'SignIn');
      } else {
       this.props.navigation.navigate('SignIn');
      }
    });

Upvotes: 2

Jaydeep Galani
Jaydeep Galani

Reputation: 4961

You forget the param of callback,

const userToken = AsyncStorage.getItem('userToken').then((userToken) => {
        this.props.navigation.navigate(userToken ? 'App' : 'SignIn');
    });

Upvotes: 1

Related Questions