timwis / enviar

Chat interface for SMS / text messages
37 stars 6 forks source link

Where should login status be checked at init? #65

Closed timwis closed 7 years ago

timwis commented 8 years ago

Currently, layout.js has an onload callback which triggers the app model's initialize effect. The initialize effect calls pouchdb's getSession(), which queries couchdb's _session endpoint to validate the cookie. If the user is not logged in, the app model's initialize effect does a hard redirect to /login. If the user is logged in, it sets the user data via the user model's set reducer, then triggers the convos model's fetch effect to get all the messages as a logged in user.

This works pretty well, but it doesn't feel right to have this done in the onload callback inside a big effect.

A more traditional approach would have some sort of validation happening in the router, but I can't quite get a good workflow going. The only thing I can think of that would truly work is to do the checking prior to app.start(). If the user's not logged in, redirect them to /login, otherwise pass their initial state (we'd have to wrap the model in a function to accept initial state). But having a server side call before render seems bad.

timwis commented 7 years ago

Moved getSession() to index.js, outside of choo. Works pretty well, though doesn't feel very chooy.