Refactor session profiles' `:go-to` event handling
Opened this issue · 0 comments
As for now session profiles have a :go-to
event handler and :go-to*
event handler.
The former is responsible for making sure that the application has enough data to perform an authentication check if needed. As soon as that's true, a :go-to*
is dispatch.
That one was, up until now, responsible for doing authorization checks (e.g. landing only for non-authenticated users) and, if authorization checks has passed, letting through the actual navigation event.
So that alone would already cry for further division into two handlers - one for authorization and one for navigation. But naming event registrations were blockers here. I really wanted to outermost one to be called :go-to
as that's the intention to do.
Recently I realized that it's very much doable nicely with help of re-frame async flow. That :go-to
event could look somewhat like this (untested!):
(rf/reg-event-fx
:go-to
(fn [_ _]
{:async-flow
{:first-dispatch [::ensure-authentication]
:rules [{:when :seen?
:events ::ensure-authentication.success
:dispatch [::ensure-authorization]}
{:when :seen?
:events ::ensure-authorization.success
:dispatch [::go-to*]
:halt? true}
{:when :seen-any-of?
:events [::ensure-authentication.fail ::ensure-authorization.fail]
:dispatch [:go-to.fail]
:halt? true}]}}))