mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.4 KiB
82 lines
2.4 KiB
2 years ago
|
import type { Api } from 'nocodb-sdk'
|
||
2 years ago
|
import { navigateTo, useGlobal, useRoute, useRouter } from '#imports'
|
||
2 years ago
|
|
||
|
const DbNotFoundMsg = 'Database config not found'
|
||
|
|
||
2 years ago
|
export function addAxiosInterceptors(api: Api<any>) {
|
||
2 years ago
|
const state = useGlobal()
|
||
2 years ago
|
const router = useRouter()
|
||
|
const route = useRoute()
|
||
|
|
||
|
api.instance.interceptors.request.use((config) => {
|
||
|
config.headers['xc-gui'] = 'true'
|
||
|
|
||
2 years ago
|
if (state.token.value) config.headers['xc-auth'] = state.token.value
|
||
2 years ago
|
|
||
2 years ago
|
if (!config.url?.endsWith('/user/me') && !config.url?.endsWith('/admin/roles') && state.previewAs?.value) {
|
||
|
config.headers['xc-preview'] = state.previewAs.value
|
||
2 years ago
|
}
|
||
|
|
||
|
if (!config.url?.endsWith('/user/me') && !config.url?.endsWith('/admin/roles')) {
|
||
2 years ago
|
if (route && route.params && route.params.projectType === 'base')
|
||
|
config.headers['xc-shared-base-id'] = route.params.projectId
|
||
2 years ago
|
}
|
||
|
|
||
|
return config
|
||
|
})
|
||
|
|
||
|
// Return a successful response back to the calling service
|
||
|
api.instance.interceptors.response.use(
|
||
|
(response) => response,
|
||
|
// Handle Error
|
||
|
(error) => {
|
||
|
if (error.response && error.response.data && error.response.data.msg === DbNotFoundMsg) return router.replace('/project/0')
|
||
|
|
||
|
// Return any error which is not due to authentication back to the calling service
|
||
|
if (!error.response || error.response.status !== 401) {
|
||
|
return Promise.reject(error)
|
||
|
}
|
||
|
|
||
|
// Logout user if token refresh didn't work or user is disabled
|
||
|
if (error.config.url === '/auth/refresh-token') {
|
||
2 years ago
|
state.signOut()
|
||
2 years ago
|
|
||
|
return Promise.reject(error)
|
||
|
}
|
||
|
|
||
|
// Try request again with new token
|
||
|
return api.instance
|
||
|
.post('/auth/refresh-token', null, {
|
||
|
withCredentials: true,
|
||
|
})
|
||
|
.then((token) => {
|
||
|
// New request with new token
|
||
|
const config = error.config
|
||
|
config.headers['xc-auth'] = token.data.token
|
||
2 years ago
|
state.signIn(token.data.token)
|
||
2 years ago
|
|
||
|
return new Promise((resolve, reject) => {
|
||
|
api.instance
|
||
|
.request(config)
|
||
|
.then((response) => {
|
||
|
resolve(response)
|
||
|
})
|
||
|
.catch((error) => {
|
||
|
reject(error)
|
||
|
})
|
||
|
})
|
||
|
})
|
||
|
.catch(async (error) => {
|
||
2 years ago
|
state.signOut()
|
||
2 years ago
|
// todo: handle new user
|
||
|
|
||
|
navigateTo('/signIn')
|
||
|
|
||
|
return Promise.reject(error)
|
||
|
})
|
||
|
},
|
||
|
)
|
||
2 years ago
|
|
||
|
return api
|
||
2 years ago
|
}
|