All files / store/auth actions.js

0% Statements 0/43
0% Branches 0/2
0% Functions 0/15
0% Lines 0/43

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                                                                                                                                                                                                                                       
/*
export function someAction (context) {
}
*/
import firebase from "firebase/app";
import "firebase/auth";
 
export function signUpWithEmailAction({ commit }, payload) {
  commit("setStatus", "loading");
  firebase
    .auth()
    .createUserWithEmailAndPassword(payload.email, payload.password)
    .then(response => {
      commit("setUser", response.user);
      commit("setStatus", "success");
      commit("setError", null);
    })
    .catch(error => {
      commit("setStatus", "failure");
      commit("setError", error.message);
    });
}
 
export function signInWithEmailAction({ commit }, payload) {
  commit("setStatus", "loading");
  firebase
    .auth()
    .signInWithEmailAndPassword(payload.email, payload.password)
    .then(response => {
      console.log(response.user);
      commit("setUser", response.user);
      commit("setStatus", "success");
      commit("setError", null);
    })
    .catch(error => {
      commit("setStatus", "failure");
      commit("setError", error.message);
    });
}
 
export function signInWithGoogle({ commit }) {
  commit("setStatus", "loading");
  return new Promise((resolve, reject) => {
    var provider = new firebase.auth.GoogleAuthProvider();
    firebase
      .auth()
      .signInWithPopup(provider)
      .then(function(response) {
        // This gives you a Google Access Token. You can use it to access the Google API.
        var token = response.credential.accessToken;
        // The signed-in user info.
        var user = response.user;
        var userProfile = {
          name: user.displayName,
          email: user.email,
          photoURL: user.photoURL,
          uid: user.uid,
          token: token
        };
        commit("setUser", userProfile);
        commit("setStatus", "success");
        commit("setError", null);
        resolve(userProfile);
        // ...
      })
      .catch(function(error) {
        // Handle Errors here.
        //   var errorCode = error.code;
        //   var errorMessage = error.message;
        // The email of the user's account used.
        //   var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        //   var credential = error.credential;
        // ...
        commit("setStatus", "failure");
        commit("setError", error.message);
        reject(error.message);
      });
  });
}
 
export function signInFromSession({ commit, dispatch }, user) {
  if (!user) {
    return dispatch("signOut");
  }
 
  commit("setStatus", "loading");
  return new Promise((resolve, reject) => {
    commit("setUser", {
      name: user.displayName,
      email: user.email,
      photoURL: user.photoURL,
      uid: user.uid
    });
    commit("setStatus", "success");
    commit("setError", null);
    resolve(user);
  });
}
 
export function signOut({ commit }) {
  return firebase
    .auth()
    .signOut()
    .then(function() {
      commit("setUser", null);
      commit("setStatus", "success");
      commit("setError", null);
    })
    .catch(function(error) {
      // An error happened.
      commit("setStatus", "failure");
      commit("setError", error.message);
    });
}