JavaScript function works for some pages only

时间:2019-03-17 22:22:47

标签: javascript

I need some help. My application is written in node, express ejs. I have a function in separate javascript file that deletes cookies if the link to log out pressed. However, it works for only few pages and the rest no. When log out pressed user should be redirected to the index page and all cookies from the browser and database should be deleted. Database part works fine, redirection too, but cooikes are not deleted if user log out from certain pages. I noticed that this function only works for the pages where the route looks like "/", and doesn't work if it's "/:something", i don't know if it makes sense or no though. Here is my frontend.js file:

window.onload=function(){
   frontFunc.displayButtons();
   frontFunc.deleteCookie(); 

 }

const frontFunc = (function(){

 function displayButtons(){
     if (!document.cookie.split(';').filter((item) => item.includes('logedIn=')).length){ //returns 0 if  no login => false, therefore we need "!"

     let a = document.getElementsByClassName('account');
         for (let i = 0 ; i<a.length; i++){
           a[i].style.display='none';
         }
        // a[0].style.display='none';

         let b= document.getElementsByClassName('score');
         for (let i = 0 ; i<b.length; i++){
             b[i].style.display='none';
         }
        // b[0].style.display='none';
     }
      else{

         let a= document.getElementsByClassName('signIn');   
             for (let i = 0 ; i<a.length; i++){
            a[i].style.display='none';
        }

     }
 }

 function deleteCookie(){
     let a =document.getElementsByClassName('logOut');
     console.log("this is logout" +  a)
     for (let i=0;i<a.length;i++){
         console.log(i)
         a[i].addEventListener('click',function(){
             document.cookie = "logedIn=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
             document.cookie = "picName=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
             document.cookie = "time=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
             document.cookie = "admin=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
             window.location.reload();
         })
     }
 }


 return {
     displayButtons,
     deleteCookie,

     }

})();

Html is the same on all pages with the difference that on some pages logout is a dropdown item: <a class="dropdown-item logOut" href="/logOut">Log out</a> // this part works and on some it is a link: a class="nav-link text-white ml-2 logOut" href="/logOut" role="button">Log Out</a> //this doesn't work

JavaScript file is in script at the end of ejs file: <script src="/javascripts/frontend.js"></script>

Where is my mistake? I have routes like admin - goes to admin page and logout works as it should, but if i go to admin/users and try to log out from that page cookies are not deleted. I tried to put function to delete cookies in the script inside ejs file but it doesn't work either. No errors in console. Please help

Cookies are set in sign in function:

const signIn_post = async(req, res)=>{
    try{
     const db = req.app.locals.db; 
     let userIsFound= await db.collection('users').findOne({'userEmail': req.body.user_email});
     if (userIsFound === null){
         res.render('signIn', {mistakes:['Email or password is wrong'], success:[], emailEJS : req.body.user_email, passwordEJS : req.body.user_password})
     } else{
         let passwordMatch = req.body.user_password == userIsFound.password;
        let role = userIsFound.role;
         if (passwordMatch&&role==0){
             if(req.cookies.user === undefined || req.cookies.logedIn === undefined){
                 const randomID = uuidv4();
                 await db.collection('cookies').createIndex( { "createdAt":1 }, { expireAfterSeconds: 864000 });
                 await db.collection('cookies').insertOne({ cookieID:randomID, "createdAt": new Date(), userEmail: userIsFound.userEmail, userName: userIsFound.userName});
                 await db.collection('users').updateOne({"userEmail": userIsFound.userEmail},{ $set: {"about":randomID}});
                 res.cookie('user', randomID, { maxAge: 864000000, httpOnly: true });
                 res.cookie('logedIn','true', { maxAge: 864000000 });
                 res.render('index'); 
             }
             else {
                 res.send('Logged in '); 
                }; 
         }

0 个答案:

没有答案