22
33var _ = require ( 'lodash' ) ;
44var format = require ( 'util' ) . format ;
5+ var rsvp = require ( 'rsvp' ) ;
56
67function FirebaseAuth ( ) {
8+ this . currentUser = null ;
79 this . _auth = {
8- userData : null ,
910 listeners : [ ] ,
1011 completionListeners : [ ] ,
1112 users : [ ] ,
@@ -15,13 +16,34 @@ function FirebaseAuth () {
1516
1617FirebaseAuth . prototype . changeAuthState = function ( userData ) {
1718 this . _defer ( 'changeAuthState' , _ . toArray ( arguments ) , function ( ) {
18- if ( ! _ . isEqual ( this . _auth . userData , userData ) ) {
19- this . _auth . userData = _ . isObject ( userData ) ? userData : null ;
19+ if ( ! _ . isEqual ( this . currentUser , userData ) ) {
20+ this . currentUser = _ . isObject ( userData ) ? userData : null ;
2021 this . _triggerAuthEvent ( ) ;
2122 }
2223 } ) ;
2324} ;
2425
26+ FirebaseAuth . prototype . onAuthStateChanged = function ( callback ) {
27+ var self = this ;
28+ var currentUser = this . currentUser ;
29+ this . _auth . listeners . push ( { fn : callback } ) ;
30+
31+ defer ( ) ;
32+ return destroy ;
33+
34+ function destroy ( ) {
35+ self . offAuth ( callback ) ;
36+ }
37+
38+ function defer ( ) {
39+ self . _defer ( 'onAuthStateChanged' , _ . toArray ( arguments ) , function ( ) {
40+ if ( ! _ . isEqual ( self . currentUser , currentUser ) ) {
41+ self . _triggerAuthEvent ( ) ;
42+ }
43+ } ) ;
44+ }
45+ } ;
46+
2547FirebaseAuth . prototype . getEmailUser = function ( email ) {
2648 var users = this . _auth . users ;
2749 return users . hasOwnProperty ( email ) ? _ . clone ( users [ email ] ) : null ;
@@ -46,12 +68,66 @@ Object.keys(authMethods)
4668 } ;
4769 } ) ;
4870
71+ var signinMethods = {
72+ signInWithCustomToken : function ( authToken ) {
73+ return {
74+ isAnonymous : false
75+ } ;
76+ } ,
77+ signInAnonymously : function ( ) {
78+ return {
79+ isAnonymous : true
80+ } ;
81+ } ,
82+ signInWithEmailAndPassword : function ( email , password ) {
83+ return {
84+ isAnonymous : false ,
85+ email : email
86+ } ;
87+ } ,
88+ signInWithPopup : function ( provider ) {
89+ return {
90+ isAnonymous : false ,
91+ providerData : [ provider ]
92+ } ;
93+ } ,
94+ signInWithRedirect : function ( provider ) {
95+ return {
96+ isAnonymous : false ,
97+ providerData : [ provider ]
98+ } ;
99+ } ,
100+ signInWithCredential : function ( credential ) {
101+ return {
102+ isAnonymous : false
103+ } ;
104+ }
105+ } ;
106+
107+ Object . keys ( signinMethods )
108+ . forEach ( function ( method ) {
109+ var getUser = signinMethods [ method ] ;
110+ FirebaseAuth . prototype [ method ] = function ( ) {
111+ var self = this ;
112+ var user = getUser . apply ( this , arguments ) ;
113+ var promise = new rsvp . Promise ( function ( resolve , reject ) {
114+ self . _authEvent ( method , function ( err ) {
115+ if ( err ) reject ( err ) ;
116+ self . currentUser = user ;
117+ resolve ( user ) ;
118+ self . _triggerAuthEvent ( ) ;
119+ } , true ) ;
120+ } ) ;
121+ return promise ;
122+ } ;
123+ } ) ;
124+
49125FirebaseAuth . prototype . auth = function ( token , callback ) {
50126 console . warn ( 'FIREBASE WARNING: FirebaseRef.auth() being deprecated. Please use FirebaseRef.authWithCustomToken() instead.' ) ;
51127 this . _authEvent ( 'auth' , callback ) ;
52128} ;
53129
54- FirebaseAuth . prototype . _authEvent = function ( method , callback ) {
130+ FirebaseAuth . prototype . _authEvent = function ( method , callback , defercallback ) {
55131 var err = this . _nextErr ( method ) ;
56132 if ( ! callback ) return ;
57133 if ( err ) {
@@ -62,26 +138,33 @@ FirebaseAuth.prototype._authEvent = function (method, callback) {
62138 } ) ;
63139 }
64140 else {
65- // if there is no error, then we just add our callback to the listener
66- // stack and wait for the next changeAuthState() call.
67- this . _auth . completionListeners . push ( { fn : callback } ) ;
141+ if ( defercallback ) {
142+ this . _defer ( method , _ . toArray ( arguments ) , function ( ) {
143+ callback ( ) ;
144+ } ) ;
145+ } else {
146+ // if there is no error, then we just add our callback to the listener
147+ // stack and wait for the next changeAuthState() call.
148+ this . _auth . completionListeners . push ( { fn : callback } ) ;
149+ }
68150 }
69151} ;
70152
71153FirebaseAuth . prototype . _triggerAuthEvent = function ( ) {
72154 var completionListeners = this . _auth . completionListeners ;
73155 this . _auth . completionListeners = [ ] ;
74- var user = this . _auth . userData ;
156+ var user = this . currentUser ;
75157 completionListeners . forEach ( function ( parts ) {
76158 parts . fn . call ( parts . context , null , _ . cloneDeep ( user ) ) ;
77159 } ) ;
78- this . _auth . listeners . forEach ( function ( parts ) {
160+ var listeners = _ . cloneDeep ( this . _auth . listeners ) ;
161+ listeners . forEach ( function ( parts ) {
79162 parts . fn . call ( parts . context , _ . cloneDeep ( user ) ) ;
80163 } ) ;
81164} ;
82165
83166FirebaseAuth . prototype . getAuth = function ( ) {
84- return this . _auth . userData ;
167+ return this . currentUser ;
85168} ;
86169
87170FirebaseAuth . prototype . onAuth = function ( onComplete , context ) {
@@ -102,12 +185,28 @@ FirebaseAuth.prototype.offAuth = function (onComplete, context) {
102185} ;
103186
104187FirebaseAuth . prototype . unauth = function ( ) {
105- if ( this . _auth . userData !== null ) {
106- this . _auth . userData = null ;
188+ if ( this . currentUser !== null ) {
189+ this . currentUser = null ;
107190 this . _triggerAuthEvent ( ) ;
108191 }
109192} ;
110193
194+ FirebaseAuth . prototype . signOut = function ( ) {
195+ var self = this , updateuser = this . currentUser !== null ;
196+ var promise = new rsvp . Promise ( function ( resolve , reject ) {
197+ self . _authEvent ( 'signOut' , function ( err ) {
198+ if ( err ) reject ( err ) ;
199+ self . currentUser = null ;
200+ resolve ( ) ;
201+
202+ if ( updateuser ) {
203+ self . _triggerAuthEvent ( ) ;
204+ }
205+ } , true ) ;
206+ } ) ;
207+ return promise ;
208+ } ;
209+
111210FirebaseAuth . prototype . createUser = function ( credentials , onComplete ) {
112211 validateCredentials ( 'createUser' , credentials , [
113212 'email' ,
0 commit comments