logo
down
shadow

Change User default validation in Loopback


Change User default validation in Loopback

By : M.V.P
Date : November 22 2020, 02:59 PM
I hope this helps . Figured it out
In this case you need to remove the default validations set by the User model
code :
module.exports = function(UserCode){
   //Add this line and it will start receiving multiple email.
   delete UserCode.validations.email;
}
{
  "name": "UserCode",
  "base": "User",
  "idInjection": true,
  "properties": {
    "password": {
      "type": "string",
      "required": true
    },
    ....
    ....
}


Share : facebook icon twitter icon
can i change user model credentials in loopback?

can i change user model credentials in loopback?


By : Discout
Date : March 29 2020, 07:55 AM
it fixes the issue You can achieve that in two ways:
If not using username field in User model, you can use it to store mobile number, mobileNo. You have to edit user.js to accept mobileNo field as login credentials. User.login and User.normalizeCredentials are used for login process, so you can edit them like provided in the code snippet.
code :
`User.normalizeCredentials = function(credentials, realmRequired, realmDelimiter) {
    var query = {};
    credentials = credentials || {};
    if (!realmRequired) {
        if (credentials.email) {
            query.email = credentials.email;
        } else if (credentials.mobileNo) {
            query.mobileNo = credentials.mobileNo;
        }
    } else {
        if (credentials.realm) {
            query.realm = credentials.realm;
        }
        var parts;
        if (credentials.email) {
            parts = splitPrincipal(credentials.email, realmDelimiter);
            query.email = parts[1];
            if (parts[0]) {
                query.realm = parts[0];
            }
        } else if (credentials.mobileNo) {    //added mobileNo.
            parts = splitPrincipal(credentials.mobileNo, realmDelimiter);
            query.mobileNo = parts[1];
            if (parts[0]) {
                query.realm = parts[0];
            }
        }
    }
    return query;
};`
`User.login = function(credentials, include, fn) {
    .
    .
    .
    .
    .
    if (!query.email && !query.mobileNo) {
        var err2 = new Error('Mobile number or email is required');
        err2.statusCode = 400;
        err2.code = 'MOBILE_NUMBER_EMAIL_REQUIRED';
        fn(err2);
        return fn.promise;
    }
}`
Set User Role in loopback using loopback-component-passport into inbuilt models

Set User Role in loopback using loopback-component-passport into inbuilt models


By : Hodgson
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I am trying to create user in loopback using npm loopback-component-passport with user roles based on a condition, , Initially create a file and add roles in DB
server/boot/role.js:
code :
module.exports = function(app) {
  var Role = app.models.Role;
  Role.create([
      {name: 'admin'},
      {name: 'user'}]
    , function(err, role) {
      if (err) throw err;
      console.log('Created roles:', role);
    });
};
module.exports = function(app) {
  var User = app.models.user;
  var Role = app.models.Role;
  var RoleMapping = app.models.RoleMapping;
  User.observe('after save', function setDefaultUsername(ctx, next) {
    if (ctx.instance) {
      if (ctx.isNewInstance) {
        var userRole = ctx.instance.isAdmin ? 'admin' : 'user';
        Role.findOne({where: {name: userRole}}, function(err, role) {
          if (err) {
            return console.log(err);
          }
          RoleMapping.create({
            principalType: RoleMapping.USER,
            principalId: ctx.instance.id,
            roleId: role.id
          }, function(err, roleMapping) {
            if (err) {
              return console.log(err);
            }
          });
        });
      }
    }
    next();
  });
};
Change the default order of retrieval in loopback

Change the default order of retrieval in loopback


By : Baimon Hnonhmai
Date : March 29 2020, 07:55 AM
With these it helps You can use the order filter in several ways, on your :
REST API :
code :
filter[order]=createdAt <ASC|DESC>
filter[order][0]=createdAt <ASC|DESC>&filter[order][1][updatedAt]=<ASC|DESC>...
model.find({
  order: 'createdAt DESC',
});
"scope": {
    "order": "createdAt ASC",
    "where": {
      "field": "something" // you can use the where filter also ...
    }
  },
How to override the default password hashing method and validation method of loopback?

How to override the default password hashing method and validation method of loopback?


By : Nguyen Van Hien
Date : March 29 2020, 07:55 AM
Does that help You can override User.hashPassword to implement your own hashing method.
Let's say you have a model Customer which has User as base model,
Change loopback default model User to Member

Change loopback default model User to Member


By : Shawn
Date : October 02 2020, 09:00 AM
To fix the issue you can do You can expand the built-in model User: https://loopback.io/doc/en/lb3/Extending-built-in-models.html
code :
server/models/Member.json

{
  "name": "Member",
  "base": "User",
...  
} 
shadow
Privacy Policy - Terms - Contact Us © animezone.co