feat: add authenticated http factory to create an axios instance with bearer header
This commit is contained in:
parent
7c954e9168
commit
59da6686d0
2 changed files with 15 additions and 8 deletions
|
@ -1,7 +1,18 @@
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
import {getToken} from '@/helpers/auth'
|
||||||
|
|
||||||
export const HTTPFactory = () => {
|
export function HTTPFactory() {
|
||||||
return axios.create({
|
return axios.create({
|
||||||
baseURL: window.API_URL,
|
baseURL: window.API_URL,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function AuthenticatedHTTPFactory(token = getToken()) {
|
||||||
|
return axios.create({
|
||||||
|
baseURL: window.API_URL,
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import {HTTPFactory} from '@/http-common'
|
import {HTTPFactory, AuthenticatedHTTPFactory} from '@/http-common'
|
||||||
import {i18n, getCurrentLanguage, saveLanguage} from '@/i18n'
|
import {i18n, getCurrentLanguage, saveLanguage} from '@/i18n'
|
||||||
import {objectToSnakeCase} from '@/helpers/case'
|
import {objectToSnakeCase} from '@/helpers/case'
|
||||||
import {LOADING} from '../mutation-types'
|
import {LOADING} from '../mutation-types'
|
||||||
|
@ -215,13 +215,9 @@ export default {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const HTTP = HTTPFactory()
|
const HTTP = AuthenticatedHTTPFactory(jwt)
|
||||||
try {
|
try {
|
||||||
const response = await HTTP.get('user', {
|
const response = await HTTP.get('user')
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${jwt}`,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
const info = new UserModel(response.data)
|
const info = new UserModel(response.data)
|
||||||
info.type = state.info.type
|
info.type = state.info.type
|
||||||
info.email = state.info.email
|
info.email = state.info.email
|
||||||
|
|
Loading…
Reference in a new issue