这是我的Utils.js
文件:
import axios from 'axios'
import { setCurrentUser, Logout } from './Redux/Actions/UserActions'
import UserApi from './Redux/Api/UserApi'
const TOKEN_NAME = 'atoken'
const LANGUAGE_TOKEN_NAME = 'gocvlang'
const ACCCEPTED_LANGUAGES = 'en'
const SAVED_USER_KEY = 'saveduser'
export { TOKEN_NAME, SAVED_USER_KEY, ACCCEPTED_LANGUAGES, LANGUAGE_TOKEN_NAME }
这是`Api.js
import axios from 'axios'
import { TOKEN_NAME, LANGUAGE_TOKEN_NAME, ACCCEPTED_LANGUAGES } from '../../Utils'
console.log(TOKEN_NAME);
/* global localStorage */
const UserToken = localStorage.getItem(TOKEN_NAME)
const Lang = localStorage.getItem(LANGUAGE_TOKEN_NAME)
这就是我在第3行上在console.log(TOKEN_NAME
上遇到的问题
undefined
有人可以告诉我是什么问题吗?
编辑:
import axios from 'axios'
import { setCurrentUser, Logout } from './Redux/Actions/UserActions'
import UserApi from './Redux/Api/UserApi'
const TOKEN_NAME = 'atoken'
const LANGUAGE_TOKEN_NAME = 'gocvlang'
const ACCCEPTED_LANGUAGES = 'en'
const SAVED_USER_KEY = 'saveduser'
export { TOKEN_NAME, SAVED_USER_KEY, ACCCEPTED_LANGUAGES, LANGUAGE_TOKEN_NAME }
/* global localStorage */
export const logout = () => {
localStorage.removeItem(SAVED_USER_KEY)
localStorage.removeItem(LANGUAGE_TOKEN_NAME)
localStorage.removeItem(TOKEN_NAME)
}
export const authenticated = (token = '', lang = null) => {
if(token.length < 10) {
return false;
}
if(! validateLanguage(lang)) {
return false;
}
localStorage.setItem(TOKEN_NAME, token)
localStorage.setItem(LANGUAGE_TOKEN_NAME, lang)
}
export const AuthOnReload = (store) => {
const savedUser = localStorage.getItem(SAVED_USER_KEY)
if(! savedUser) {
return console.log('no saved user');
}
if(! localStorage.getItem(TOKEN_NAME)) {
return console.log('no user token');
}
try {
const user = JSON.parse(savedUser);
console.log(user, 'User Set');
store.dispatch(setCurrentUser(user, localStorage.getItem(TOKEN_NAME)))
UserApi.verify(user).then(res => console.log(res))
} catch (Exception) {
return;
}
/**
* @ CHECK IF CURRENT USER MATCHES TOKEN
*/
}
export const validateLanguage = (lang) => {
if(! lang) {
lang = localStorage.getItem(LANGUAGE_TOKEN_NAME);
}
return ACCCEPTED_LANGUAGES.indexOf(lang) >= 0;
}
Api.js
import axios from 'axios'
import { TOKEN_NAME, LANGUAGE_TOKEN_NAME, ACCCEPTED_LANGUAGES } from '../../Utils'
console.log(TOKEN_NAME);
/* global localStorage */
const UserToken = localStorage.getItem(TOKEN_NAME)
const Lang = localStorage.getItem(LANGUAGE_TOKEN_NAME)
/**
* Default Authenticated Api
*/
export default axios.create({
baseUrl: window.proxy,
headers: {
'Authorization': `Bearer ${UserToken}`,
'Accept-Language': Lang
},
validateStatus: function (status) {
console.log(status);
return status >= 200 && status < 300; // default
}
})
/**
* Default Unauthenticated Api
*
*/
export const GuestApi = axios.create({
baseUrl: window.proxy
})