Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 48984

Ionic Secure Storage get() at the start of the app

$
0
0

I have a service that gets a token for validate an Auth status at the /src/main.ts:

if (TokenService.getAccessToken() !== undefined) {
...
}

token.service.ts

import storage from '@/plugins/storage'

const ACCESS_TOKEN_KEY = "access_token";

const TokenService = {
    getAccessToken(){
        storage.get(ACCESS_TOKEN_KEY).then(v => {
            return v
        })
    },
    async saveAccessToken(accessToken: string) {
        storage.set(ACCESS_TOKEN_KEY, accessToken);
    },
}

export { TokenService };

storage.ts:

import { Storage, Drivers } from '@ionic/storage'

const localStorage = new Storage()
localStorage.create()

const storage = {
    set: async(i: any, v: any) => {
        await localStorage.set(i, v)
    },
    get(i: any){
        return localStorage.get(i)
    },
    remove: async (i: any) => {
        await localStorage.remove(i);
    },
    clear: async () => {
        await localStorage.clear();
    }
}

export default storage

i only get undefined but when using console.log() inside the storage.ts i get the value but after the app started.

i also use this function to make a condition in /router/index.ts
when i was using localStorage i was getting the values well.

my dependencies:

    "@ionic-native/secure-storage": "^5.31.1",
    "@ionic/storage": "^3.0.4",
    "@ionic/vue": "^5.6.0-dev.202102221703.5300dcc",
    "@ionic/vue-router": "^5.6.0-dev.202102221703.5300dcc",
    "vue": "^3.0.0-0",
    "vue-router": "^4.0.0-0",
    "vuex": "^4.0.0-rc.2",

What can i do?
thanks.!

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 48984

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>