Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When Username Is Updated, User Is Logged Out #46

Open
austencollins opened this issue Jun 3, 2014 · 1 comment
Open

When Username Is Updated, User Is Logged Out #46

austencollins opened this issue Jun 3, 2014 · 1 comment
Labels

Comments

@austencollins
Copy link

What is the correct way to update a record? Right now, whenever I update a username, the user is logged out:

http://stackoverflow.com/questions/24023443/passport-local-mongoose-when-i-update-a-records-username-im-logged-out-why

@austencollins austencollins changed the title When Usernames Is Updated, User Is Logged Out When Username Is Updated, User Is Logged Out Jun 3, 2014
@saintedlama
Copy link
Owner

The reason for this behavior is the serialize/deserialize implementation shipped with passport-local-mongoose:

schema.statics.serializeUser = function() {
    return function(user, cb) {
        cb(null, user.get(options.usernameField));
    }
};

schema.statics.deserializeUser = function() {
    var self = this;

    return function(username, cb) {
        self.findByUsername(username, cb);
    }
};

This implementation uses the username field for serialization and deserialization. As a consequence a change to the username will fail if the username value changed. You can prevent this behavior by using a custom serialization/deserialization strategy like this:

schema.statics.serializeUser = function() {
    return function(user, cb) {
        cb(null, user.id);
    }
};

schema.statics.deserializeUser = function() {
    var self = this;

    return function(id, cb) {
        self.findOne(id, cb);
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants