2017-07-04 124 views
0

我們需要檢查訪問該站點的用戶是否已登錄。如果不是,則必須將其重定向到登錄屏幕。我試圖找到一個JS解決方案,發現here,但控制檯仍在吐出the user is not logged in即使他們是。Bigcommerce重定向登錄如果註銷

我檢查該cookie的Bigcommerce使用,它似乎仍然使用LoginEmail

//this will allow you to mention the cookie by index 
function getCookie(name) { 
    var value = "; " + document.cookie; 
    var parts = value.split("; " + name + "="); 
    if (parts.length == 2) return parts.pop().split(";").shift(); 
} 
//set variable that will check if login email exists 
var loggedIn = getCookie('LoginEmail'); 

//logic that will output different content based on the loggedIn Status 
if(typeof loggedIn === 'undefined'){ 
    console.log("They are not logged in!"); 
    window.location.replace("/login.php"); 
} 
else{ 
    console.log("They are logged in! "); 
} 

我與模板主頁觸發此。提前致謝!

+0

你的意思是,如果有人訪問你的網站訪問者必須登錄查看您的網站? –

+0

@RajKumarBhardwaj很好。我最終通過檢測登錄按鈕div'guest-user'來尋找不同的方法,如果爲true,則重定向到login.php。當用戶登錄時,由於模板中的「if」語句,「guest-user」div會消失。 – scopeak

回答

0

有一個終端會檢查用戶是否已登錄,並且可以根據需要處理響應。我已經包括下面的代碼段:

let xhttp = new XMLHttpRequest(); 
 
    
 
    xhttp.onreadystatechange = function() { 
 
     if (this.readyState == 4) { 
 
      if (this.status == 200) { 
 
       callback(JSON.parse(this.responseText)) 
 
      } else { 
 
       callback(null); 
 
      } 
 
     } 
 
    }; 
 
    
 
    xhttp.open('GET', "/customer/current.jwt?app_client_id=' + clientId"); 
 
    xhttp.send();

相關問題