forked from trent_larson/crowd-funder-for-time-pwa
Added Pinia account state tracking. Also modified router to go to Start if no account is defined.
This commit is contained in:
@@ -3,8 +3,7 @@ import { createApp } from "vue";
|
|||||||
import App from "./App.vue";
|
import App from "./App.vue";
|
||||||
import "./registerServiceWorker";
|
import "./registerServiceWorker";
|
||||||
import router from "./router";
|
import router from "./router";
|
||||||
import store from "./store";
|
import { useAccountStore } from "./store/account";
|
||||||
|
|
||||||
import "./assets/styles/tailwind.css";
|
import "./assets/styles/tailwind.css";
|
||||||
|
|
||||||
import { library } from "@fortawesome/fontawesome-svg-core";
|
import { library } from "@fortawesome/fontawesome-svg-core";
|
||||||
@@ -50,7 +49,6 @@ import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
|
|||||||
|
|
||||||
createApp(App)
|
createApp(App)
|
||||||
.component("fa", FontAwesomeIcon)
|
.component("fa", FontAwesomeIcon)
|
||||||
.use(store)
|
|
||||||
.use(createPinia())
|
.use(createPinia())
|
||||||
.use(router)
|
.use(router)
|
||||||
.mount("#app");
|
.mount("#app");
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
|
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
|
||||||
|
import { useAccountStore } from "../store/account";
|
||||||
import HomeView from "../views/HomeView.vue";
|
import HomeView from "../views/HomeView.vue";
|
||||||
|
|
||||||
const routes: Array<RouteRecordRaw> = [
|
const routes: Array<RouteRecordRaw> = [
|
||||||
@@ -109,4 +110,14 @@ const router = createRouter({
|
|||||||
routes,
|
routes,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.beforeEach(async (to) => {
|
||||||
|
// redirect to start page if no account
|
||||||
|
const publicPages = ["/start"];
|
||||||
|
const authRequired = !publicPages.includes(to.path);
|
||||||
|
const authStore = useAccountStore();
|
||||||
|
|
||||||
|
if (authRequired && !authStore.account) {
|
||||||
|
return "/start";
|
||||||
|
}
|
||||||
|
});
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
@@ -4,16 +4,19 @@ import { defineStore } from "pinia";
|
|||||||
export const useAccountStore = defineStore({
|
export const useAccountStore = defineStore({
|
||||||
id: "account",
|
id: "account",
|
||||||
state: () => ({
|
state: () => ({
|
||||||
firstName: "",
|
account: JSON.parse(
|
||||||
lastName: "",
|
typeof localStorage["account"] == "undefined"
|
||||||
|
? null
|
||||||
|
: localStorage["account"]
|
||||||
|
),
|
||||||
}),
|
}),
|
||||||
|
getters: {
|
||||||
|
firstName: (state) => state.account.firstName,
|
||||||
|
lastName: (state) => state.account.lastName,
|
||||||
|
},
|
||||||
actions: {
|
actions: {
|
||||||
reset() {
|
reset() {
|
||||||
this.$patch({
|
localStorage.removeItem("account");
|
||||||
firstName: "",
|
|
||||||
lastName: "",
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
import { createStore } from "vuex";
|
|
||||||
|
|
||||||
export default createStore({
|
|
||||||
state: {},
|
|
||||||
getters: {},
|
|
||||||
mutations: {},
|
|
||||||
actions: {},
|
|
||||||
modules: {},
|
|
||||||
});
|
|
||||||
Reference in New Issue
Block a user