git clone [email protected]:strongloop/loopback-example-access-control.git
cd loopback-example-access-control/server
npm install
Make sure you are in the server directory!
node app
To install the latest version of slc
:
npm install strong-cli -g
To check your version of slc
:
slc version
Should print something similar to:
slc v2.1.0 (node v0.10.22)
mkdir -p access-control/client
cd access-control
slc lb project server
cd server
slc lb model bank
slc lb model account
slc lb model transaction
See 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 --everyone
slc 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 account
See 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;
}
);
}
});