diff --git a/ffplayout/frontend/nuxt.config.js b/ffplayout/frontend/nuxt.config.js index 1cf8c853..a8f1f358 100644 --- a/ffplayout/frontend/nuxt.config.js +++ b/ffplayout/frontend/nuxt.config.js @@ -43,6 +43,7 @@ export default { ** Plugins to load before mounting the App */ plugins: [ + { src: '~/plugins/axios' }, { src: '~/plugins/filters' }, { src: '~plugins/video.js', ssr: false }, { src: '~plugins/scrollbar.js', ssr: false }, diff --git a/ffplayout/frontend/pages/index.vue b/ffplayout/frontend/pages/index.vue index 9a7936c1..bb982fbd 100644 --- a/ffplayout/frontend/pages/index.vue +++ b/ffplayout/frontend/pages/index.vue @@ -130,8 +130,8 @@
- - Control + + Player Media @@ -212,13 +212,13 @@ export default { }, async sysStats () { - const response = await this.$axios.get('api/stats/?stats=all', { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken }, progress: false }) + const response = await this.$axios.get('api/stats/?stats=all') this.stat = response.data if (process.browser) { this.interval = setInterval(async () => { await this.$store.dispatch('auth/inspectToken') - const response = await this.$axios.get('api/stats/?stats=all', { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken }, progress: false }) + const response = await this.$axios.get('api/stats/?stats=all') this.stat = response.data }, 2000) } diff --git a/ffplayout/frontend/pages/logging.vue b/ffplayout/frontend/pages/logging.vue index 736fecd0..4a56ed50 100644 --- a/ffplayout/frontend/pages/logging.vue +++ b/ffplayout/frontend/pages/logging.vue @@ -75,10 +75,7 @@ export default { methods: { async getLog (type) { await this.$store.dispatch('auth/inspectToken') - const response = await this.$axios.get( - `api/log/?type=${type}`, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + const response = await this.$axios.get(`api/log/?type=${type}`) if (response.data.log) { this.currentLog = response.data.log @@ -86,11 +83,7 @@ export default { }, async getSystemLog () { await this.$store.dispatch('auth/inspectToken') - const response = await this.$axios.post( - 'api/system/', - { run: 'log' }, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + const response = await this.$axios.post('api/system/', { run: 'log' }) if (response.data.data) { this.currentLog = response.data.data diff --git a/ffplayout/frontend/pages/media.vue b/ffplayout/frontend/pages/media.vue index 0e383efe..65d4bd95 100644 --- a/ffplayout/frontend/pages/media.vue +++ b/ffplayout/frontend/pages/media.vue @@ -277,8 +277,7 @@ export default { await this.$axios.post( 'api/media/op/', - { folder: this.folderName, path: this.crumbs.map(e => e.text).join('/') }, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } + { folder: this.folderName, path: this.crumbs.map(e => e.text).join('/') } ) this.$root.$emit('bv::hide::modal', 'folder-modal') @@ -307,14 +306,13 @@ export default { async onSubmitUpload (evt) { evt.preventDefault() - await this.$store.dispatch('auth/inspectToken') - const uploadProgress = fileName => (progressEvent) => { const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total) this.currentProgress = progress } for (const [i, file] of this.inputFiles.entries()) { + await this.$store.dispatch('auth/inspectToken') this.uploadTask = file.name const config = { @@ -330,8 +328,7 @@ export default { ) .then( this.overallProgress = (i + 1) * 100 / this.inputFiles.length, - this.currentProgress = 0, - await this.$store.dispatch('auth/inspectToken') + this.currentProgress = 0 ) .catch(err => console.log(err)) } @@ -406,10 +403,7 @@ export default { pathName = this.deleteSource } - await this.$axios.delete( - `api/media/op/?file=${encodeURIComponent(file)}&path=${encodeURIComponent(pathName)}`, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + await this.$axios.delete(`api/media/op/?file=${encodeURIComponent(file)}&path=${encodeURIComponent(pathName)}`) .catch(err => console.log(err)) this.$root.$emit('bv::hide::modal', 'delete-modal') diff --git a/ffplayout/frontend/pages/message.vue b/ffplayout/frontend/pages/message.vue index d8feda56..09b7466b 100644 --- a/ffplayout/frontend/pages/message.vue +++ b/ffplayout/frontend/pages/message.vue @@ -281,7 +281,7 @@ export default { if (preset) { req = `?name=${preset}` } - const response = await this.$axios.get(`api/messenger/${req}`, { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } }) + const response = await this.$axios.get(`api/messenger/${req}`) if (response.data && !preset) { for (const item of response.data) { @@ -333,11 +333,7 @@ export default { overall_alpha: this.form.overallAlpha } - const response = await this.$axios.post( - 'api/messenger/', - preset, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + const response = await this.$axios.post('api/messenger/', preset) if (response.status === 201) { this.success = true @@ -369,11 +365,7 @@ export default { overall_alpha: this.form.overallAlpha } - const response = await this.$axios.put( - `api/messenger/${this.form.id}/`, - preset, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + const response = await this.$axios.put(`api/messenger/${this.form.id}/`, preset) if (response.status === 200) { this.success = true @@ -393,10 +385,7 @@ export default { async deletePreset () { await this.$store.dispatch('auth/inspectToken') if (this.selected) { - await this.$axios.delete( - `api/messenger/${this.form.id}/`, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + await this.$axios.delete(`api/messenger/${this.form.id}/`) } this.$bvModal.hide('delete-modal') @@ -422,11 +411,7 @@ export default { boxborderw: this.form.border } - const response = await this.$axios.post( - 'api/send/', - { data: obj }, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + const response = await this.$axios.post('api/send/', { data: obj }) if (response.data && response.data.status.Success && response.data.status.Success === '0 Success') { this.success = true diff --git a/ffplayout/frontend/pages/player.vue b/ffplayout/frontend/pages/player.vue index c280d034..537748de 100644 --- a/ffplayout/frontend/pages/player.vue +++ b/ffplayout/frontend/pages/player.vue @@ -366,11 +366,7 @@ export default { async getStatus () { await this.$store.dispatch('auth/inspectToken') - const status = await this.$axios.post( - 'api/system/', - { run: 'status' }, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + const status = await this.$axios.post('api/system/', { run: 'status' }) if (status.data.data && status.data.data === 'active') { this.isPlaying = 'is-playing' @@ -380,11 +376,7 @@ export default { }, async playoutControl (state) { await this.$store.dispatch('auth/inspectToken') - await this.$axios.post( - 'api/system/', - { run: state }, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } - ) + await this.$axios.post('api/system/', { run: state }) setTimeout(() => { this.getStatus() }, 1000) }, @@ -450,8 +442,7 @@ export default { await this.$axios.post( 'api/playlist/', - { data: { channel: this.$store.state.playlist.playlistChannel, date: this.today, program: saveList } }, - { headers: { Authorization: 'Bearer ' + this.$store.state.auth.jwtToken } } + { data: { channel: this.$store.state.playlist.playlistChannel, date: this.today, program: saveList } } ) } } diff --git a/ffplayout/frontend/plugins/axios.js b/ffplayout/frontend/plugins/axios.js new file mode 100644 index 00000000..6cc779ba --- /dev/null +++ b/ffplayout/frontend/plugins/axios.js @@ -0,0 +1,21 @@ +export default function ({ $axios, store, redirect }) { + $axios.onRequest((config) => { + const token = store.state.auth.jwtToken + if (token) { + config.headers.common.Authorization = `Bearer ${token}` + } + + // disable progress on auth and stats + if (config.url.includes('stats') || config.url.includes('auth')) { + config.progress = false + } + }) + + $axios.onError((error) => { + const code = parseInt(error.response && error.response.status) + + if (code === 400) { + redirect('/') + } + }) +} diff --git a/ffplayout/frontend/store/config.js b/ffplayout/frontend/store/config.js index 682637f7..9a605c40 100644 --- a/ffplayout/frontend/store/config.js +++ b/ffplayout/frontend/store/config.js @@ -25,9 +25,9 @@ export const mutations = { } export const actions = { - async getGuiConfig ({ commit, state, rootState }) { - const options = await this.$axios.options('api/guisettings/', { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) - const response = await this.$axios.get('api/guisettings/', { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) + async getGuiConfig ({ commit, state }) { + const options = await this.$axios.options('api/guisettings/') + const response = await this.$axios.get('api/guisettings/') if (options.data) { const choices = options.data.actions.POST.net_interface.choices.map(function (obj) { @@ -43,29 +43,29 @@ export const actions = { } }, - async setGuiConfig ({ commit, state, rootState }, obj) { + async setGuiConfig ({ commit, state }, obj) { const stringObj = JSON.parse(JSON.stringify(obj)) stringObj.extra_extensions = obj.extra_extensions.join(' ') - const update = await this.$axios.put('api/guisettings/1/', stringObj, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) + const update = await this.$axios.put('api/guisettings/1/', stringObj) return update }, - async getPlayoutConfig ({ commit, state, rootState }) { - const response = await this.$axios.get('api/config/?configPlayout', { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) + async getPlayoutConfig ({ commit, state }) { + const response = await this.$axios.get('api/config/?configPlayout') if (response.data) { commit('UPDATE_PLAYLOUT_CONFIG', response.data) } }, - async setPlayoutConfig ({ commit, state, rootState }, obj) { - const update = await this.$axios.post('api/config/?configPlayout', { data: obj }, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) + async setPlayoutConfig ({ commit, state }, obj) { + const update = await this.$axios.post('api/config/?configPlayout', { data: obj }) return update }, - async getUserConfig ({ commit, state, rootState }) { - const user = await this.$axios.get('api/current/user/', { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) - const response = await this.$axios.get(`api/users/?username=${user.data.username}`, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) + async getUserConfig ({ commit, state }) { + const user = await this.$axios.get('api/current/user/') + const response = await this.$axios.get(`api/users/?username=${user.data.username}`) if (user.data) { commit('SET_CURRENT_USER', user.data.username) @@ -75,8 +75,8 @@ export const actions = { } }, - async setUserConfig ({ commit, state, rootState }, obj) { - const update = await this.$axios.put(`api/users/${obj.id}/`, obj, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) + async setUserConfig ({ commit, state }, obj) { + const update = await this.$axios.put(`api/users/${obj.id}/`, obj) return update } } diff --git a/ffplayout/frontend/store/media.js b/ffplayout/frontend/store/media.js index 4e1ccc63..db6489d0 100644 --- a/ffplayout/frontend/store/media.js +++ b/ffplayout/frontend/store/media.js @@ -17,10 +17,10 @@ export const mutations = { } export const actions = { - async getTree ({ commit, dispatch, state, rootState }, { extensions, path }) { + async getTree ({ commit, dispatch, state }, { extensions, path }) { const crumbs = [] let root = '/' - const response = await this.$axios.get(`api/media/?extensions=${extensions}&path=${path}`, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) + const response = await this.$axios.get(`api/media/?extensions=${extensions}&path=${path}`) if (response.data.tree) { const pathArr = response.data.tree[0].split('/') diff --git a/ffplayout/frontend/store/playlist.js b/ffplayout/frontend/store/playlist.js index 11cc4b91..50fb937d 100644 --- a/ffplayout/frontend/store/playlist.js +++ b/ffplayout/frontend/store/playlist.js @@ -50,8 +50,8 @@ export const mutations = { } export const actions = { - async getPlaylist ({ commit, dispatch, state, rootState }, { dayStart, date }) { - const response = await this.$axios.get(`api/playlist/?date=${date}`, { headers: { Authorization: 'Bearer ' + rootState.auth.jwtToken } }) + async getPlaylist ({ commit, dispatch, state }, { dayStart, date }) { + const response = await this.$axios.get(`api/playlist/?date=${date}`) if (response.data && response.data.program) { commit('UPDATE_PLAYLIST_CHANNEL', response.data.channel)