JavaScript:从用户页面注销时不会清除Cookie

时间:2016-08-29 03:36:06

标签: javascript cookies

我正在使用Cookie来保持用户的信息出现在前端。通过Passport进行记录。

登录时,代码会根据Passport返回的用户信息设置Cookie。

注销时,未设置cookie,然后调用/auth/logout来记录用户。

所有内容都适用于登录/注销,但每当我尝试在注销时删除cookie时,都不会发生任何事情。

这仅发生在我网站的user.js页面。我能够从任何其他页面成功注销

这是我的代码:

动态设置导航栏

document.getElementById( 'navigation-bar' ).innerHTML = 
'<ul>'+
  "<li><button onclick='Home();' id='home-link'>Home</button></li>"+
  "<li><a id='recipes-link' href='/recipes'>Recipes</a></li>"+
  "<li><a id='festivals-link' href='/festivals'>Festivals</a></li>"+
  "<li><button id='logout-button' onclick='Logout()'>Logout</button></li>"+
 '</ul>';

退出功能

function Logout() {

        //Sets each cookie's expire time to 1970, forcing expire.
        DeleteCookies();

        //Runs Passport's 'logout' function
        window.open( '/auth/logout',  '_parent' );

}

设置Cookie

function Login () {

    //Setting user parameters.
    let login = { email: null, password: null };

    login.email = document.getElementById( 'email' ).value;
    login.password = document.getElementById( 'password' ).value;

    PostRequest( '/auth/login', login, 'application/json', ( status, data ) => {

        if ( status == 200 ) {
            //Set 'expireTime' to current date and then add 1 hour to it.
            let expireTime = new Date( Date.now() );
            expireTime.setHours( expireTime.getHours() + 1 );

            // Parse the return data.
            let user = JSON.parse(data);

            //Set cookie for each user key. 
            for ( const key in user ) {
                document.cookie = key + '=' + user[key] + ';expires=' + expireTime.toUTCString() + ";";
            }

            //Open user's profile in another window using name as url.
            window.open( '/user/' + user.username, '_parent' );
        } else {
            console.log('error');
        }

    });

}

删除Cookie

function DeleteCookies() {
    console.log( 'this is running' );

    //Use old date to expire the cookies
    let expire = 'expires=Thu, 01 Jan 1970 00:00:00 GMT;'

    //Split each cookie into an array element.
    let cookiesArray = document.cookie.split(";");

    //Split the cookie to have name as the first value.
    //Then use the cookie's name to expire the cookie.
    for ( let cookie = 0; cookie < cookiesArray.length; cookie++ ) {
        let name = cookiesArray[cookie].split( "=" )[0];
        document.cookie = name + "=;" + expire;
    }

}

检查Cookie是否已设置

function IsLoggedIn() {

    //If user is logged in, display the logout button.
    if (  GetCookie( '_id' ) != '' ) {
        document.getElementById( 'logout-button' ).style.display = 'inline-block';
    }

}

user.js代码

var user = {};

function UserReady() {

    GetRequest( '/api/me', undefined, ( status, data ) => {

        if ( status === 200 ) {
            user = JSON.parse( data );
            document.getElementById( 'username' ).innerHTML = "Welcome back, " + user.username;
        }

    });

}

感谢所有帮助。

0 个答案:

没有答案