5

I'm using the Passport authentication plugin for KeystoneJS in my project.

Everything works great if the user account exists and is tied to the social network being used. When creating a new user, though, with the config option 'auto create user': true, I end up with a 500 error on the oauth callback route. The log says it's a validation error.

ValidationError: Validation failed 
  at model.Document.invalidate (/app/node_modules/keystone/node_modules/mongoose/lib/document.js:1021:32) 
  at /app/node_modules/keystone/node_modules/mongoose/lib/document.js:970:16 
  at validate (/app/node_modules/keystone/node_modules/mongoose/lib/schematype.js:610:7) 
  at /app/node_modules/keystone/node_modules/mongoose/lib/schematype.js:627:9 
  at Array.forEach (native) 
  at SchemaString.SchemaType.doValidate (/app/node_modules/keystone/node_modules/mongoose/lib/schematype.js:614:19) 
  at /app/node_modules/keystone/node_modules/mongoose/lib/document.js:968:9 
  at process._tickCallback (node.js:419:13)

What could be causing this?


Edit

User model:

var keystone = require('keystone'),
Types = keystone.Field.Types,
social = require('keystone-social-login');

/**
 * User Model
 * ==========
 */

var User = new keystone.List('User');

User.add({
    name: { type: Types.Name, required: true, index: true },
    email: { type: Types.Email, initial: true, required: true, index: true },
    password: { type: Types.Password, initial: true, required: true }
}, 'Permissions', {
    userLevel: { type: Types.Select, options: 'user, business, admin', default: 'user', initial: true }
});

// Provide access to Keystone
User.schema.virtual('canAccessKeystone').get(function() {
    return this.userLevel === 'admin';
});


/**
 * Registration
 */

social.plugin(User);

User.defaultColumns = 'name, email, userLevel';
User.register();
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
Steve
  • 569
  • 8
  • 20
  • 2
    It means that [Mongoose validation](http://mongoosejs.com/docs/validation.html) have failed on some field. Try printing [`errors` property of your `ValidationError` object](http://mongoosejs.com/docs/api.html#document_Document-invalidate) to find out what's wrong with your user. – Leonid Beschastny Nov 23 '14 at 15:39
  • Any console.log added to the plugin is ignored. I just get the same 500 error page and the same console errors. I don't see how I can output the errors from the object. – Steve Nov 23 '14 at 16:15
  • Can you show the code of your user model? – lukaszfiszer Nov 25 '14 at 01:15
  • 1
    Thanks. It look all right. So as @LeonidBeschastny said, we need to be able to see `errors` property of `ValidationError` object. Could you add `console.log(err.errors)` inside 500 error handler in the router file (normally `routes/index.js`), like this: `keystone.set('500', function(err, req, res, next) { (...) console.log(err.errors) res.err(err, title, message); });` – lukaszfiszer Nov 25 '14 at 01:47
  • 1
    @lukaszfiszer You, sir, are a godsend! `ValidatorError: Path 'password' is required.` -- I had no idea how to access this error before. Thank you! – Steve Nov 25 '14 at 02:48
  • That looks like a bug in keystone-social-login module, because it makes not sense to require `password` field when user authenticates via an external provider. Removing `required: true` from password field should help. I recommend doing the same for `email` if you want to use Twitter authentication, as twitter does not give access to users' email via their oAuth. – lukaszfiszer Nov 25 '14 at 03:00
  • 1
    Good point. Thank you again. I will make those changes and cross my fingers :) – Steve Nov 25 '14 at 03:30
  • This error frequently appears when you put something on an Enum and then a default value that doesn't belong to the enum list. – Crisboot Oct 14 '15 at 18:33

0 Answers0