2017-07-04 37 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();