From a1efc79329b5ce65c1d3db9352e05e1350562485 Mon Sep 17 00:00:00 2001 From: Kamil Klecha Date: Sat, 16 Oct 2021 22:18:26 +0200 Subject: [PATCH] Changed login from rest to firebase lib --- src/store/index.ts | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/store/index.ts b/src/store/index.ts index 1d435f7..9e2a6dc 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -1,6 +1,7 @@ import Vue from "vue"; import Vuex from "vuex"; -import axios from "axios"; + +import { getAuth, signInWithEmailAndPassword, signOut } from "firebase/auth"; Vue.use(Vuex); @@ -17,24 +18,31 @@ export default new Vuex.Store({ }, }, actions: { - loginUser({ commit }, auth) { - axios.post( - "https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=" + process.env.VUE_APP_FIREBASE_API_KEY, - { - email: auth.email, - password: auth.pass, - returnSecureToken: true, - } - ).then((res) => { - console.log(res); - commit('storeUser', res.data); - }).catch((e) => { - console.error(e); - }) + loginUser({ commit }, data) { + const auth = getAuth(); + + signInWithEmailAndPassword(auth, data.email, data.pass) + .then((userCredential) => { + // Signed in + commit("storeUser", userCredential.user); + // ... + }) + .catch((error) => { + const errorCode = error.code; + const errorMessage = error.message; + }); }, logout({ commit }) { - commit('clearUserData'); - } + const auth = getAuth(); + signOut(auth) + .then(() => { + // Sign-out successful. + }) + .catch((error) => { + // An error happened. + }); + commit("clearUserData"); + }, }, getters: { auth: (state) => state.user !== undefined,