Skip to content

Commit

Permalink
Merge pull request #3169 from c2corg/auth-guard
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobesson authored Dec 29, 2022
2 parents 27817be + c53b827 commit ae628c8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
21 changes: 20 additions & 1 deletion src/components/generics/buttons/LoginButton.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
<template>
<router-link v-show="!$user.isLogged" :to="{ name: 'auth' }" class="button is-primary">
<router-link v-show="!$user.isLogged" :to="{ name: 'auth', query: { redirect } }" class="button is-primary">
<slot>
<span v-translate>Login</span>
</slot>
</router-link>
</template>

<script>
export default {
data() {
return {
redirect: '/',
};
},
watch: {
$route: {
handler(to) {
this.redirect = to.fullPath;
},
immediate: true,
},
},
};
</script>
21 changes: 17 additions & 4 deletions src/js/vue-plugins/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,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 },
Expand Down Expand Up @@ -117,12 +118,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({
Expand Down Expand Up @@ -188,4 +191,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;
9 changes: 8 additions & 1 deletion src/views/Navigation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

<hr class="dropdown-divider" />

<a class="dropdown-item is-size-5" @click="$user.signout()">
<a class="dropdown-item is-size-5" @click="signout()">
<fa-icon icon="sign-out-alt" />
<span>&nbsp;</span>
<span v-translate>Logout</span>
Expand Down Expand Up @@ -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' });
}
},
},
};
</script>
Expand Down
8 changes: 4 additions & 4 deletions src/views/user/LoginView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default {
// that's all !
beforeRouteEnter(to, from, next) {
next((vm) => {
vm.from = from;
vm.to = to;
});
},
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 ?? '/');
}
},
Expand Down
8 changes: 0 additions & 8 deletions src/views/wiki/edition/utils/document-edition-view-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!'));
Expand Down

0 comments on commit ae628c8

Please sign in to comment.