Closed mjstahl closed 6 years ago
Thanks for kareniel
on IRC for the following snippet. I do think there could be a good way to work this into documentation. Maybe a tangent in the Forms
documentation?
const login = require('./login');
module.exports = function protect (view) {
return function (state, emit) {
if (!state.isLoggedIn) {
emit(state.events.REPLACESTATE, '/login')
return login(state, emit)
}
return view(state, emit)
}
}
app.route('/home', protect(require('./views/home')))
Expected behavior
I am brand new to Choo so I may be thinking about this all wrong. But the use case is, the User comes to a route that requires authentication. They are not authenticated so they are routed to a login view at a different route.
I have written up a small Choo to make it easier to demostrate the problem. The expected result from the code below is:
And the route should be:
package.json
index.js
Actual behavior
The actual result is:
And the route should be:
Steps to reproduce behavior
Write here.
npm i
./node_modules/.bin/bankai start index.js
https://localhost:8080/
Notes
What I noticed when tracing the function calls was that NAVIGATE event emitted and its callback executed.
In the
start
function (which was called afterdocumentReady
inmount
), the RENDER event was never emitted becauseself._loaded
was false (https://github.com/choojs/choo/blob/master/index.js#L101). After this code is executed, thedocumentReady
callback instart
is called, settingself._loaded
to true.So it appears that there is an order of operations issue maybe caused by the
setTimeout
in thedocumentReady
call.I would like to fix this problem (assuming its a problem), but I need a little bit of hand holding since I am only 1 day into Choo. Any guidance would be greatly appreciated.