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

How to check if session exists globally #1

Closed
peoray opened this issue Feb 6, 2019 · 3 comments
Closed

How to check if session exists globally #1

peoray opened this issue Feb 6, 2019 · 3 comments
Assignees
Labels
good first issue Good for newcomers

Comments

@peoray
Copy link

peoray commented Feb 6, 2019

In the documentation, to check if a session exists on a component, we need to do:

 beforeCreate: function () {
    if (!this.$session.exists()) {
      this.$router.push('/')
    }

This will check if a session exists, else redirect back to homepage.

I have many components, and doing this on each of them feels like WET code, so I decided to do it on the router.js file

import VueEasySession from 'vue-easysession'
var session = VueEasySession.getInstance()
if(!session.exists()) {
   next('/')
     } else {
      next();
    }
}

It isn't working and I'm getting an error:
[vue-router] uncaught error during route navigation:
Invalid Object with a dropdown of many files

@ioadres
Copy link
Owner

ioadres commented Feb 12, 2019

you can use the expireSession for redirect to Login page.

this.$session.start(exp : milliseconds), the parameter exp is optional and indicate the time of expiration the session(millisecond)

var options = {
    persist: true,
    keySession: 'myKeySession',
    expireSessionCallback: function () {
        window.location.href = '/'
    }
}

expireSessionCallback: function () {
window.location.href = '/'
}

If this does not work, I will try to find a solution for you problem because as you said : "doing this on each component feels like WET" is true!

@ioadres ioadres added the good first issue Good for newcomers label Feb 12, 2019
@ioadres ioadres self-assigned this Feb 12, 2019
@peoray
Copy link
Author

peoray commented Feb 14, 2019

Thanks, just seeing this now, I'll try the code above and tell you how it went

@peoray
Copy link
Author

peoray commented Mar 4, 2019

Tried this and got confused too. Not sure you understand the issue or maybe I didn't explain well. I'll like to further discuss this to have a solution.

Basically, all I'm saying is how to be able to use the code that checks if a session exists for each component and put it inside router.js so it can be used with a beforeRoute method so we don't have to keep writing the same code for every component.
I hope this is clear this time around? 🙂 Also, if there is any way I can contribute, I'm not good at it but willing to learn

@ioadres ioadres closed this as completed Jun 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants