trailsjs / sails-auth

Passport-based User Authentication system for sails.js applications. Designed to work well with the sails-permissions module.
https://www.npmjs.org/package/sails-auth
MIT License
265 stars 141 forks source link

How to add fields to User model? #119

Open alerdenisov opened 8 years ago

alerdenisov commented 8 years ago

I need to profile and extra data on User, but doesn't want to change User model in module. What i need to do?

DaAwesomeP commented 8 years ago

I don't know what you mean by not change the User model. That's definitely the easiest way to do it:

// api/models/User.js

var _ = require('lodash');
var _super = require('sails-permissions/api/models/User');

_.merge(exports, _super);
_.merge(exports, {

  // Extend with custom logic here by adding additional fields, methods, etc.
  attributes: {
    name: {
      type: 'string'
    },
    some_other_data: {
      type: 'string'
    }
  }
});
po5i commented 8 years ago

+1 for @DaAwesomeP solution

aman-gautam commented 8 years ago

@DaAwesomeP Absolutely! This can be the way to go.

var _super = require('sails-auth/api/models/User'); // Dev may not be using sails-permissions

@alerdenisov, If you really don't want to add profile data in User table, you can create a one-to-one mapping between your User model and Profile model. Check this link to see how to create one-to-one associations.

PS: For creating a separate model as well, you will have to create a one-to-one mapping field in the User and Profile Models. For adding the field, you will have to extend the User model as DaAwesomeP suggested. You're out of luck there IMO!

trex005 commented 6 years ago

@aman-gautam while you have a very good point, how do you set the profile that the autogenerated admin uses?