From 643d9d09e9c61b6a69ecc6091d4226b867f41b98 Mon Sep 17 00:00:00 2001 From: jb-alvarado Date: Sun, 24 May 2020 13:35:22 +0200 Subject: [PATCH] global auth check, fix #11 --- ffplayout/frontend/pages/index.vue | 1 - ffplayout/frontend/pages/logging.vue | 2 -- ffplayout/frontend/pages/media.vue | 11 +---------- ffplayout/frontend/pages/message.vue | 5 ----- ffplayout/frontend/pages/player.vue | 12 ------------ ffplayout/frontend/plugins/axios.js | 4 +++- ffplayout/frontend/store/auth.js | 16 +++++++++++++--- 7 files changed, 17 insertions(+), 34 deletions(-) diff --git a/ffplayout/frontend/pages/index.vue b/ffplayout/frontend/pages/index.vue index 0caf55b6..84c09a0c 100644 --- a/ffplayout/frontend/pages/index.vue +++ b/ffplayout/frontend/pages/index.vue @@ -231,7 +231,6 @@ export default { if (process.browser) { this.interval = setInterval(async () => { - await this.$store.dispatch('auth/inspectToken') const response = await this.$axios.get('api/player/stats/?stats=all') this.stat = response.data }, 2000) diff --git a/ffplayout/frontend/pages/logging.vue b/ffplayout/frontend/pages/logging.vue index 1359b936..738483f3 100644 --- a/ffplayout/frontend/pages/logging.vue +++ b/ffplayout/frontend/pages/logging.vue @@ -87,7 +87,6 @@ export default { methods: { async getLog (type) { - await this.$store.dispatch('auth/inspectToken') this.logName = type const response = await this.$axios.get(`api/player/log/?type=${type}&date=${this.listDate}`) @@ -98,7 +97,6 @@ export default { } }, async getSystemLog () { - await this.$store.dispatch('auth/inspectToken') const response = await this.$axios.post('api/player/system/', { run: 'log' }) if (response.data.data) { diff --git a/ffplayout/frontend/pages/media.vue b/ffplayout/frontend/pages/media.vue index 33390fdc..01b74eeb 100644 --- a/ffplayout/frontend/pages/media.vue +++ b/ffplayout/frontend/pages/media.vue @@ -294,11 +294,7 @@ export default { ...mapState('media', ['crumbs', 'folderTree']) }, - async created () { - await this.$store.dispatch('auth/inspectToken') - await this.$store.dispatch('config/getGuiConfig') - await this.$store.dispatch('config/getPlayoutConfig') - + created () { this.extensions = [...this.configPlayout.storage.extensions, ...this.configGui.extra_extensions].join(' ') this.getPath(this.extensions, '') }, @@ -307,7 +303,6 @@ export default { async getPath (extensions, path) { this.lastPath = path this.isLoading = true - await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('media/getTree', { extensions, path }) this.isLoading = false }, @@ -349,7 +344,6 @@ export default { async onSubmitCreateFolder (evt) { evt.preventDefault() - await this.$store.dispatch('auth/inspectToken') await this.$axios.post( 'api/player/media/op/', @@ -389,7 +383,6 @@ export default { } for (const [i, file] of this.inputFiles.entries()) { - await this.$store.dispatch('auth/inspectToken') this.uploadTask = file.name const config = { @@ -466,7 +459,6 @@ export default { async renameFile (evt) { evt.preventDefault() - await this.$store.dispatch('auth/inspectToken') await this.$axios.patch( 'api/player/media/op/', @@ -502,7 +494,6 @@ export default { }, async deleteFileOrFolder () { - await this.$store.dispatch('auth/inspectToken') let file let pathName diff --git a/ffplayout/frontend/pages/message.vue b/ffplayout/frontend/pages/message.vue index d86e3ab3..18e94bed 100644 --- a/ffplayout/frontend/pages/message.vue +++ b/ffplayout/frontend/pages/message.vue @@ -275,7 +275,6 @@ export default { methods: { async getPreset (preset) { - await this.$store.dispatch('auth/inspectToken') let req = '' if (preset) { @@ -316,7 +315,6 @@ export default { this.createPreset() }, async createPreset () { - await this.$store.dispatch('auth/inspectToken') const preset = { name: this.newPresetName, message: this.form.text, @@ -346,7 +344,6 @@ export default { }) }, async savePreset () { - await this.$store.dispatch('auth/inspectToken') if (this.selected) { const preset = { id: this.form.id, @@ -383,7 +380,6 @@ export default { this.deletePreset() }, async deletePreset () { - await this.$store.dispatch('auth/inspectToken') if (this.selected) { await this.$axios.delete(`api/player/messenger/${this.form.id}/`) } @@ -393,7 +389,6 @@ export default { }, async submitMessage () { - await this.$store.dispatch('auth/inspectToken') function aToHex (num) { return '0x' + Math.round(num * 255).toString(16) } diff --git a/ffplayout/frontend/pages/player.vue b/ffplayout/frontend/pages/player.vue index ae46ba04..bd8b9493 100644 --- a/ffplayout/frontend/pages/player.vue +++ b/ffplayout/frontend/pages/player.vue @@ -335,7 +335,6 @@ export default { }, async created () { - await this.getConfig() await this.getStatus() this.extensions = this.configPlayout.storage.extensions.join(' ') @@ -382,20 +381,12 @@ export default { }, methods: { - async getConfig () { - await this.$store.dispatch('auth/inspectToken') - await this.$store.dispatch('config/getGuiConfig') - await this.$store.dispatch('config/getPlayoutConfig') - }, async getPath (extensions, path) { this.isLoading = true - await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('media/getTree', { extensions, path }) this.isLoading = false }, async getStatus () { - await this.$store.dispatch('auth/inspectToken') - const status = await this.$axios.post('api/player/system/', { run: 'status' }) if (status.data.data && status.data.data === 'active') { @@ -405,13 +396,11 @@ export default { } }, async playoutControl (state) { - await this.$store.dispatch('auth/inspectToken') await this.$axios.post('api/player/system/', { run: state }) setTimeout(() => { this.getStatus() }, 1000) }, async getPlaylist () { - await this.$store.dispatch('auth/inspectToken') await this.$store.dispatch('playlist/getPlaylist', { dayStart: this.configPlayout.playlist.day_start, date: this.listDate }) }, showModal (src) { @@ -464,7 +453,6 @@ export default { await this.$store.dispatch('playlist/getPlaylist', { dayStart: this.configPlayout.playlist.day_start, date: this.listDate }) }, async savePlaylist () { - await this.$store.dispatch('auth/inspectToken') this.$store.commit('playlist/UPDATE_PLAYLIST', this.$processPlaylist( this.configPlayout.playlist.day_start, this.playlist)) diff --git a/ffplayout/frontend/plugins/axios.js b/ffplayout/frontend/plugins/axios.js index f8f1b113..586e8b53 100644 --- a/ffplayout/frontend/plugins/axios.js +++ b/ffplayout/frontend/plugins/axios.js @@ -1,4 +1,6 @@ -export default function ({ $axios, store, redirect }) { +export default async function ({ $axios, store, redirect }) { + await store.dispatch('auth/inspectToken') + $axios.onRequest((config) => { const token = store.state.auth.jwtToken if (token) { diff --git a/ffplayout/frontend/store/auth.js b/ffplayout/frontend/store/auth.js index 005031da..9c73d334 100644 --- a/ffplayout/frontend/store/auth.js +++ b/ffplayout/frontend/store/auth.js @@ -62,10 +62,20 @@ export const actions = { refresh: state.jwtRefresh, progress: false } - const response = await this.$axios.post('auth/token/refresh/', payload) - commit('UPADTE_TOKEN', { token: response.data.access }) - commit('UPDATE_IS_LOGIN', true) + await this.$axios.post('auth/token/refresh/', payload) + .then((response) => { + commit('UPADTE_TOKEN', { token: response.data.access }) + commit('UPDATE_IS_LOGIN', true) + }) + .catch((error) => { + if (error.response.status === 401) { + this.$cookies.remove('token') + this.$cookies.remove('refresh') + commit('UPADTE_TOKEN', { token: null, refresh: null }) + commit('UPDATE_IS_LOGIN', false) + } + }) }, async inspectToken ({ commit, dispatch, state }) {