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

feat : rework activation #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/passport-local-sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ var defaultAttachOptions = {
usernameField: 'username',
usernameLowerCase: false,
activationRequired: false,
activationPasswordRequired : true,
hashField: 'hash',
saltField: 'salt',
activationKeyField: 'activationKey',
verifiedField : 'verified',
resetPasswordKeyField: 'resetPasswordKey',
incorrectPasswordError: 'Incorrect password',
incorrectUsernameError: 'Incorrect username',
Expand All @@ -28,6 +30,7 @@ var defaultAttachOptions = {
missingPasswordError: 'Password argument not set!',
userExistsError: 'User already exists with %s',
activationError: 'Email activation required',
activationRequiredError : 'Your account needs to be activated',
noSaltValueStoredError: 'Authentication not possible. No salt value stored in db!'
};

Expand Down Expand Up @@ -142,7 +145,11 @@ var attachToUser = function (UserSchema, options) {
var hash = new Buffer(hashRaw, 'binary').toString('hex');

if (hash === self.get(options.hashField)) {
if (options.activationRequired && ( self.get(options.verifiedField) === false || self.get(options.activationKeyField) !== null )) {
return cb(null, false, {message : options.activationRequiredError});
} else {
return cb(null, self);
}
} else {
return cb(null, false, { message: options.incorrectPasswordError });
}
Expand Down Expand Up @@ -229,6 +236,7 @@ var attachToUser = function (UserSchema, options) {
};

UserSchema.activate = function (username, password, activationKey, cb) {
if (options.activationPasswordRequired === true){
var self = this;
var auth = self.authenticate();
auth(username, password, function (err, user, info) {
Expand All @@ -249,6 +257,28 @@ var attachToUser = function (UserSchema, options) {
return cb({ message: options.invalidActivationKeyError });
}
});
} else {
this.findByUsername(username, function(err, user, info) {

if (err) { return cb(err); }

if (!user) { return cb(info); }

if (user.get(options.activationKeyField) === activationKey) {
user.set(options.activationKeyField, null);
user.set(options.verifiedField, true);
user.save()
.then(function() {
return cb(null, user);
})
.catch(function (err) {
return cb(err);
});
} else {
return cb({ message: options.invalidActivationKeyError });
}
});
}
};

UserSchema.findByUsername = function (username, cb) {
Expand Down