1
1
import { 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' ;
3
4
import { Router } from "@angular/router" ;
4
5
import * as firebase from 'firebase' ;
5
6
6
- export class EmailPasswordCredentials {
7
- email : string ;
8
- password : string ;
9
- }
10
-
11
7
12
8
@Injectable ( )
13
9
export class AuthService {
14
10
15
- authState : FirebaseAuthState = null ;
11
+ authState : any = null ;
16
12
17
- constructor ( private af : AngularFire ,
13
+ constructor ( private afAuth : AngularFireAuth ,
18
14
private db : AngularFireDatabase ,
19
15
private router :Router ) {
20
16
21
- af . auth . subscribe ( ( auth ) => {
22
- this . authState = auth ;
17
+ this . afAuth . authState . subscribe ( ( auth ) => {
18
+ this . authState = auth
23
19
} ) ;
24
20
}
25
21
@@ -28,14 +24,14 @@ export class AuthService {
28
24
return this . authState !== null ;
29
25
}
30
26
31
- // Returns current user
27
+ // Returns current user data
32
28
get currentUser ( ) : any {
33
- return this . authenticated ? this . authState . auth : null ;
29
+ return this . authenticated ? this . authState : null ;
34
30
}
35
31
36
32
// Returns
37
33
get currentUserObservable ( ) : any {
38
- return this . af . auth
34
+ return this . afAuth . authState
39
35
}
40
36
41
37
// Returns current user UID
@@ -45,83 +41,76 @@ export class AuthService {
45
41
46
42
// Anonymous User
47
43
get currentUserAnonymous ( ) : boolean {
48
- return this . authenticated ? this . authState . anonymous : false
44
+ return this . authenticated ? this . authState . isAnonymous : false
49
45
}
50
46
51
47
// Returns current user display name or Guest
52
48
get currentUserDisplayName ( ) : string {
53
- if ( ! this . authenticated ) { return 'Guest' }
54
-
49
+ if ( ! this . authState ) { return 'Guest' }
55
50
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' }
59
52
}
60
53
61
54
//// Social Auth ////
62
55
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 ) ;
65
59
}
66
60
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 ) ;
69
64
}
70
65
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 ) ;
73
69
}
74
70
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 ) ;
77
74
}
78
75
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
+ } )
82
82
. catch ( error => console . log ( error ) ) ;
83
83
}
84
84
85
85
86
86
//// Anonymous Auth ////
87
87
88
88
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 ( )
92
93
} )
93
- . then ( ( ) => this . updateUserData ( ) )
94
94
. catch ( error => console . log ( error ) ) ;
95
95
}
96
96
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
-
111
97
//// Email/Password Auth ////
112
98
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
+ } )
116
105
. catch ( error => console . log ( error ) ) ;
117
106
}
118
107
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
+ } )
125
114
. catch ( error => console . log ( error ) ) ;
126
115
}
127
116
@@ -138,7 +127,7 @@ export class AuthService {
138
127
//// Sign Out ////
139
128
140
129
signOut ( ) : void {
141
- this . af . auth . logout ( ) ;
130
+ this . afAuth . auth . signOut ( ) ;
142
131
this . router . navigate ( [ '/' ] )
143
132
}
144
133
@@ -151,9 +140,9 @@ export class AuthService {
151
140
152
141
let path = `users/${ this . currentUserId } ` ; // Endpoint on firebase
153
142
let data = {
154
- name : this . currentUser . displayName ,
155
- email : this . currentUser . email ,
156
- }
143
+ email : this . authState . email ,
144
+ name : this . authState . displayName
145
+ }
157
146
158
147
this . db . object ( path ) . update ( data )
159
148
. catch ( error => console . log ( error ) ) ;
0 commit comments