Build a blog with Areto Node.js framework

User model

User model class inherits the User class that provides authentication of the user interface, and the areto/db/ActiveRecord class that manages the database.

models/User.js

'use strict';
const Base = require('areto/web/UserIdentity');
module.exports = class User extends Base {
  // place methods here
};
module.exports.init(module);
const security = require('areto/helpers/SecurityHelper');

Define static properties of the class in getConstants method.

  • TABLE - is the name of the table that stores the model.
  • STORED_ATTRS - this model attributes to be stored in the database. The _id attribute is used as a model identifier. It is created by MongoDB for each new record.

Static properties are available both through the class itself, and an instance: User.TABLE === (new User).TABLE === (new User).constructor.TABLE.

The init method is used to initialize static properties. It is called immediately after the class definition. Its argument is the current file module Node.js: module.exports.init(module).

models/User.js

...
static getConstants () {
  return {
    TABLE: 'user',
    STORED_ATTRS: ['name', 'email', 'role', 'status', 'passwordHash', 'authKey']
  };
}
...

The findIdentity method searches the database record by ID id and status (status === 'active'). It is necessary to search for the authenticated user according to the data stored in the session.

models/User.js

...
static findIdentity (id) {
 return this.findById(id).andWhere({status: 'active'});
}
...

The init method is called immediately after you create the model. It provides an initialization of attribute values. If you override it, then call the parent class method to work properly super.init.

models/User.js

...
init () {
  super.init();
  this.set('role', 'reader');
  this.set('status', 'active');
}
...

The getTitle method returns the name of the model to be displayed in different cases. By default, it uses the model ID.

models/User.js

...
getTitle () {
  return this.get('name');
}
...

The isActive, isBanned methods used to check the current status of the user.

models/User.js

...
isActive () {
    return this.get('status') === 'active';
}

isBanned () {
    return this.get('status') === 'banned';
}
...

The getAssignments method returns the user role stored in the role attribute that is needed to authorize access to resources of the blog.

models/User.js

...
getAssignments (cb) {
  cb(null, [this.get('role')]);
}
...

The beforeSave method is called before saving the model. It is asynchronous, so at the end of the work is to be called cb. Also, if you override it, then you must call the super.beforeSave() parent asynchronous method.

models/User.js

...
beforeSave (cb, insert) {
  super.beforeSave(err => {
    if (err) {
      return cb(err);
    }
    this.setPasswordHash();
    if (insert) {
      this.setAuthKey(cb)
    } else {
      cb();
    }
  }, insert);
}
...

The last couple of methods manage user password. To do this, connect the areto/helpers/Security helper class after User initialization. Only the sha1 password hash is stored in the database.

models/User.js

...
validatePassword (password) {
  return security.validatePassword(password, this.get('passwordHash'));
}

setPasswordHash () {
  if (this.get('password')) {
    this.set('passwordHash', security.encryptPassword(this.get('password')));
  }
}
...