Files
weather-demo/src/store/index.ts
2021-10-20 11:14:58 +02:00

59 lines
1.3 KiB
TypeScript

import Vue from "vue";
import Vuex from "vuex";
import snackbars from "./modules/snackbars";
import { getAuth, signInWithEmailAndPassword, signOut } from "firebase/auth";
Vue.use(Vuex);
export default new Vuex.Store({
state: {
user: undefined,
},
mutations: {
storeUser(state, payload) {
state.user = payload;
},
clearUserData(state) {
state.user = undefined;
},
},
actions: {
loginUser({ commit, dispatch }, data) {
const auth = getAuth();
signInWithEmailAndPassword(auth, data.email, data.pass)
.then((userCredential) => {
// Signed in
commit("storeUser", userCredential.user);
dispatch("infoMessage", "Zalogowano");
// ...
})
.catch((error) => {
dispatch("errorMessage", "Błąd podczas logowania!");
// const errorCode = error.code;
// const errorMessage = error.message;
});
},
logout({ commit, dispatch }) {
const auth = getAuth();
signOut(auth)
.then(() => {
dispatch("infoMessage", "Wylogowano");
})
.catch((error) => {
// An error happened.
});
commit("clearUserData");
},
},
getters: {
auth: (state) => state.user !== undefined,
user: (state) => state.user,
},
modules: {
snackbars,
},
});