Created
April 19, 2019 15:58
-
-
Save cmilfont/09248075f7d0bb00e5a380d1021b65f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { take, put, call } from 'redux-saga/effects'; | |
import { eventChannel } from 'redux-saga'; | |
import { push } from 'react-router-redux'; | |
import actions from 'api/actions'; | |
import reducer from 'api/reducers/crud'; | |
import { loginMutation as mutation } from 'api/graphql/mutations'; | |
function createSocketChannel(firebase) { | |
return eventChannel((emit) => { | |
return firebase.auth() | |
.onAuthStateChanged(user => emit(user || {})); | |
}); | |
} | |
function* watchLogged(firebase, graphqlClient) { | |
const socketChannel = yield call(createSocketChannel, firebase); | |
while (true) { | |
const payload = yield take(socketChannel); | |
if (payload.email) { | |
const token = yield payload.getIdToken(); | |
const query = mutation({ token }); | |
yield call(graphqlClient.mutate, query); | |
yield put({ | |
type: actions.FETCH_ENTITY_SUCCESSFUL, | |
meta: { | |
entity: 'user', | |
reducer, | |
}, | |
payload, | |
}); | |
} else { | |
yield put(push('/login')); | |
} | |
} | |
} | |
export default watchLogged; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment