latata
latata

Reputation: 1723

How to keep a user logged in until browser is closed in Meteor?

I use Meteor default account-password package. The user login data (loginToken, userId and loginTokenExpires) is stored in localStorage, so when I close a browser the user is still logged in. How to prevent it? Is it possible to store the user data in sessionStorage?

Upvotes: 2

Views: 1575

Answers (2)

Zagi
Zagi

Reputation: 11

I posted this solution in another topic: https://stackoverflow.com/a/64464638/11220998

  if (sessionStorage.getItem('session') === null) {
    Meteor.logout();
  }
  window.onload=function(){
    sessionStorage.setItem('session','on');
  };

User will be logged out if it's a new session and not on reload of the page.

Upvotes: 0

Mikael Lirbank
Mikael Lirbank

Reputation: 4615

Try this:

Accounts.config({loginExpirationInDays: 0});

The docs http://docs.meteor.com/#/full/accounts_config say (I have not tried it myself but sounds like it should work):

The number of days from when a user logs in until their token expires and they are logged out. Defaults to 90. Set to null to disable login expiration.

UPDATED ANSWER: You can clean up the localstorage when the browser is about to close, like this:

Template.body.rendered = function () {
  $(window).on('beforeunload', function () {
    // You can either remove the login tokens manually from localstorage like this:
    // localStorage.removeItem(key);

    // Alternatively you should be able to log out Meteor here (not tested), eg:
    // Meteor.logout();
  });
};

Upvotes: 3

Related Questions