Crypho / cordova-plugin-secure-storage

Secure storage plugin for Apache Cordova
MIT License
278 stars 269 forks source link

Save value of ss.get() in a variable ? #168

Closed frozensquid closed 5 years ago

frozensquid commented 5 years ago

Hello, I have an issue about the return of the value get by the function.

The plugin is initilized when deviceRead, this is ok 👍

ss = new cordova.plugins.SecureStorage(
  function() {
    console.log("Success");
  },
  function(error) {
    console.log("Error " + error);
  },
  "my_app"
);

I created a function to get a value

function getSecureStorage(key){
ss.get(
  function(value) {
   alert("Success, got " + value);
  return value;
  },
  function(error) {
    alert("Error " + error);
  },
  key
);  
}

I created another function to know if a "token" exist so i have:

function CheckVal() {
     var ssToken = getSecureStorage("token");
    alert("ss :" +ssToken);

}

When i build it and launch my app on my phone, When i had a value nammed token i have the alert that say OK. But when i trigger the CheckVal() value, the var ssToken is ALWAYS 'undefined'.. while i well returned the var value which contain the "token" value. I know that it's well inserted because the alert in getSecureStorage() function trigger with the value..

Can you explain me please? I am available if you have other questions.

Best regards, Florian

demetris-manikas commented 5 years ago

@frozensquid Your implementation of getSecureStorage is faulty. You probably need to read something like https://developers.google.com/web/fundamentals/primers/promises and then make getSecureStorage return a Promise. Closing.