简体   繁体   中英

nodejs read session from database

Im writing an application using nodejs, nowjs, codeigniter and mysql. Im using database to store the session. Is there a good way to check and retrieve the session cookie in nodejs? So if that for example, i need to check if a user owns a specific row in the database that is about to get deleted, using nowjs. eg:

server:

//some code here to get session cookie and retrieve stored data from database

var session = { user_id: //userid from database }

everyone.now.deleteGuestbook = function(recordId) {
    var owner_id = client.query(
        'SELECT owner_id FROM guestbook WHERE id = ?',
        [recordId]
    );
    if (session.user_id === owner_id) {
        client.query(
        'DELETE FROM guestbook WHERE id = ?',
        [recordId], function() {
                 everyone.now.deleteRecord;
            }
        );
    }
}

client:

//Delete guestbook record
$('#guestbook_records li').live('click', function(e) {
    if ($(e.target).is('a.delete')) {
        var id = $(this).find('a.delete').attr('href');
        dialogBox('warning', 'Are you sure you want to delete the record?',
            function() {
                now.deleteGuestbook(id);
            });
    }
    else {
        return;
    }
    return false;
});

now.deleteMessage = function(id) {
    $('li[class="' + id + '"]').slideUp(400, function() {
        $(this).remove();
    });
};

Session ids are typically preserved in client cookies which are surfaced in httpServer handlers as request.headers.cookie.

You'd need to know the name of the cookie containing the session id and then:

var cookies=(function(str){                     # create hash from cookie string
      var result={};
      str.split(/;\s+/).forEach(function(e){
        var parts=e.split(/=/,2);
        result[parts[0]]=parts[1]||'';
      });
      return result;
    })(request.headers.cookie),
    sessionCookieName='session',                # name of cookie holding session id
    sessionId=cookies[sessionCookieName]||'';   # session id or ''

Your client code is messy

// live is bad and inefficient
$('#guestbook_records li').live('click', function(e) {
    // this kind of check seems horrid
    if ($(e.target).is('a.delete')) {
        // not very optimum to find it. 
        var id = $(this).find('a.delete').attr('href');
        dialogBox('warning', 'Are you sure you want to delete the record?',
            function() {
                now.deleteGuestbook(id);
            });
    }
    else {
        // why return here?
        return;
    }
    // return false should be in if block.
    return false;
});

Try this instead

$("#guestbook_records li a.delete").click(function(e) {
    var id = this.href;
    dialogBox("warning", "Are you sure you want to delete the record?",
        function() {
             now.deleteGuesBook(id);
        });
    e.preventDefault();
}

Also .hasClass is better

now.deleteMessage = function(id) {
    $('li').hasClass(id).slideUp(400, function() {
        $(this).remove();
    });
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM