11'use strict' ;
22
33angular . 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