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

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

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

Comments

Projects
None yet
3 participants
@leebenson
Copy link

leebenson commented Nov 27, 2016

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Author

leebenson commented Nov 28, 2016

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

@ekryski

This comment has been minimized.

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. :shrug:

@leebenson

This comment has been minimized.

Copy link
Author

leebenson commented Nov 28, 2016

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

@daffl

This comment has been minimized.

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 Nov 30, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.