在redux中验证我的用户后,我应该如何验证我的套接字?

时间:2017-05-27 23:05:44

标签: javascript reactjs authentication socket.io redux

onSubmit> userAction.login>发送“SUCCESS_AUTHENTICATE”>在reducers中设置用户和套接字状态的标记> ?????那我怎么能让socket.emit(“authenticate”,{token})发生呢?

1 个答案:

答案 0 :(得分:0)

简单地编写一个简短的中间件,工作得很好。

const socketAuthenticator = (store) => (next) => (action) =>{
    if(action.type === "AUTHENTICATION_FULFILLED" || action.type === "REGISTRATION_FULFILLED") {
        const token = action.payload.token;
        const socket = store.getState().socket.socket
        socket.emit('authenticate', { token })
    }
    next(action)
}