All files / store/auth actions.js

0% Statements 0/53
0% Branches 0/12
0% Functions 0/15
0% Lines 0/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143                                                                                                                                                                                                                                                                                             
/*
export function someAction (context) {
}
*/
import firebase from "firebase/app";
import "firebase/auth";
 
export function signInWithGoogle({ commit, dispatch }) {
  var google = new firebase.auth.GoogleAuthProvider();
  google.setCustomParameters({
    // Forces account selection even when one account
    // is available.
    prompt: "select_account"
  });
  google.addScope("profile");
  google.addScope("email");
  commit("updateStatus", "loading");
  firebase
    .auth()
    .signInWithRedirect(google)
    .then(function(result) {
      var token = result.credential.accessToken;
      var user = result.user;
      commit("updateUser", user);
      commit("updateError", null);
      commit("updateToken", token);
      commit("updateStatus", "success");
      dispatch("loadUserProfile", user);
    })
    .catch(function(error) {
      commit("updateError", error.message);
      commit("updateStatus", "error");
    });
}
 
export function signInAnonymously({ commit, dispatch, state }, nickname) {
  commit("updateNickname", nickname);
  firebase
    .auth()
    .signInAnonymously()
    .then(function(result) {
      // var token = result.credential.accessToken;
      var user = result.user;
      commit("updateUser", { user, displayName: nickname });
      commit("updateError", null);
      dispatch("loadUserProfile", user);
 
      // dispatch("loadAnonymousProfile", { user, nickname });
    })
    .catch(function(error) {
      commit("updateError", error.message);
      commit("updateStatus", "error");
    });
}
 
export function loadUserProfile({ commit, dispatch, state }, payload) {
  commit("updateStatus", "loading");
  const db = firebase.firestore();
  const userRef = db.collection("users").doc(payload.uid);
  userRef
    .get()
    .then(function(doc) {
      const _setRef = () => {
        dispatch(
          "user/setUserRef",
          {
            ref: userRef
          },
          {
            root: true
          }
        )
          .then(function() {
            commit("updateStatus", "success");
          })
          .catch(function(error) {
            commit("updateStatus", "error");
            commit("updateError", error.message);
          });
      };
 
      if (doc.exists) {
        // console.log('Load user profile')
        // console.log(doc)
        commit("updateUserProfile", doc.data());
        commit("updateStatus", "success");
        return _setRef();
      } else {
        // console.log('Create user profile')
        const profile = {
          displayName: state.user.displayName || state.nickname,
          email: state.user.email,
          uid: state.user.uid,
          photoURL: state.user.photoURL,
          location: "",
          bio: "",
          website: "",
          anonymous: state.user.isAnonymous,
          send_stats: true,
          receive_notifications: false
        };
        userRef
          .set(profile, {
            merge: true
          })
          .then(function() {
            commit("updateUserProfile", profile);
            commit("updateStatus", "success");
            return _setRef();
          });
      }
    })
    .catch(function(error) {
      // console.log("Some error occurred in user profile");
      commit("updateStatus", "error");
      commit("updateError", error.message);
 
      // console.log(error);
    });
}
 
export function sessionLogin({ commit, dispatch, state }, user) {
  commit("updateError", null);
  commit("updateStatus", "success");
  if (user.anonymous) {
    commit("updateUser", {
      displayName: state.nickname || "Anonymous",
      ...user
    });
  } else {
    commit("updateUser", user);
  }
 
  if (!state.userProfile || user.uid != state.userProfile.uid)
    dispatch("loadUserProfile", user);
}
 
export function resetUser({ commit, dispatch }) {
  commit("updateStatus", "success");
  commit("updateUser", null);
  commit("updateUserProfile", null);
}