Skip to content

Feature: auth module #1309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ export default class Generator extends Base {
if(this.filters.socketio) angModules.push("'btford.socket-io'");
if(this.filters.uirouter) angModules.push("'ui.router'");
if(this.filters.uibootstrap) angModules.push("'ui.bootstrap'");
if(this.filters.auth) angModules.push("'validation.match'");
if(this.filters.auth) {
angModules.unshift(`'${this.scriptAppName}.auth'`);
angModules.push("'validation.match'");
}

this.angularModules = '\n ' + angModules.join(',\n ') +'\n';
}
Expand Down
7 changes: 7 additions & 0 deletions app/templates/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,13 @@ module.exports = function (grunt) {
filePath = filePath.replace('/.tmp/', '');
return '<script src="' + filePath + '"></script>';
},
sort: function(a, b) {
var module = /\.module\.js$/;
var aMod = module.test(a);
var bMod = module.test(b);
// inject *.module.js first
return (aMod === bMod) ? 0 : (aMod ? -1 : 1);
},
starttag: '<!-- injector:js -->',
endtag: '<!-- endinjector -->'
},
Expand Down
6 changes: 4 additions & 2 deletions app/templates/client/app/admin(auth)/admin(js).js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ angular.module('<%= scriptAppName %>')
$routeProvider
.when('/admin', {
templateUrl: 'app/admin/admin.html',
controller: 'AdminCtrl'
controller: 'AdminCtrl',
authenticate: 'admin'
});
});<% } %><% if (filters.uirouter) { %>.config(function($stateProvider) {
$stateProvider
.state('admin', {
url: '/admin',
templateUrl: 'app/admin/admin.html',
controller: 'AdminCtrl'
controller: 'AdminCtrl',
authenticate: 'admin'
});
});<% } %>
56 changes: 5 additions & 51 deletions app/templates/client/app/app(js).js
Original file line number Diff line number Diff line change
@@ -1,59 +1,13 @@
'use strict';

angular.module('<%= scriptAppName %>', [<%- angularModules %>])
<% if (filters.ngroute) { %>.config(function($routeProvider, $locationProvider<% if (filters.auth) { %>, $httpProvider<% } %>) {
.config(function(<% if (filters.ngroute) { %>$routeProvider<% } if (filters.uirouter) { %>$urlRouterProvider<% } %>, $locationProvider) {<% if (filters.ngroute) { %>
$routeProvider
.otherwise({
redirectTo: '/'
});

$locationProvider.html5Mode(true);<% if (filters.auth) { %>
$httpProvider.interceptors.push('authInterceptor');<% } %>
})<% } if (filters.uirouter) { %>.config(function($stateProvider, $urlRouterProvider, $locationProvider<% if (filters.auth) { %>, $httpProvider<% } %>) {
});<% } if (filters.uirouter) { %>
$urlRouterProvider
.otherwise('/');

$locationProvider.html5Mode(true);<% if (filters.auth) { %>
$httpProvider.interceptors.push('authInterceptor');<% } %>
})<% } if (filters.auth) { %>

.factory('authInterceptor', function($rootScope, $q, $cookies<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $injector<% } %>) {
<% if (filters.uirouter) { %>var state;
<% } %>return {
// Add authorization token to headers
request: function(config) {
config.headers = config.headers || {};
if ($cookies.get('token')) {
config.headers.Authorization = 'Bearer ' + $cookies.get('token');
}
return config;
},

// Intercept 401s and redirect you to login
responseError: function(response) {
if (response.status === 401) {
<% if (filters.ngroute) { %>$location.path('/login');<% } if (filters.uirouter) { %>(state || (state = $injector.get('$state'))).go('login');<% } %>
// remove any stale tokens
$cookies.remove('token');
return $q.reject(response);
}
else {
return $q.reject(response);
}
}
};
})
.otherwise('/');<% } %>

.run(function($rootScope<% if (filters.ngroute) { %>, $location<% } if (filters.uirouter) { %>, $state<% } %>, Auth) {
// Redirect to login if route requires auth and the user is not logged in
$rootScope.$on(<% if (filters.ngroute) { %>'$routeChangeStart'<% } %><% if (filters.uirouter) { %>'$stateChangeStart'<% } %>, function(event, next) {
if (next.authenticate) {
Auth.isLoggedIn(function(loggedIn) {
if (!loggedIn) {
event.preventDefault();
<% if (filters.ngroute) { %>$location.path('/login');<% } if (filters.uirouter) { %>$state.go('login');<% } %>
}
});
}
});
})<% } %>;
$locationProvider.html5Mode(true);
});
7 changes: 4 additions & 3 deletions app/templates/client/app/main/main.controller(js).js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

(function() {

function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) {
Expand All @@ -8,8 +9,8 @@ function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %
$http.get('/api/things').then(function(response) {
self.awesomeThings = response.data;<% if (filters.socketio) { %>
socket.syncUpdates('thing', self.awesomeThings);<% } %>
});
<% if (filters.models) { %>
});<% if (filters.models) { %>

this.addThing = function() {
if (self.newThing === '') {
return;
Expand All @@ -20,7 +21,7 @@ function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %

this.deleteThing = function(thing) {
$http.delete('/api/things/' + thing._id);
};<% } %><% if (filters.socketio) { %>
};<% } if (filters.socketio) { %>

$scope.$on('$destroy', function() {
socket.unsyncUpdates('thing');
Expand Down
12 changes: 12 additions & 0 deletions app/templates/client/components/auth(auth)/auth.module(js).js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

angular.module('<%= scriptAppName %>.auth', [
'<%= scriptAppName %>.constants',
'<%= scriptAppName %>.util',
'ngCookies'<% if (filters.ngroute) { %>,
'ngRoute'<% } if (filters.uirouter) { %>,
'ui.router'<% } %>
])
.config(function($httpProvider) {
$httpProvider.interceptors.push('authInterceptor');
});
Loading