-
Notifications
You must be signed in to change notification settings - Fork 117
Client-side app.on('login') #355
Comments
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? |
Exactly, it'd be an event that fires only when |
@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 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/logoutIf 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 statusIf you are looking to know when every user comes online/offline (ie. a chat app) you would want to // 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. 🤷 |
@ekryski, thanks so much. Great advice. I didn't realise |
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. |
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.The text was updated successfully, but these errors were encountered: