Set up an axios interceptor to inject the auth token on all requests

This commit is contained in:
Benedikt Kulmann
2020-05-12 15:46:33 +02:00
parent 0fddb624d0
commit 56bbab28e7
5 changed files with 29 additions and 9 deletions
+2 -2
View File
@@ -3,7 +3,7 @@ import SettingsApp from './components/SettingsApp.vue'
import store from './store'
// just a dummy function to trick gettext tools
function $gettext(msg) {
function $gettext (msg) {
return msg
}
@@ -14,7 +14,7 @@ const appInfo = {
isFileEditor: false,
extensions: [],
config: {
url: 'http://localhost:9190'
url: 'https://localhost:9200'
}
}
+19 -3
View File
@@ -3,6 +3,7 @@ import {
ListSettingsValues,
SaveSettingsValue
} from '../client/settings'
import axios from 'axios'
const state = {
config: null,
@@ -75,7 +76,8 @@ const actions = {
commit('SET_INITIALIZED', true)
},
async fetchSettingsBundles ({ commit, dispatch, getters }) {
async fetchSettingsBundles ({ commit, dispatch, getters, rootGetters }) {
injectAuthToken(rootGetters)
const response = await ListSettingsBundles({
$domain: getters.config.url,
body: {}
@@ -114,7 +116,8 @@ const actions = {
}
},
async fetchSettingsValues ({ commit, dispatch, getters }) {
async fetchSettingsValues ({ commit, dispatch, getters, rootGetters }) {
injectAuthToken(rootGetters)
const response = await ListSettingsValues({
$domain: getters.config.url,
body: {
@@ -139,7 +142,8 @@ const actions = {
}
},
async saveSettingsValue ({ commit, dispatch, getters }, payload) {
async saveSettingsValue ({ commit, dispatch, getters, rootGetters }, payload) {
injectAuthToken(rootGetters)
const response = await SaveSettingsValue({
$domain: getters.config.url,
body: {
@@ -178,3 +182,15 @@ function applySettingsValueToMap (settingsValue, map) {
map.get(settingsValue.identifier.extension).get(settingsValue.identifier.bundleKey).set(settingsValue.identifier.settingKey, settingsValue)
return map
}
function injectAuthToken (rootGetters) {
axios.interceptors.request.use(config => {
if (typeof config.headers.Authorization === 'undefined') {
const token = rootGetters.user.token
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
}
return config
})
}