git clone git@github.com:strongloop/loopback-example-access-control.git
cd loopback-example-access-control/server
npm installMake sure you are in the server directory!
node appTo install the latest version of slc:
npm install strong-cli -gTo check your version of slc:
slc versionShould print something similar to:
slc v2.1.0 (node v0.10.22)
mkdir -p access-control/client
cd access-control
slc lb project servercd server
slc lb model bankslc lb model accountslc lb model transactionSee the models.json file for the relations. Below is an example.
...
"user": {
"options": {
"base": "User",
"relations": {
"accessTokens": {
"model": "accessToken",
"type": "hasMany",
"foreignKey": "userId"
},
"account": {
"model": "account",
"type": "belongsTo"
},
"transactions": {
"model": "transaction",
"type": "hasMany"
}
},
...
slc lb acl --all-models --deny --everyoneslc lb acl --allow --everyone --read --model bank
slc lb acl --allow --everyone --method create --model user
slc lb acl --allow --owner --all --model user
slc lb acl --allow --owner --read --model account
slc lb acl --allow --owner --write --model accountSee the actual source. Below is a basic example.
// in client/js/services.js
angular.module('starter.services', ['ngResource'])
.factory('User', ['$resource', function($resource) {
return $resource('/api/users/:id', {id: '@id'}, {
login: {
method: 'POST',
url: '/api/users/login'
},
logout: {
method: 'POST',
url: '/api/users/logout'
}
});
}])
.config(function ($httpProvider) {
$httpProvider.interceptors.push('requestInterceptor');
})
.factory('requestInterceptor', function ($q, $rootScope) {
return {
'request': function (config) {
console.log('config', config);
if($rootScope.accessToken) {
config.headers.authorization = $rootScope.accessToken;
}
return config || $q.when(config);
}
}
});See the full source. Below is a basic login / register controller.
.controller('LoginCtrl', function($rootScope, $scope, $routeParams, User, $location) {
$scope.registration = {};
$scope.credentials = {};
$scope.login = function() {
$scope.loginResult = User.login($scope.credentials,
function() {
$rootScope.accessToken = $scope.loginResult.id;
$rootScope.currentUserId = $scope.loginResult.userId;
$location.path('/');
},
function(res) {
$scope.loginError = res.data.error;
}
);
}
$scope.register = function() {
$scope.user = User.save($scope.registration,
function() {
// success
},
function(res) {
$scope.registerError = res.data.error;
}
);
}
});

