Simpleddp - How to keep user logged in?

Hey guys,

I am using @gregivy 's awesome simpleddp package and its simpleddp-plugin-login to connect a Vue app to a Meteor server.

However, I realized that after a user logs in, if I refresh the page or switch to a different route, server.userId is undefined and apparently the login is lost.

I’ve read a few other people complaining about this and it looks like the solution involves “saving the login token” somewhere in the Vue app. Now I’ll be honest here: I have no idea what this is supposed to mean, how to do it and how to handle logins this way.

Could anybody help me out?

Thanks!

Meteor.loginToken is held in local storage. You can inspect using Chrome dev tools via the “Application” tab.

1 Like

Hello @patrickcneuhaus!

Try this:

server.on('login', (m) => {
  localStorage.set('auth_token', server.token);
});

Then when the page is ready you should check if there is an auth_token:

let auth_token = localStorage.get('auth_token');
if (auth_token) {
  // you have the token
  try {
    await server.login({resume:auth_token});
  } catch(e) {
   //something went wrong, maybe the token is too old or not valid
  }
} else {
  // you don't have the token, user should authorize
}
2 Likes

Hey,

Thanks for the reply! One question though: Is this going to work on mobile?

Do you mean cordova app? It will work anywhere, if the env has something like localStorage.

Great! It works here.

The only problem is that I keep getting “logged out” after a minute… can I change the default token time?

I am not sure this happens due to token time. By default it is much more than a minute (I’m not sure about the exact time). Login plugin resumes authorization automatically in case of connection drops etc, so it is probably not a case for the problem.

Can you provide a reproduction code for this behavior?

Hey,

I managed to get this issue solved, but… am having trouble with Accounts-related functions.
Login works fine, but createUser doesn’t. Any idea why?

SignupPage.vue

import { server } from "../main.js";
server
  .createUser({
          email: email,
          password: password,
          profile: {
            customerId: customerId
          }
        })
        .then(() => {
          //
        })

There is no such function as createUser(). The easiest way is to create a special method on the server side for user creation.

Hey @gregivy, how are you?

Sorry to bother you once again with questions… but I recently got a call and my customer is complaining that every time the app is closed, the user is logged out.

I implemented the code you posted above to get the auth_token, but it’s not working…

Maybe it has something to do with localStorage…? Do I need to request permission to use the device’s localStorage somewhere? Or create a configuration file for it? Or maybe the localStorage is emptied every time the app is closed, thus making this approach fail?

If you have any ideas on how to solve this, please let me know! :slight_smile:

Hello @patrickcneuhaus!
You don’t need permissions for localStorage, thats for sure. It just works and it does not become empty after the app is closed.