Add axios default config
This commit is contained in:
parent
fb83281978
commit
c19f5b62a0
|
|
@ -0,0 +1,47 @@
|
||||||
|
import axios from 'axios';
|
||||||
|
import dispatcher from '../stores/dispatcher';
|
||||||
|
import * as GlobalAction from './GlobalAction';
|
||||||
|
|
||||||
|
let currentToken = null;
|
||||||
|
const tokenKey = 'gotify-login-key';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the authorization token for the next requests.
|
||||||
|
* @param {string} token the gotify application token
|
||||||
|
*/
|
||||||
|
export function setAuthorizationToken(token) {
|
||||||
|
currentToken = token;
|
||||||
|
if (token) {
|
||||||
|
localStorage.setItem(tokenKey, token);
|
||||||
|
axios.defaults.headers.common['X-Gotify-Key'] = token;
|
||||||
|
} else {
|
||||||
|
localStorage.removeItem(tokenKey);
|
||||||
|
delete axios.defaults.headers.common['X-Gotify-Key'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
axios.interceptors.response.use(null, (error) => {
|
||||||
|
if (error.response.status === 401) {
|
||||||
|
setAuthorizationToken(null);
|
||||||
|
dispatcher.dispatch({type: 'REMOVE_CURRENT_USER'});
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn('Error status', error.response.status);
|
||||||
|
return Promise.reject(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {string} the application token
|
||||||
|
*/
|
||||||
|
export function getToken() {
|
||||||
|
return currentToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Checks if the current user is logged, if so update the state. */
|
||||||
|
export function checkIfAlreadyLoggedIn() {
|
||||||
|
const key = localStorage.getItem(tokenKey);
|
||||||
|
if (key) {
|
||||||
|
setAuthorizationToken(key);
|
||||||
|
GlobalAction.initialLoad();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue