mirror of
https://github.com/Dannecron/ich-lerne-deutsch.git
synced 2025-12-25 12:52:35 +03:00
Improve check auth in middleware. Some fixes.
This commit is contained in:
@@ -55,7 +55,8 @@ export default {
|
|||||||
signOut() {
|
signOut() {
|
||||||
this.$confirm('Вы точно хотите выйти?').then(res => {
|
this.$confirm('Вы точно хотите выйти?').then(res => {
|
||||||
if (res) {
|
if (res) {
|
||||||
this.$store.dispatch('signOut');
|
this.$store.dispatch('signOut')
|
||||||
|
.then(() => this.changeLocation({ name: 'home' }));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -110,5 +111,3 @@ export default {
|
|||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
</style>
|
|
||||||
|
|||||||
@@ -39,10 +39,6 @@ new Vue({
|
|||||||
store,
|
store,
|
||||||
render: h => h(App),
|
render: h => h(App),
|
||||||
created: function onApplicationCreated() {
|
created: function onApplicationCreated() {
|
||||||
firebase.auth().onAuthStateChanged((user) => {
|
|
||||||
this.$store.dispatch('stateChanged', user);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.$store.dispatch('loadArticles');
|
this.$store.dispatch('loadArticles');
|
||||||
},
|
},
|
||||||
}).$mount('#app');
|
}).$mount('#app');
|
||||||
|
|||||||
@@ -6,15 +6,7 @@ import Home from '@/views/Home';
|
|||||||
|
|
||||||
Vue.use(Router);
|
Vue.use(Router);
|
||||||
|
|
||||||
function AuthMiddleware(from, to, next) {
|
const router = new Router({
|
||||||
if (Store.getters.isUserAuthenticated) {
|
|
||||||
next();
|
|
||||||
} else {
|
|
||||||
next('/sign_in');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default new Router({
|
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
path: '/',
|
path: '/',
|
||||||
@@ -47,7 +39,7 @@ export default new Router({
|
|||||||
path: '/profile',
|
path: '/profile',
|
||||||
name: 'profile',
|
name: 'profile',
|
||||||
component: () => import(/* webpackChunkName: "words" */ '@/views/Profile'),
|
component: () => import(/* webpackChunkName: "words" */ '@/views/Profile'),
|
||||||
beforeEnter: AuthMiddleware,
|
meta: { authRequired: true },
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/sign_in',
|
path: '/sign_in',
|
||||||
@@ -66,3 +58,16 @@ export default new Router({
|
|||||||
],
|
],
|
||||||
mode: 'history',
|
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;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ export default {
|
|||||||
email: null,
|
email: null,
|
||||||
name: null,
|
name: null,
|
||||||
},
|
},
|
||||||
|
unSubscribeAuth: null,
|
||||||
},
|
},
|
||||||
mutations: {
|
mutations: {
|
||||||
setUser(state, { uid, email }) {
|
setUser(state, { uid, email }) {
|
||||||
@@ -28,8 +29,26 @@ export default {
|
|||||||
setUserEmail(state, email) {
|
setUserEmail(state, email) {
|
||||||
Vue.set(state.user, 'email', email);
|
Vue.set(state.user, 'email', email);
|
||||||
},
|
},
|
||||||
|
setUnSubscribeAuth(state, func) {
|
||||||
|
state.unSubscribeAuth = func;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
actions: {
|
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) {
|
async signUp({ commit }, payload) {
|
||||||
await commit('setProcessing', true);
|
await commit('setProcessing', true);
|
||||||
commit('clearError');
|
commit('clearError');
|
||||||
|
|||||||
Reference in New Issue
Block a user