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.
133 lines
4.4 KiB
133 lines
4.4 KiB
4 years ago
|
// import axios from 'axios';
|
||
|
//
|
||
|
// if (!window.axios) {
|
||
|
// window.axios = axios.create({
|
||
|
// baseURL: 'http://localhost:8080',
|
||
|
// });
|
||
|
// }
|
||
|
|
||
3 years ago
|
export default ({ store, $axios, redirect, $toast, route, app }) => {
|
||
4 years ago
|
// Add a request interceptor
|
||
3 years ago
|
$axios.interceptors.request.use(function(config) {
|
||
3 years ago
|
config.headers['xc-gui'] = 'true'
|
||
4 years ago
|
if (store.state.users.token) {
|
||
3 years ago
|
config.headers['xc-auth'] = store.state.users.token
|
||
4 years ago
|
}
|
||
|
if (!config.url.endsWith('/user/me') && !config.url.endsWith('/admin/roles') && store.state.users.previewAs) {
|
||
3 years ago
|
config.headers['xc-preview'] = store.state.users.previewAs
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
if (!config.url.endsWith('/user/me') && !config.url.endsWith('/admin/roles')) {
|
||
|
if (app.context && app.context.route && app.context.route.params && app.context.route.params.shared_base_id) {
|
||
|
config.headers['xc-shared-base-id'] = app.context.route.params.shared_base_id
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
return config
|
||
|
})
|
||
4 years ago
|
|
||
|
// $axios.setBaseURL('http://localhost:8080')
|
||
|
|
||
|
$axios.interceptors.response.use((response) => {
|
||
|
// Return a successful response back to the calling service
|
||
3 years ago
|
return response
|
||
4 years ago
|
}, (error) => {
|
||
|
if (error.response && error.response.data && error.response.data.msg === 'Database config not found') {
|
||
3 years ago
|
redirect('/project/0')
|
||
|
return
|
||
4 years ago
|
}
|
||
|
|
||
|
// Return any error which is not due to authentication back to the calling service
|
||
3 years ago
|
if (!error.response || error.response.status !== 401) {
|
||
4 years ago
|
return new Promise((resolve, reject) => {
|
||
3 years ago
|
reject(error)
|
||
|
})
|
||
4 years ago
|
}
|
||
|
|
||
|
// Logout user if token refresh didn't work or user is disabled
|
||
3 years ago
|
if (error.config.url === '/auth/refresh-token') {
|
||
4 years ago
|
store.dispatch('users/ActSignOut')
|
||
|
|
||
|
return new Promise((resolve, reject) => {
|
||
3 years ago
|
reject(error)
|
||
|
})
|
||
4 years ago
|
}
|
||
|
|
||
|
// Try request again with new token
|
||
3 years ago
|
return $axios.post('/auth/refresh-token', null, {
|
||
4 years ago
|
withCredentials: true
|
||
|
})
|
||
|
.then((token) => {
|
||
|
// New request with new token
|
||
3 years ago
|
const config = error.config
|
||
|
config.headers['xc-auth'] = token.data.token
|
||
4 years ago
|
store.commit('users/MutSetToken', token.data.token)
|
||
|
|
||
|
return new Promise((resolve, reject) => {
|
||
3 years ago
|
$axios.request(config).then((response) => {
|
||
|
resolve(response)
|
||
4 years ago
|
}).catch((error) => {
|
||
3 years ago
|
reject(error)
|
||
4 years ago
|
})
|
||
3 years ago
|
})
|
||
4 years ago
|
})
|
||
3 years ago
|
.catch(async(error) => {
|
||
3 years ago
|
await store.dispatch('users/ActSignOut')
|
||
3 years ago
|
if (store.state.project.appInfo.firstUser) {
|
||
3 years ago
|
redirect('/')
|
||
4 years ago
|
} else {
|
||
3 years ago
|
$toast.clear()
|
||
2 years ago
|
$toast.info('Token Expired. Please login again.', {
|
||
4 years ago
|
position: 'bottom-center'
|
||
|
}).goAway(5000)
|
||
3 years ago
|
redirect('/user/authentication/signin')
|
||
4 years ago
|
}
|
||
3 years ago
|
Promise.reject(error)
|
||
|
})
|
||
|
})
|
||
4 years ago
|
}
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|