Skip to content

Commit d0c5607

Browse files
author
Peter Shafer
committed
Merge latest changes from master.
2 parents 3e22d2e + 939af5e commit d0c5607

File tree

16 files changed

+253
-56
lines changed

16 files changed

+253
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ Alternatively, you can change these settings from a controller by changing the d
3737
Todo
3838
----
3939

40-
Add ability to extend data that's passed with API calls.
40+
Add support for JWT (JSON web tokens)

app/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ <h3 class="text-muted">Angular Django Registration Auth Demo</h3>
7878
<script src="scripts/services/djangoAuth.js"></script>
7979
<script src="scripts/services/validate.js"></script>
8080
<script src="scripts/controllers/master.js"></script>
81+
<script src="scripts/controllers/restricted.js"></script>
82+
<script src="scripts/controllers/authrequired.js"></script>
8183
<!-- endbuild -->
8284

8385
</body>

app/scripts/app.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,99 @@ angular.module('angularDjangoRegistrationAuthApp', [
1010
$routeProvider
1111
.when('/', {
1212
templateUrl: 'views/main.html',
13-
controller: 'MainCtrl'
13+
controller: 'MainCtrl',
14+
resolve: {
15+
authenticated: ['djangoAuth', function(djangoAuth){
16+
return djangoAuth.authenticationStatus();
17+
}],
18+
}
1419
})
1520
.when('/register', {
1621
templateUrl: 'views/register.html',
22+
resolve: {
23+
authenticated: ['djangoAuth', function(djangoAuth){
24+
return djangoAuth.authenticationStatus();
25+
}],
26+
}
1727
})
1828
.when('/passwordReset', {
1929
templateUrl: 'views/passwordreset.html',
30+
resolve: {
31+
authenticated: ['djangoAuth', function(djangoAuth){
32+
return djangoAuth.authenticationStatus();
33+
}],
34+
}
2035
})
2136
.when('/passwordResetConfirm/:firstToken/:passwordResetToken', {
2237
templateUrl: 'views/passwordresetconfirm.html',
38+
resolve: {
39+
authenticated: ['djangoAuth', function(djangoAuth){
40+
return djangoAuth.authenticationStatus();
41+
}],
42+
}
2343
})
2444
.when('/login', {
2545
templateUrl: 'views/login.html',
46+
resolve: {
47+
authenticated: ['djangoAuth', function(djangoAuth){
48+
return djangoAuth.authenticationStatus();
49+
}],
50+
}
2651
})
2752
.when('/verifyEmail/:emailVerificationToken', {
2853
templateUrl: 'views/verifyemail.html',
54+
resolve: {
55+
authenticated: ['djangoAuth', function(djangoAuth){
56+
return djangoAuth.authenticationStatus();
57+
}],
58+
}
2959
})
3060
.when('/logout', {
3161
templateUrl: 'views/logout.html',
62+
resolve: {
63+
authenticated: ['djangoAuth', function(djangoAuth){
64+
return djangoAuth.authenticationStatus();
65+
}],
66+
}
3267
})
3368
.when('/userProfile', {
3469
templateUrl: 'views/userprofile.html',
70+
resolve: {
71+
authenticated: ['djangoAuth', function(djangoAuth){
72+
return djangoAuth.authenticationStatus();
73+
}],
74+
}
3575
})
3676
.when('/passwordChange', {
3777
templateUrl: 'views/passwordchange.html',
78+
resolve: {
79+
authenticated: ['djangoAuth', function(djangoAuth){
80+
return djangoAuth.authenticationStatus();
81+
}],
82+
}
83+
})
84+
.when('/restricted', {
85+
templateUrl: 'views/restricted.html',
86+
controller: 'RestrictedCtrl',
87+
resolve: {
88+
authenticated: ['djangoAuth', function(djangoAuth){
89+
return djangoAuth.authenticationStatus();
90+
}],
91+
}
92+
})
93+
.when('/authRequired', {
94+
templateUrl: 'views/authrequired.html',
95+
controller: 'AuthrequiredCtrl',
96+
resolve: {
97+
authenticated: ['djangoAuth', function(djangoAuth){
98+
return djangoAuth.authenticationStatus(true);
99+
}],
100+
}
38101
})
39102
.otherwise({
40103
redirectTo: '/'
41104
});
105+
})
106+
.run(function(djangoAuth){
107+
djangoAuth.initialize('//127.0.0.1:8000/rest-auth', false);
42108
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
/**
4+
* @ngdoc function
5+
* @name angularDjangoRegistrationAuthApp.controller:AuthrequiredCtrl
6+
* @description
7+
* # AuthrequiredCtrl
8+
* Controller of the angularDjangoRegistrationAuthApp
9+
*/
10+
angular.module('angularDjangoRegistrationAuthApp')
11+
.controller('AuthrequiredCtrl', function ($scope) {
12+
$scope.awesomeThings = [
13+
'HTML5 Boilerplate',
14+
'AngularJS',
15+
'Karma'
16+
];
17+
});

app/scripts/controllers/login.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('angularDjangoRegistrationAuthApp')
4-
.controller('LoginCtrl', function ($scope, djangoAuth, Validate) {
4+
.controller('LoginCtrl', function ($scope, $location, djangoAuth, Validate) {
55
$scope.model = {'username':'','password':''};
66
$scope.complete = false;
77
$scope.login = function(formData){
@@ -11,8 +11,7 @@ angular.module('angularDjangoRegistrationAuthApp')
1111
djangoAuth.login($scope.model.username, $scope.model.password)
1212
.then(function(data){
1313
// success case
14-
$scope.complete = true;
15-
$scope.setAuth(true);
14+
$location.path("/");
1615
},function(data){
1716
// error case
1817
$scope.error = data.error;

app/scripts/controllers/logout.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
angular.module('angularDjangoRegistrationAuthApp')
44
.controller('LogoutCtrl', function ($scope, $location, djangoAuth) {
55
djangoAuth.logout();
6-
$scope.setAuth(false);
76
});

app/scripts/controllers/main.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ angular.module('angularDjangoRegistrationAuthApp')
66
$scope.login = function(){
77
djangoAuth.login(prompt('Username'),prompt('password'))
88
.then(function(data){
9-
$scope.setAuth(true);
109
handleSuccess(data);
1110
},handleError);
1211
}
1312

1413
$scope.logout = function(){
1514
djangoAuth.logout()
1615
.then(handleSuccess,handleError);
17-
$scope.setAuth(false);
1816
}
1917

2018
$scope.resetPassword = function(){
@@ -67,4 +65,13 @@ angular.module('angularDjangoRegistrationAuthApp')
6765
var handleError = function(data){
6866
$scope.response = data;
6967
}
68+
69+
$scope.show_login = true;
70+
$scope.$on("djangoAuth.logged_in", function(data){
71+
$scope.show_login = false;
72+
});
73+
$scope.$on("djangoAuth.logged_out", function(data){
74+
$scope.show_login = true;
75+
});
76+
7077
});

app/scripts/controllers/master.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
'use strict';
22

33
angular.module('angularDjangoRegistrationAuthApp')
4-
.controller('MasterCtrl', function ($scope, djangoAuth) {
5-
djangoAuth.initialize('//127.0.0.1:8000/rest-auth', false, $scope);
4+
.controller('MasterCtrl', function ($scope, $location, djangoAuth) {
5+
// Assume user is not logged in until we hear otherwise
6+
$scope.authenticated = false;
7+
// Wait for the status of authentication, set scope var to true if it resolves
8+
djangoAuth.authenticationStatus(true).then(function(){
9+
$scope.authenticated = true;
10+
});
11+
// Wait and respond to the logout event.
12+
$scope.$on('djangoAuth.logged_out', function() {
13+
$scope.authenticated = false;
14+
});
15+
// Wait and respond to the log in event.
16+
$scope.$on('djangoAuth.logged_in', function() {
17+
$scope.authenticated = true;
18+
});
19+
// If the user attempts to access a restricted page, redirect them back to the main page.
20+
$scope.$on('$routeChangeError', function(ev, current, previous, rejection){
21+
console.error("Unable to change routes. Error: ", rejection)
22+
$location.path('/restricted').replace();
23+
});
624
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
/**
4+
* @ngdoc function
5+
* @name angularDjangoRegistrationAuthApp.controller:RestrictedCtrl
6+
* @description
7+
* # RestrictedCtrl
8+
* Controller of the angularDjangoRegistrationAuthApp
9+
*/
10+
angular.module('angularDjangoRegistrationAuthApp')
11+
.controller('RestrictedCtrl', function ($scope, $location) {
12+
$scope.$on('djangoAuth.logged_in', function() {
13+
$location.path('/');
14+
});
15+
});

app/scripts/services/djangoAuth.js

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('angularDjangoRegistrationAuthApp')
4-
.service('djangoAuth', function djangoAuth($q, $http, $cookies) {
4+
.service('djangoAuth', function djangoAuth($q, $http, $cookies, $rootScope) {
55
// AngularJS will instantiate a singleton by calling "new" on this function
66
var service = {
77
/* START CUSTOMIZATION HERE */
@@ -13,6 +13,7 @@ angular.module('angularDjangoRegistrationAuthApp')
1313
'use_session': true,
1414
/* END OF CUSTOMIZATION */
1515
'authenticated': null,
16+
'authPromise': null,
1617
'request': function(args) {
1718
// Let's retrieve the token from the cookie, if available
1819
if($cookies.token){
@@ -90,15 +91,20 @@ angular.module('angularDjangoRegistrationAuthApp')
9091
$http.defaults.headers.common.Authorization = 'Token ' + data.key;
9192
$cookies.token = data.key;
9293
}
94+
djangoAuth.authenticated = true;
95+
$rootScope.$broadcast("djangoAuth.logged_in", data);
9396
});
9497
},
9598
'logout': function(){
99+
var djangoAuth = this;
96100
return this.request({
97101
'method': "GET",
98102
'url': "/logout/"
99103
}).then(function(data){
100104
delete $http.defaults.headers.common.Authorization;
101105
delete $cookies.token;
106+
djangoAuth.authenticated = false;
107+
$rootScope.$broadcast("djangoAuth.logged_out");
102108
});
103109
},
104110
'changePassword': function(password1,password2){
@@ -151,27 +157,48 @@ angular.module('angularDjangoRegistrationAuthApp')
151157
}
152158
});
153159
},
154-
'initialize': function(url, sessions, model){
155-
this.API_URL = url;
156-
this.use_session = sessions;
157-
if(model){
158-
model.authenticated = null;
159-
if(this.authenticated == null){
160-
var djangoAuth = this;
161-
this.profile().then(function(){
162-
djangoAuth.authenticated = true;
163-
model.authenticated = true;
164-
},function(){
165-
djangoAuth.authenticated = false;
166-
model.authenticated = false;
167-
});
160+
'authenticationStatus': function(restrict, force){
161+
// Set restrict to true to reject the promise if not logged in
162+
// Set to false or omit to resolve when status is known
163+
// Set force to true to ignore stored value and query API
164+
restrict = restrict || false;
165+
force = force || false;
166+
if(this.authPromise == null || force){
167+
this.authPromise = this.request({
168+
'method': "GET",
169+
'url': "/user/"
170+
})
171+
}
172+
var da = this;
173+
var getAuthStatus = $q.defer();
174+
if(this.authenticated != null && !force){
175+
// We have a stored value which means we can pass it back right away.
176+
if(this.authenticated == false && restrict){
177+
getAuthStatus.reject("User is not logged in.");
168178
}else{
169-
model.authenticated = this.authenticated;
170-
}
171-
model.setAuth = function(auth){
172-
model.authenticated = auth;
179+
getAuthStatus.resolve();
173180
}
181+
}else{
182+
// There isn't a stored value, or we're forcing a request back to
183+
// the API to get the authentication status.
184+
this.authPromise.then(function(){
185+
da.authenticated = true;
186+
getAuthStatus.resolve();
187+
},function(){
188+
da.authenticated = false;
189+
if(restrict){
190+
getAuthStatus.reject("User is not logged in.");
191+
}else{
192+
getAuthStatus.resolve();
193+
}
194+
});
174195
}
196+
return getAuthStatus.promise;
197+
},
198+
'initialize': function(url, sessions){
199+
this.API_URL = url;
200+
this.use_session = sessions;
201+
return this.authenticationStatus();
175202
}
176203

177204
}

0 commit comments

Comments
 (0)