Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I access to 'this.$session' on Vuex store? #14

Open
kotehector opened this issue Apr 17, 2018 · 8 comments
Open

Can I access to 'this.$session' on Vuex store? #14

kotehector opened this issue Apr 17, 2018 · 8 comments

Comments

@kotehector
Copy link

No description provided.

@mbana
Copy link

mbana commented May 23, 2018

Same question here...

@gamcoh
Copy link

gamcoh commented Jun 5, 2018

same question

@Pyronerd62
Copy link

Ok I figured it out.
Here we go.
In main.js

import VueSession from 'vue-session'
Vue.use(VueSession, {persist: true})

In Store.js or store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

let store =  new Vuex.Store({
    state: {
        isLoggedIn: false,
    },
    getters: {
        isLoggedIn:function(state){
            return state.isLoggedIn;
        },
        user:function(state){
            // console.log(store);
            return store._vm.$session.get('user');
        }
    },
    mutations: {
        saveUser(state,payload){
            console.log('user payload', payload);
            // this._vm.$session.start();
            store._vm.$session.set('user',payload);
            state.isLoggedIn = (payload != null)
        }
    },
    actions: {
        
    }
});

export default store

@mbana
Copy link

mbana commented Jul 4, 2018

The point is to avoid having to write such logic.
In reply to: #14 (comment).

@Pyronerd62
Copy link

I'm not sure what you're asking. That is your basic Vuex store setup. How else do you want to use the mutations and getters? This works for us as the session is tied to the Vue instance itself and it persists across tabs and components.

@shlomiLan
Copy link

I managed to access it with Vue.prototype.$session, not sure if this is the "right" way to do it.

@aswzen
Copy link

aswzen commented Jul 23, 2020

import Vue from 'vue';
import Vuex from 'vuex';
import VueSession from 'vue-session';

Vue.use(Vuex);
Vue.use(VueSession);

export const store = new Vuex.Store({
    state: {
       isLoggedInSession: !!Vue.prototype.$session.get('access-token')
    }
});

@abdulmalikgh
Copy link

You can only use this.$session.get() or any method on VueSessionStorage when you are in Vue components. But in other files use the following.
First make it global in main.js
by
import VueSessionStorage from 'vue-session'
Vue.use(VueSessionStorage)
then in your API or Vuex file

import Vue from 'vue'
import VueSessionStorage from 'vue-session'

Vue.prototype.$session.get("token")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants