Skip to content

Instantly share code, notes, and snippets.

@cmilfont
Created April 19, 2019 15:58
Show Gist options
  • Save cmilfont/09248075f7d0bb00e5a380d1021b65f1 to your computer and use it in GitHub Desktop.
Save cmilfont/09248075f7d0bb00e5a380d1021b65f1 to your computer and use it in GitHub Desktop.
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