1

I have added Ionic Secure Storage plugin (to store authentication tokens) into my Ionic project, and it works properly locally when running cordova run browser (so that cordova is loaded as a plaform).

However, when I open my project in Ionic DevApp and Ionic View on Android (works correctly on iOS), it fails silently whenever I try to retrieve the saved token.

Here is my code:

// ... unrelated imports omitted
import {Platform} from "ionic-angular";
import {SecureStorage, SecureStorageObject} from "@ionic-native/secure-storage";

@Component({
    selector: 'my-component',
    templateUrl: 'my-component.html'
})
export class MyComponent {

    constructor(private platform: Platform,
                private secureStorage: SecureStorage) {
    }

    ngOnInit() {
        this.getToken().then(token => {
            // ... do something with the retrieved token
        });
    }

    getToken() {
        if (this.platform.is('cordova')) {
            /**
             * Code below silently fails in Ionic View and Ionic Dev App
             * on Android (works correctly on iOS)
             */
            return this.platform.ready().then(() =>
                return this.secureStorage.create('cp_secure_storage')
                    .then((storage: SecureStorageObject) => {
                        return storage.get('TOKEN_NAME')
                            .then(token => {
                                console.log(token);
                                return token;
                            }, () => null);
                    });
            });
        } else {
            return Promise.resolve(localStorage.getItem('TOKEN_NAME'));
        }
    }

}

I have Ionic error monitoring turned on and it catches no errors.

Plugin version:

"@ionic-native/secure-storage": "4.5.3",
"cordova-plugin-secure-storage": "^2.6.8"
mikhail-t
  • 4,103
  • 7
  • 36
  • 56
  • What's shown w/`console.log(this.platform.platforms())`? Is "cordova" one of them on the Android device where it's failing? Maybe add "android" (& "ios") explicitly? Does that `if` block get hit? Tried breakpoint? – mc01 Mar 08 '18 at 16:53
  • yep, `this.platform.platforms()` shows `cordova, mobile, android` in Ionic DevApp, and correct to actually get it "working" I had to limit the platform check to `this.platform.is('ios')`, so I am just using straight localStorage for `android` platform. – mikhail-t Mar 08 '18 at 17:00
  • Weird ... `if (this.platform.is('android') || this.platform.is('ios')) { //stuff }` ? – mc01 Mar 08 '18 at 17:05
  • yes, I had to use similar logic in the app to make sure Secure Storage is only used on ios, and if it is android I use just `localStorage`, which makes only ios secure. I have a suspicion some recent changes in Android Ionic View and DevApp broke the support for Secure Storage. – mikhail-t Mar 08 '18 at 17:10

0 Answers0