Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const FIREBASE = Symbol('FIREBASE');
- // Middleware constructor
- export default firebase => {
- // Init auth providers
- const providers = {
- fb: new firebase.auth.FacebookAuthProvider(),
- google: new firebase.auth.GoogleAuthProvider()
- };
- // This is the basic middleware pattern
- return ({ dispatch }) => next => action => {
- const firebaseAction = action[FIREBASE];
- // Let the action pass if it is not a 'firebase' action
- if (typeof firebaseAction === "undefined") {
- return next(action);
- }
- const { withFirebase, types, callBack } = firebaseAction;
- const [request, success, failure] = types;
- const promise = withFirebase(firebase, providers);
- dispatch({
- type: request,
- payload: {}
- });
- // This is an async immediately invoked function
- (async () => {
- const [error, response] = await handle(promise);
- if (error) {
- return dispatch({
- type: failure,
- payload: error
- })
- }
- if (response) {
- dispatch({
- type: success,
- payload: response
- });
- callBack && callBack();
- }
- })();
- };
- };
- // More on this explained below
- function handle(promise) {
- return promise.then(res => [null, res]).catch(err => [err, null]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement