Skip to content

Instantly share code, notes, and snippets.

@timwis
Created September 4, 2016 00:35
Show Gist options
  • Save timwis/ab24400592513fa2728cc0bd9b431a44 to your computer and use it in GitHub Desktop.
Save timwis/ab24400592513fa2728cc0bd9b431a44 to your computer and use it in GitHub Desktop.
app.router((route) => [
route('/', Layout()),
route('/:phone', requireLogin(Layout(Chat))),
route('/login', Layout(Login))
])
function requireLogin (View) {
return function (state, prev, send) {
if (!state.user.name) send('user:requireLogin')
return View(state, prev, send)
}
}
{
effects: {
requireLogin: (data, state, send, done) => {
db.getSession((err, body) => {
if (err) {
// Error with request
return done(new Error('Error getting current session'))
} else if (!body.userCtx.name) {
// Not logged in
send('redirect', '/login', done)
} else {
// Logged in
send('user:set', body.userCtx, done)
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment