From bfb8afc662622971b7c0242974a679d6da7bbb46 Mon Sep 17 00:00:00 2001 From: Grzegorz Leoniec Date: Tue, 5 Feb 2019 11:04:41 +0100 Subject: [PATCH] Improved post edit guard --- pages/post/_slug/edit/_id.vue | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/pages/post/_slug/edit/_id.vue b/pages/post/_slug/edit/_id.vue index 491fa004e..f210e210d 100644 --- a/pages/post/_slug/edit/_id.vue +++ b/pages/post/_slug/edit/_id.vue @@ -21,14 +21,21 @@ export default { HcContributionForm }, computed: { + user() { + return this.$store.getters['auth/user'] + }, + author() { + return this.contribution ? this.contribution.author : {} + }, contribution() { - if (!this.Post || !this.Post[0].id) { - return - } - if (this.Post[0].author.id !== this.$store.getters['auth/user'].id) { + return this.Post ? this.Post[0] : {} + } + }, + watch: { + contribution() { + if (this.author.id !== this.user.id) { throw new Error(`You can't edit that!`) } - return this.Post[0] } }, apollo: {