11import { Injectable } from '@angular/core' ;
2- import { AngularFireAuth , AngularFireDatabase , FirebaseAuthState , AuthProviders , AuthMethods , AngularFire } from "angularfire2" ;
2+ import { AngularFireDatabaseModule , AngularFireDatabase , FirebaseListObservable } from 'angularfire2/database' ;
3+ import { AngularFireAuth } from 'angularfire2/auth' ;
34import { Router } from "@angular/router" ;
45import * as firebase from 'firebase' ;
56
6- export class EmailPasswordCredentials {
7- email : string ;
8- password : string ;
9- }
10-
117
128@Injectable ( )
139export class AuthService {
1410
15- authState : FirebaseAuthState = null ;
11+ authState : any = null ;
1612
17- constructor ( private af : AngularFire ,
13+ constructor ( private afAuth : AngularFireAuth ,
1814 private db : AngularFireDatabase ,
1915 private router :Router ) {
2016
21- af . auth . subscribe ( ( auth ) => {
22- this . authState = auth ;
17+ this . afAuth . authState . subscribe ( ( auth ) => {
18+ this . authState = auth
2319 } ) ;
2420 }
2521
@@ -28,14 +24,14 @@ export class AuthService {
2824 return this . authState !== null ;
2925 }
3026
31- // Returns current user
27+ // Returns current user data
3228 get currentUser ( ) : any {
33- return this . authenticated ? this . authState . auth : null ;
29+ return this . authenticated ? this . authState : null ;
3430 }
3531
3632 // Returns
3733 get currentUserObservable ( ) : any {
38- return this . af . auth
34+ return this . afAuth . authState
3935 }
4036
4137 // Returns current user UID
@@ -45,83 +41,76 @@ export class AuthService {
4541
4642 // Anonymous User
4743 get currentUserAnonymous ( ) : boolean {
48- return this . authenticated ? this . authState . anonymous : false
44+ return this . authenticated ? this . authState . isAnonymous : false
4945 }
5046
5147 // Returns current user display name or Guest
5248 get currentUserDisplayName ( ) : string {
53- if ( ! this . authenticated ) { return 'Guest' }
54-
49+ if ( ! this . authState ) { return 'Guest' }
5550 else if ( this . currentUserAnonymous ) { return 'Anonymous' }
56-
57- else { return this . authState . auth . displayName || 'User without a Name' }
58-
51+ else { return this . authState [ 'displayName' ] || 'User without a Name' }
5952 }
6053
6154 //// Social Auth ////
6255
63- githubLogin ( ) : firebase . Promise < FirebaseAuthState > {
64- return this . socialSignIn ( AuthProviders . Github ) ;
56+ githubLogin ( ) {
57+ const provider = new firebase . auth . GithubAuthProvider ( )
58+ return this . socialSignIn ( provider ) ;
6559 }
6660
67- googleLogin ( ) : firebase . Promise < FirebaseAuthState > {
68- return this . socialSignIn ( AuthProviders . Google ) ;
61+ googleLogin ( ) {
62+ const provider = new firebase . auth . GoogleAuthProvider ( )
63+ return this . socialSignIn ( provider ) ;
6964 }
7065
71- facebookLogin ( ) : firebase . Promise < FirebaseAuthState > {
72- return this . socialSignIn ( AuthProviders . Facebook ) ;
66+ facebookLogin ( ) {
67+ const provider = new firebase . auth . FacebookAuthProvider ( )
68+ return this . socialSignIn ( provider ) ;
7369 }
7470
75- twitterLogin ( ) : firebase . Promise < FirebaseAuthState > {
76- return this . socialSignIn ( AuthProviders . Twitter ) ;
71+ twitterLogin ( ) {
72+ const provider = new firebase . auth . TwitterAuthProvider ( )
73+ return this . socialSignIn ( provider ) ;
7774 }
7875
79- private socialSignIn ( provider : number ) : firebase . Promise < FirebaseAuthState > {
80- return this . af . auth . login ( { provider, method : AuthMethods . Popup } )
81- . then ( ( ) => this . updateUserData ( ) )
76+ private socialSignIn ( provider ) {
77+ return this . afAuth . auth . signInWithPopup ( provider )
78+ . then ( ( credential ) => {
79+ this . authState = credential . user
80+ this . updateUserData ( )
81+ } )
8282 . catch ( error => console . log ( error ) ) ;
8383 }
8484
8585
8686 //// Anonymous Auth ////
8787
8888 anonymousLogin ( ) {
89- return this . af . auth . login ( {
90- provider : AuthProviders . Anonymous ,
91- method : AuthMethods . Anonymous ,
89+ return this . afAuth . auth . signInAnonymously ( )
90+ . then ( ( user ) => {
91+ this . authState = user
92+ this . updateUserData ( )
9293 } )
93- . then ( ( ) => this . updateUserData ( ) )
9494 . catch ( error => console . log ( error ) ) ;
9595 }
9696
97- // anonymousUpgrade(): firebase.Promise<FirebaseAuthState> {
98- //
99- // let anonId = this.currentUserId
100- //
101- // // Login with google
102- // return this.googleLogin().then( () => {
103- // // get the data snapshot from anonymous account account
104- // this.db.object(anonId).subscribe(snapshot => {
105- // // map the anonymous user data to the new account.
106- // this.db.object(this.currentUserId).update(snapshot)
107- // })
108- // });
109- // }
110-
11197 //// Email/Password Auth ////
11298
113- emailSignUp ( credentials : EmailPasswordCredentials ) : firebase . Promise < FirebaseAuthState > {
114- return this . af . auth . createUser ( credentials )
115- . then ( ( ) => this . updateUserData ( ) )
99+ emailSignUp ( email :string , password :string ) {
100+ return this . afAuth . auth . createUserWithEmailAndPassword ( email , password )
101+ . then ( ( user ) => {
102+ this . authState = user
103+ this . updateUserData ( )
104+ } )
116105 . catch ( error => console . log ( error ) ) ;
117106 }
118107
119- emailLogin ( credentials : EmailPasswordCredentials ) : firebase . Promise < FirebaseAuthState > {
120- return this . af . auth . login ( credentials ,
121- { provider : AuthProviders . Password ,
122- method : AuthMethods . Password
123- } )
124- . then ( ( ) => this . updateUserData ( ) )
108+ emailLogin ( email : string , password : string ) {
109+ return this . afAuth . auth . signInWithEmailAndPassword ( email , password )
110+ . then ( ( user ) => {
111+ this . authState = user
112+ this . updateUserData ( )
113+ } )
125114 . catch ( error => console . log ( error ) ) ;
126115 }
127116
@@ -138,7 +127,7 @@ export class AuthService {
138127 //// Sign Out ////
139128
140129 signOut ( ) : void {
141- this . af . auth . logout ( ) ;
130+ this . afAuth . auth . signOut ( ) ;
142131 this . router . navigate ( [ '/' ] )
143132 }
144133
@@ -151,9 +140,9 @@ export class AuthService {
151140
152141 let path = `users/${ this . currentUserId } ` ; // Endpoint on firebase
153142 let data = {
154- name : this . currentUser . displayName ,
155- email : this . currentUser . email ,
156- }
143+ email : this . authState . email ,
144+ name : this . authState . displayName
145+ }
157146
158147 this . db . object ( path ) . update ( data )
159148 . catch ( error => console . log ( error ) ) ;
0 commit comments