Overview
Loopback provides useful built-in models for common use cases:
- Application model - contains metadata for a client application that has its own identity and associated configuration with the LoopBack server.
- User model - register and authenticate users of your app locally or against third-party services.
- Access control models - ACL, AccessToken, Scope, Role, and RoleMapping models for controlling access to applications, resources, and methods.
- Email model - send emails to your app users using SMTP or third-party services.
The built-in models (except for Email) extend PersistedModel, so they have automatically have a full complement of create, update, and delete (CRUD) operations.
Application model
Use the Application model to manage client applications and organize their users.
User model
See Managing users.
Access control models
Use access control models to control access to applications, resources, and methods. These models include:
ACL model
An ACL model connects principals to protected resources. The system grants permissions to principals (users or applications, that can be grouped into roles) .
- Protected resources: the model data and operations (model/property/method/relation)
- Is a given client application or user allowed to access (read, write, or execute) the protected resource?
Creating a new ACL instance.
ACL.create( {principalType: ACL.USER, principalId: 'u001', model: 'User', property: ACL.ALL, accessType: ACL.ALL, permission: ACL.ALLOW}, function (err, acl) { ACL.create( {principalType: ACL.USER, principalId: 'u001', model: 'User', property: ACL.ALL, accessType: ACL.READ, permission: ACL.DENY}, function (err, acl) { } }) } })
Email model
Set up an email data source by adding an entry to /server/datasources.json
, such as the following (for example):
{ ... "myEmailDataSource": { "connector": "mail", "transports": [{ "type": "smtp", "host": "smtp.private.com", "secure": false, "port": 587, "tls": { "rejectUnauthorized": false }, "auth": { "user": "me@private.com", "pass": "password" } }] } ... }
See Email connector for more information on email data sources.
Then, reference the data source in /server/model-config.json
as follows (for example):
{ ... "Email": { "dataSource": "myEmailDataSource", }, ... }
Send email messages
The following example illustrates how to send emails from an app. Add the following code to a file in the /models
directory:
module.exports = function(MyModel) { // send an email MyModel.sendEmail = function(cb) { MyModel.app.models.Email.send({ to: 'foo@bar.com', from: 'you@gmail.com', subject: 'my subject', text: 'my text', html: 'my <em>html</em>' }, function(err, mail) { console.log('email sent!'); cb(err); }); } };