From c53b8273f953176f277ad510585d9cb2cbdaa49f Mon Sep 17 00:00:00 2001 From: Bruno Besson Date: Thu, 29 Dec 2022 08:11:33 +0100 Subject: [PATCH] feat: rework auth gards --- .../generics/buttons/LoginButton.vue | 21 ++++++++++++++++++- src/js/vue-plugins/router.js | 21 +++++++++++++++---- src/views/Navigation.vue | 9 +++++++- src/views/user/LoginView.vue | 8 +++---- .../utils/document-edition-view-mixin.js | 8 ------- 5 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/components/generics/buttons/LoginButton.vue b/src/components/generics/buttons/LoginButton.vue index 7805143c55..2e244b8063 100644 --- a/src/components/generics/buttons/LoginButton.vue +++ b/src/components/generics/buttons/LoginButton.vue @@ -1,7 +1,26 @@ + + diff --git a/src/js/vue-plugins/router.js b/src/js/vue-plugins/router.js index 515d6311f7..841732ad6c 100644 --- a/src/js/vue-plugins/router.js +++ b/src/js/vue-plugins/router.js @@ -61,14 +61,15 @@ const routes = [ { path: '/associations-history', name: 'associations-history', component: AssociationsHistoryView }, { path: '/auth', name: 'auth', component: LoginView }, { path: '/auth-sso', name: 'auth-sso', component: LoginView }, - { path: '/account', name: 'account', component: AccountView }, - { path: '/following', name: 'following', component: FollowingView }, - { path: '/preferences', name: 'preferences', component: PreferencesView }, - { path: '/trackers', name: 'trackers', component: TrackersView }, + { path: '/account', name: 'account', component: AccountView, meta: { requiresAuth: true } }, + { path: '/following', name: 'following', component: FollowingView, meta: { requiresAuth: true } }, + { path: '/preferences', name: 'preferences', component: PreferencesView, meta: { requiresAuth: true } }, + { path: '/trackers', name: 'trackers', component: TrackersView, meta: { requiresAuth: true } }, { path: '/trackers/:vendor/exchange-token', name: 'trackers-exchange-token', component: TrackersExchangeTokenView, + meta: { requiresAuth: true }, }, { path: '/yeti/:document_id(\\d+)?/:page?', name: 'yeti', component: YetiView }, { path: '/outings-stats', name: 'outings-stats', component: OutingsStatsView }, @@ -120,12 +121,14 @@ const addDocumentTypeView = function (def, viewComponent, editionComponent) { path: '/' + def.documentType + 's/edit/:id(\\d+)/:lang', name: def.documentType + '-edit', component: editionComponent, + meta: { requiresAuth: true }, }); routes.push({ path: '/' + def.documentType + 's/add/:lang', name: def.documentType + '-add', component: editionComponent, + meta: { requiresAuth: true }, }); routes.push({ @@ -191,4 +194,14 @@ const router = new Router({ }, }); +// authentication guard +router.beforeEach((to, from, next) => { + const vm = router.app; + if (to.matched.some((record) => record.meta.requiresAuth) && !vm.$user.isLogged) { + next({ name: 'auth', query: { redirect: to.fullPath } }); + } else { + next(); + } +}); + export default router; diff --git a/src/views/Navigation.vue b/src/views/Navigation.vue index 2a2b206468..af18ade0a6 100644 --- a/src/views/Navigation.vue +++ b/src/views/Navigation.vue @@ -119,7 +119,7 @@ - +   Logout @@ -254,6 +254,13 @@ export default { this.$refs.searchInput.focus(); }); }, + + signout() { + this.$user.signout(this.$route); + if (this.$route.meta.requiresAuth) { + this.$router.push({ name: 'home' }); + } + }, }, }; diff --git a/src/views/user/LoginView.vue b/src/views/user/LoginView.vue index 8f12bcc938..7e0aebd01e 100644 --- a/src/views/user/LoginView.vue +++ b/src/views/user/LoginView.vue @@ -201,7 +201,7 @@ export default { // that's all ! beforeRouteEnter(to, from, next) { next((vm) => { - vm.from = from; + vm.to = to; }); }, @@ -305,10 +305,10 @@ export default { }, signin() { - this.promise = this.$user.signIn(this.username, this.password).then(this.onSucessSigin); + this.promise = this.$user.signIn(this.username, this.password).then(this.onSuccessSigin); }, - onSucessSigin(data) { + onSuccessSigin(data) { const discourse_url = data.data.redirect_internal; if (discourse_url) { @@ -338,7 +338,7 @@ export default { // redirect() may be called twice if (!this.redirectionStillDone) { this.redirectionStillDone = true; - this.$router.push(this.from?.fullPath); + this.$router.push(this.to?.query?.redirect ?? '/'); } }, diff --git a/src/views/wiki/edition/utils/document-edition-view-mixin.js b/src/views/wiki/edition/utils/document-edition-view-mixin.js index 0eb1960faf..fac72b3f05 100644 --- a/src/views/wiki/edition/utils/document-edition-view-mixin.js +++ b/src/views/wiki/edition/utils/document-edition-view-mixin.js @@ -85,14 +85,6 @@ export default { window.removeEventListener('beforeunload', this.beforeUnload); }, - beforeRouteEnter(to, from, next) { - next((vm) => { - if (!vm.$user.isLogged) { - vm.$router.push({ name: 'auth' }); - } - }); - }, - beforeRouteLeave(to, from, next) { if (this.modified) { const answer = window.confirm(this.$gettext('Do you really want to leave? you have unsaved changes!'));