From 3c7d774757c4db40c22414497d60c8bad2032a22 Mon Sep 17 00:00:00 2001 From: dannc Date: Sat, 23 Mar 2019 11:51:50 +0700 Subject: [PATCH] Improve check auth in middleware. Some fixes. --- src/components/AppHeader.vue | 5 ++--- src/main.js | 4 ---- src/router.js | 25 +++++++++++++++---------- src/store/user.js | 19 +++++++++++++++++++ 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/components/AppHeader.vue b/src/components/AppHeader.vue index 06aa393..9439231 100644 --- a/src/components/AppHeader.vue +++ b/src/components/AppHeader.vue @@ -55,7 +55,8 @@ export default { signOut() { this.$confirm('Вы точно хотите выйти?').then(res => { if (res) { - this.$store.dispatch('signOut'); + this.$store.dispatch('signOut') + .then(() => this.changeLocation({ name: 'home' })); } }); }, @@ -110,5 +111,3 @@ export default { }; - diff --git a/src/main.js b/src/main.js index aea06c0..aab8bdc 100644 --- a/src/main.js +++ b/src/main.js @@ -39,10 +39,6 @@ new Vue({ store, render: h => h(App), created: function onApplicationCreated() { - firebase.auth().onAuthStateChanged((user) => { - this.$store.dispatch('stateChanged', user); - }); - this.$store.dispatch('loadArticles'); }, }).$mount('#app'); diff --git a/src/router.js b/src/router.js index d3d910b..6280a6b 100644 --- a/src/router.js +++ b/src/router.js @@ -6,15 +6,7 @@ import Home from '@/views/Home'; Vue.use(Router); -function AuthMiddleware(from, to, next) { - if (Store.getters.isUserAuthenticated) { - next(); - } else { - next('/sign_in'); - } -} - -export default new Router({ +const router = new Router({ routes: [ { path: '/', @@ -47,7 +39,7 @@ export default new Router({ path: '/profile', name: 'profile', component: () => import(/* webpackChunkName: "words" */ '@/views/Profile'), - beforeEnter: AuthMiddleware, + meta: { authRequired: true }, }, { path: '/sign_in', @@ -66,3 +58,16 @@ export default new Router({ ], mode: 'history', }); + +router.beforeEach((to, from, next) => { + Store.dispatch('initAuth') + .then((user) => { + if (to.matched.some(route => route.meta.authRequired)) { + return user ? next() : next('/sign_in'); + } + + return next(); + }) +}) + +export default router; diff --git a/src/store/user.js b/src/store/user.js index a321777..1b182e3 100644 --- a/src/store/user.js +++ b/src/store/user.js @@ -11,6 +11,7 @@ export default { email: null, name: null, }, + unSubscribeAuth: null, }, mutations: { setUser(state, { uid, email }) { @@ -28,8 +29,26 @@ export default { setUserEmail(state, email) { Vue.set(state.user, 'email', email); }, + setUnSubscribeAuth(state, func) { + state.unSubscribeAuth = func; + }, }, actions: { + initAuth({ commit, dispatch, state }) { + return new Promise((resolve) => { + if (state.unSubscribeAuth) { + state.unSubscribeAuth(); + } + + const unSubscribe = firebase.auth() + .onAuthStateChanged((user) => { + dispatch('stateChanged', user); + resolve(user); + }); + + commit('setUnSubscribeAuth', unSubscribe); + }) + }, async signUp({ commit }, payload) { await commit('setProcessing', true); commit('clearError');