2016-11-24 263 views
1

我正在檢查每個axios請求是否存在非公用路徑的標頭中的令牌。如果未找到令牌,我想讓用戶登錄頁面。 但這是在reactjs應用程序內。有什麼辦法可以做到這一點?Howto在reactjs應用程序中從axios攔截器重定向?

axios.interceptors.request.use(function (config) { 
//If the header does not contain the token and the url not public, redirect to login 
var accessToken = null; 
if(sessionStorage.getItem('currentUserString')){ 
    accessToken = 'bearer '+JSON.parse(sessionStorage.getItem('currentUserString')).tokenDetails.accessToken; 
} 

var configURL = config.url; 
//if token is found add it to the header 
if (accessToken) { 
    if (config.method !== 'OPTIONS') { 
      config.headers.authorization = accessToken; 
    } 
} 
//otherwise if the request is not for login page/auth service/home redirect to login page 
else if(!isNotOpenPath(config.url)){ 

    //we may want to store current path in the cookies. This can be retrieved after login is successful 
    //WHAT TO DO HERE TO TAKE USER TO LOGIN PAGE IN THE REACT JS APP???? 
    }  
} 

如何發出反應的應用程序,特別是我們沒有派遣。 我可以只使用 document.location.href='\login'

+0

何不了window.location =「LOGINPATH」? – Hosar

+0

好的。但那將是方法。對?沒有反應的具體方式存在? –

回答

相關問題