Storage doesn't clear, there seems to be multiple instances


#1

I use ionic/storage in two different views. In my login page I set the user token and I clear it in the profile page when the user logs out. The problem is, when the user logs out, the storage does not get cleared in the login page. I know this because when I go back to the login page, I can directly log back in using the token in storage (which was supposed to be removed). However, in the profile page it actually clears the storage. When I call storage.clear() from the login page, then it clears properly.

I think I somehow made two instances of storage… any help would be much appreciated!

Here is how I import & use storage:

//app.module.ts

import { Storage } from ‘@ionic/storage’;

let storage = new Storage();

providers: [
Storage,
// config values for AuthHttp
provideAuth({

  tokenGetter: (() => storage.get('id_token')),
            ...

}) ]

//login.ts


import { Storage } from '@ionic/storage';
... 
   constructor(... public storage: Storage...) {
            ...
		this.storage = storage;
	}

loginSuccessful(){
     ...
    this.storage.set('id_token', uData.token);
}
...

.

//profile.ts

import { Storage } from '@ionic/storage';

...
constructor(... storage: Storage...) {
                ...
		this.storage = storage;
	}


logout() {		
		console.log('log out was called!');
		var self = this;
		
		this.authHttp.get('http://localhost:8080/api/logout')
		.subscribe(
			data => {
				console.log(self.storage.get('id_token')); // this print shows the existing token
				self.storage.clear();
				console.log(self.storage.get('id_token')); // this print shows the token is removed
				self.app.getRootNav().setRoot(LoginPage, {});
			},
			error => {
				console.log(error._body);
				self.storage.clear();
			}
			);	
	}

#2

Nevermind, I was logging the user in automatically in the login page, that’s why when I logged out it was logging the user in but staying in the same page :slight_smile:


#3

Regardless of anything else, you don’t need to be doing this:


#4

Thank you! But then how do I set the tokenGetter for the authHttp? is that redundant as well?

tokenGetter: (() => storage.get('id_token')),