Skip to content
This repository was archived by the owner on Mar 22, 2022. It is now read-only.

Client-side app.on('login') #355

Closed
leebenson opened this issue Nov 27, 2016 · 5 comments
Closed

Client-side app.on('login') #355

leebenson opened this issue Nov 27, 2016 · 5 comments

Comments

@leebenson
Copy link

Viewing the 1.0 readme, I see app.on('login'|'logout') is coming to the server. Awesome!

Are there any plans to release this client-side?

I use mobx on the client, and subscribe to events to hydrate stores with data from the server. It'd be useful to continue with this pattern client-side authentication too. Currently, I am doing an auth check on initial load (type: token) and another explicitly if/when the user logs in (type: local), and chaining the promise returned by .authenticate to stuff that data into my stores.

If I had one global app.on('login'), instead, I could fire-and-forget .authenticate (Flux-style) and avoid setting up handlers for each possible auth strategy.

@daffl
Copy link
Member

daffl commented Nov 28, 2016

I would assume that event would be only for the user of the app right (not everybody like on the server)? I think that makes sense, @ekryski any thoughts?

@leebenson
Copy link
Author

Exactly, it'd be an event that fires only when app.authenticate finishes locally, on the client.

@ekryski
Copy link
Member

ekryski commented Nov 28, 2016

@leebenson: @corymsmith and I just chatted this over. We're also using MobX. I think what you are looking for can already be done. The current logout and login events are server side only because they get fired on every user login/logout, we can't expose the same info directly to the client.

If you want to listen for those updates on the client there are 2 ways to do it depending on what you are looking for:

Listening for your own login/logout

If you only care about your own login and logout events on the client and simply want your store to be able to listen for those events to decouple things you can easily just do this:

// client
app.authenticate().then(response => app.emit('login', response));

// server
app.on('login', data => {
   // update your store
});

Listening for everyone's status

If you are looking to know when every user comes online/offline (ie. a chat app) you would want to patch the user server side on login and logout and then listen for patched events on your client and update your store. You'd want to also filter the patched events to ensure you are not leaking sensitive info.

// client
app.service('users').on('patched', user => {
  // update your store or do whatever
  if (user.online) {
    // do whatever
  }
});

// server
app.on('login', (tokens, { connection }) => {
   app.service('users').patch(connection.user._id, { online: true });
});

Now, we could do the first option automatically in the client but it's negligible code to implement yourself. 🤷

@leebenson
Copy link
Author

@ekryski, thanks so much. Great advice. I didn't realise app was an event emitter. That helps a lot 👍

@daffl
Copy link
Member

daffl commented Nov 30, 2016

Indeed what @ekryski said. I forgot about that too. I think we can close this then since it's only really two lines of code to implement.

@daffl daffl closed this as completed Nov 30, 2016
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants