React/Flux way to handle permission sensitive actions with login flows

后端 未结 3 997
后悔当初
后悔当初 2021-02-08 20:24

I have been playing with React/Flux, and I am having trouble wrapping my head around the \'Flux Way\' for handling permission-sensitive actions.

Overarching question: W

3条回答
  •  粉色の甜心
    2021-02-08 21:01

    The simplest would be just asking SessionStore if there's a session.

     case CommentConstants.ADD_COMMENT:
        if (SessionStore.getUser()) {
           createComment(action.data);
        }
        break;
    

    Of course, this just saves you a server request. The behavior shouldn't be any different than always calling createComment(action.data).

    Is this a case where the payload should be passed into ensureCurrentUser, and then into the route handler for /login? What is the Flux way of handling these types of flows?

    For this, you might want to have an event emitter that emits a 'login' event.

     case CommentConstants.ADD_COMMENT:
        if (SessionStore.getUser()) {
           createComment(action.data);
        }
        else {
           someEventEmitter.one('login', function(){
               createComment(action.data);
           });
           someEventEmitter.emit('loginRequired');
        }
        break;
    

    And when loginRequired is emitted, if there isn't a user logged in, the login view would be presented.

提交回复
热议问题