-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Awk34
merged 12 commits into
angular-fullstack:canary
from
kingcody:feature/auth-module
Sep 16, 2015
Merged
Feature: auth module #1309
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6147e76
refactor(client:auth): use named function, create Auth reference
kingcody 9133296
feat(injector): inject module definitions first
kingcody d3d0f56
feat(client:auth): implement auth components as a seperate module
kingcody f350412
refactor(client:auth): move authInterceptor to auth module
kingcody 533f20e
feat(client:auth): implement `Auth.hasRole`
kingcody f222cfb
feat(client:auth): implement router auth via router.decorator
kingcody cf38d62
refactor(client:auth): use named function for user resource
kingcody dafd65a
fix(client:auth): exclued user fetching for logout route
kingcody 650a529
style(app): flatten IIFEs
kingcody 2fab1f5
refactor(client:auth) simplify `authInterceptor.responseError`
kingcody 3c29832
feat(app): implement util module for thin, reusable functions
kingcody abcb51a
fix(client:auth): restrict `Authorization` header to same origin
kingcody File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
'use strict'; | ||
|
||
(function() { | ||
|
||
function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) { | ||
var self = this; | ||
this.awesomeThings = []; | ||
|
||
$http.get('/api/things').then(function(response) { | ||
self.awesomeThings = response.data;<% if (filters.socketio) { %> | ||
socket.syncUpdates('thing', self.awesomeThings);<% } %> | ||
}); | ||
<% if (filters.models) { %> | ||
this.addThing = function() { | ||
if (self.newThing === '') { | ||
return; | ||
} | ||
$http.post('/api/things', { name: self.newThing }); | ||
self.newThing = ''; | ||
}; | ||
|
||
this.deleteThing = function(thing) { | ||
$http.delete('/api/things/' + thing._id); | ||
};<% } %><% if (filters.socketio) { %> | ||
|
||
$scope.$on('$destroy', function() { | ||
socket.unsyncUpdates('thing'); | ||
});<% } %> | ||
} | ||
|
||
angular.module('<%= scriptAppName %>') | ||
.controller('MainController', MainController); | ||
function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) { | ||
var self = this; | ||
this.awesomeThings = []; | ||
|
||
$http.get('/api/things').then(function(response) { | ||
self.awesomeThings = response.data;<% if (filters.socketio) { %> | ||
socket.syncUpdates('thing', self.awesomeThings);<% } %> | ||
});<% if (filters.models) { %> | ||
|
||
this.addThing = function() { | ||
if (self.newThing === '') { | ||
return; | ||
} | ||
$http.post('/api/things', { name: self.newThing }); | ||
self.newThing = ''; | ||
}; | ||
|
||
this.deleteThing = function(thing) { | ||
$http.delete('/api/things/' + thing._id); | ||
};<% } if (filters.socketio) { %> | ||
|
||
$scope.$on('$destroy', function() { | ||
socket.unsyncUpdates('thing'); | ||
});<% } %> | ||
} | ||
|
||
angular.module('<%= scriptAppName %>') | ||
.controller('MainController', MainController); | ||
|
||
})(); | ||
11 changes: 11 additions & 0 deletions
11
app/templates/client/components/auth(auth)/auth.module(js).js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
angular.module('<%= scriptAppName %>.auth', [ | ||
'<%= scriptAppName %>.constants', | ||
'ngCookies'<% if (filters.ngroute) { %>, | ||
'ngRoute'<% } if (filters.uirouter) { %>, | ||
'ui.router'<% } %> | ||
]) | ||
.config(function($httpProvider) { | ||
$httpProvider.interceptors.push('authInterceptor'); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/templates/client/components/auth(auth)/interceptor.service(js).js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
(function() { | ||
|
||
function authInterceptor($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); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
angular.module('<%= scriptAppName %>.auth') | ||
.factory('authInterceptor', authInterceptor); | ||
|
||
})(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should keep this tabbed the way it was. The IIFE shouldn't mess with our code's formatting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure...