I am using waterlock for sails.js to manage my users. I want them to have a unique username so I have this in my User.js:
module.exports = {
autoPk : false,
attributes: require('waterlock').models.user.attributes({
id : {
type : "string",
primaryKey : true,
unique : true
}
}),
beforeCreate: require('waterlock').models.user.beforeCreate,
beforeUpdate: require('waterlock').models.user.beforeUpdate
};
My Auth.js looks like this:
module.exports = {
attributes: require('waterlock').models.auth.attributes({
username : {
type : "string",
unique : true
}
}),
beforeCreate: require('waterlock').models.auth.beforeCreate,
beforeUpdate: require('waterlock').models.auth.beforeUpdate
};
And I am creating users like this:
auth = {
username: params.username,
password: params.password
};
userObj = {
id: params.username
};
User.create(userObj)
.exec(function (err, user) {
if (err) {
sails.log.error("USER: " + err);
req.session.flash = {
err: err
};
return res.json(err);//res.redirect('/user/create');
} else {
req.session.user = user;
waterlock.engine.attachAuthToUser(auth, user, function (err) {
if (err) {
sails.log.error("AUTH: " + err);
return res.json(err);
} else {
req.session.authenticated = true;
return res.json({ok: true});
}
});
}
});
Obviously I want the creation of a user to fail when one with the same id already exists. However, the creation of the user works and the creation of the auth fails because of the unique constraint on username. But once this fails I already have two users with the same ID in my database, with no auth attached. Why is this happening? I am using node v0.12.1, sails 0.11 and waterlock 0.14. The adapter I am using is the built-in sails-disk with {migrate : "alter"}. Another strange thing is: When I restart sails, it asks me if it should remove duplicates in the user table. Why does it recognize them then but not when I create the user? Thank you for your help, Alexander