1
0

fix: authenticate per request (#2258)

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: https://kolaente.dev/vikunja/frontend/pulls/2258
Reviewed-by: konrad <k@knt.li>
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni
2022-09-04 14:30:11 +00:00
committed by konrad
parent 3e770e11f1
commit 6e4a3ff199
3 changed files with 29 additions and 23 deletions

View File

@ -2,17 +2,35 @@ import axios from 'axios'
import {getToken} from '@/helpers/auth'
export function HTTPFactory() {
return axios.create({
baseURL: window.API_URL,
const instance = axios.create({baseURL: window.API_URL})
instance.interceptors.request.use((config) => {
// by setting the baseURL fresh for every request
// we make sure that it is never outdated in case it is updated
config.baseURL = window.API_URL
return config
})
return instance
}
export function AuthenticatedHTTPFactory(token = getToken()) {
return axios.create({
baseURL: window.API_URL,
headers: {
Authorization: `Bearer ${token}`,
export function AuthenticatedHTTPFactory() {
const instance = HTTPFactory()
instance.interceptors.request.use((config) => {
config.headers = {
...config.headers,
'Content-Type': 'application/json',
},
}
// Set the default auth header if we have a token
const token = getToken()
if (token !== null) {
config.headers['Authorization'] = `Bearer ${token}`
}
return config
})
return instance
}