@@ -14,225 +14,187 @@ export class AuthService {
14
14
private alertService : AlertService ,
15
15
private userService : UserService ) { }
16
16
17
- // Signup/register
17
+ // Signup/registration
18
18
public signUpWithGoogle ( ) : Promise < void > {
19
19
const providerGoogle = new firebase . auth . GoogleAuthProvider ( ) ;
20
20
return firebase . auth ( ) . signInWithPopup ( providerGoogle )
21
- . then ( ( result ) => {
22
- } )
23
- . then ( response => {
21
+ . then ( ( ) => {
24
22
this . router . navigate ( [ '/' ] ) ;
25
23
firebase . auth ( ) . currentUser . getIdToken ( )
26
24
. then (
27
25
( token : string ) => this . token = token
28
26
) ;
29
- this . alertService . showToaster ( 'Verification email is sent to you.' ) ;
30
- this . userService . verificationUserEmail ( ) ;
31
- this . userService . saveUserInfo ( firebase . auth ( ) . currentUser . uid , name , firebase . auth ( ) . currentUser . email ) ;
32
- }
33
- )
27
+ this . verificationEmail ( ) ;
28
+ } )
34
29
. catch (
35
- error => console . error ( error )
30
+ error => this . alertService . showToaster ( error )
36
31
) ;
37
32
}
38
33
39
34
public signUpWithTwitter ( ) : Promise < void > {
40
35
const providerTwitter = new firebase . auth . TwitterAuthProvider ( ) ;
41
36
return firebase . auth ( ) . signInWithPopup ( providerTwitter )
42
- . then ( ( result ) => {
43
- const currentUser = result . user ;
44
- } )
45
- . then ( response => {
37
+ . then ( ( ) => {
46
38
this . router . navigate ( [ '/' ] ) ;
47
39
firebase . auth ( ) . currentUser . getIdToken ( )
48
40
. then (
49
41
( token : string ) => this . token = token
50
42
) ;
51
- this . alertService . showToaster ( 'Please check your inbox for a verification email.' ) ;
52
- this . userService . verificationUserEmail ( ) ;
53
- this . userService . saveUserInfo ( firebase . auth ( ) . currentUser . uid , name , firebase . auth ( ) . currentUser . email ) ;
54
- }
55
- )
43
+ this . verificationEmail ( ) ;
44
+ } )
56
45
. catch (
57
- error => console . error ( error )
46
+ error => this . alertService . showToaster ( error )
58
47
) ;
59
48
}
60
49
61
50
public signUpWithFacebook ( ) : Promise < void > {
62
51
const providerFacebook = new firebase . auth . FacebookAuthProvider ( ) ;
63
52
return firebase . auth ( ) . signInWithPopup ( providerFacebook )
64
- . then (
65
- response => {
66
- this . router . navigate ( [ '/' ] ) ;
67
- firebase . auth ( ) . currentUser . getIdToken ( )
68
- . then (
69
- ( token : string ) => this . token = token
70
- ) ;
71
- this . alertService . showToaster ( 'Verification email is sent to you.' ) ;
72
- this . userService . verificationUserEmail ( ) ;
73
- this . userService . saveUserInfo ( firebase . auth ( ) . currentUser . uid , name , firebase . auth ( ) . currentUser . email ) ;
74
- }
75
- )
53
+ . then ( ( ) => {
54
+ this . router . navigate ( [ '/' ] ) ;
55
+ firebase . auth ( ) . currentUser . getIdToken ( )
56
+ . then (
57
+ ( token : string ) => this . token = token
58
+ ) ;
59
+ this . verificationEmail ( ) ;
60
+ } )
76
61
. catch (
77
- error => console . error ( error )
62
+ error => this . alertService . showToaster ( error )
78
63
) ;
79
64
}
80
65
81
66
public signUpWithGithub ( ) : Promise < void > {
82
67
const providerGithub = new firebase . auth . GithubAuthProvider ( ) ;
83
68
return firebase . auth ( ) . signInWithPopup ( providerGithub )
84
- . then ( ( result ) => {
85
- const currentUser = result . user ;
86
- this . alertService . showToaster ( 'Verification email is sent to you.' ) ;
87
- this . userService . verificationUserEmail ( ) ;
88
- this . userService . saveUserInfo ( firebase . auth ( ) . currentUser . uid , name , firebase . auth ( ) . currentUser . email ) ;
89
- } )
90
- . then ( response => {
69
+ . then ( ( ) => {
91
70
this . router . navigate ( [ '/' ] ) ;
92
71
firebase . auth ( ) . currentUser . getIdToken ( )
93
72
. then (
94
73
( token : string ) => this . token = token
95
74
) ;
96
- }
97
- )
75
+ this . verificationEmail ( ) ;
76
+ } )
98
77
. catch (
99
- error => console . error ( error )
78
+ error => this . alertService . showToaster ( error )
100
79
) ;
101
80
}
102
81
103
82
public signupUser ( email : string , password : string ) : Promise < void > {
104
83
return firebase . auth ( ) . createUserWithEmailAndPassword ( email , password )
105
- . then ( ( result ) => {
106
- this . alertService . showToaster ( 'Verification email is sent to you.' ) ;
107
- this . userService . verificationUserEmail ( ) ;
108
- this . userService . saveUserInfo ( firebase . auth ( ) . currentUser . uid , name , email ) ;
109
- }
110
- ) . catch (
111
- error => console . error ( error )
84
+ . then ( ( ) => {
85
+ this . verificationEmail ( ) ;
86
+ } )
87
+ . catch (
88
+ error => this . alertService . showToaster ( error )
112
89
) ;
113
90
}
114
91
115
92
// Signin/login
116
93
public signInWithGoogle ( ) : Promise < void > {
117
94
const providerGoogle = new firebase . auth . GoogleAuthProvider ( ) ;
118
95
return firebase . auth ( ) . signInWithPopup ( providerGoogle )
119
- . then ( ( result ) => {
120
- } )
121
- . then ( response => {
96
+ . then ( ( ) => {
122
97
this . router . navigate ( [ '/' ] ) ;
123
98
firebase . auth ( ) . currentUser . getIdToken ( )
124
99
. then (
125
100
( token : string ) => this . token = token
126
101
) ;
127
102
this . alertService . showToaster ( 'Google login succesful' ) ;
128
- }
129
- )
103
+ } )
130
104
. catch (
131
- error => console . error ( error )
105
+ error => this . alertService . showToaster ( error )
132
106
) ;
133
107
}
134
108
135
109
public signInWithTwitter ( ) : Promise < void > {
136
110
const providerTwitter = new firebase . auth . TwitterAuthProvider ( ) ;
137
111
return firebase . auth ( ) . signInWithPopup ( providerTwitter )
138
- . then ( response => {
112
+ . then ( ( ) => {
139
113
this . router . navigate ( [ '/' ] ) ;
140
114
firebase . auth ( ) . currentUser . getIdToken ( )
141
115
. then (
142
116
( token : string ) => this . token = token
143
117
) ;
144
118
this . alertService . showToaster ( 'Twitter login succesful' ) ;
145
- }
146
- )
119
+ } )
147
120
. catch (
148
- error => console . error ( error )
121
+ error => this . alertService . showToaster ( error )
149
122
) ;
150
123
}
151
124
152
125
public signInWithFacebook ( ) : Promise < void > {
153
126
const providerFacebook = new firebase . auth . FacebookAuthProvider ( ) ;
154
127
return firebase . auth ( ) . signInWithPopup ( providerFacebook )
155
- . then (
156
- response => {
157
- this . router . navigate ( [ '/' ] ) ;
158
- firebase . auth ( ) . currentUser . getIdToken ( )
159
- . then (
160
- ( token : string ) => this . token = token
161
- ) ;
162
- this . alertService . showToaster ( 'Facebook login succesful' ) ;
163
- }
164
- )
128
+ . then ( ( ) => {
129
+ this . router . navigate ( [ '/' ] ) ;
130
+ firebase . auth ( ) . currentUser . getIdToken ( )
131
+ . then (
132
+ ( token : string ) => this . token = token
133
+ ) ;
134
+ this . alertService . showToaster ( 'Facebook login succesful' ) ;
135
+ } )
165
136
. catch (
166
- error => console . error ( error )
137
+ error => this . alertService . showToaster ( error )
167
138
) ;
168
139
}
169
140
170
141
public signInWithGithub ( ) : Promise < void > {
171
142
const providerGithub = new firebase . auth . GithubAuthProvider ( ) ;
172
143
return firebase . auth ( ) . signInWithPopup ( providerGithub )
173
- . then ( ( result ) => {
174
- } )
175
- . then ( response => {
144
+ . then ( ( ) => {
176
145
this . router . navigate ( [ '/' ] ) ;
177
146
firebase . auth ( ) . currentUser . getIdToken ( )
178
147
. then (
179
148
( token : string ) => this . token = token
180
149
) ;
181
150
this . alertService . showToaster ( 'Github login succesful' ) ;
182
- }
183
- )
151
+ } )
184
152
. catch (
185
- error => console . error ( error )
153
+ error => this . alertService . showToaster ( error )
186
154
) ;
187
155
}
188
156
189
157
public signinUser ( email : string , password : string ) : Promise < void > {
190
158
return firebase . auth ( ) . signInWithEmailAndPassword ( email , password )
191
- . then (
192
- response => {
193
- this . router . navigate ( [ '/' ] ) ;
194
- firebase . auth ( ) . currentUser . getIdToken ( )
195
- . then (
196
- ( token : string ) => this . token = token
197
- ) ;
198
- this . alertService . showToaster ( 'Login succesful' ) ;
199
- }
200
- )
159
+ . then ( ( ) => {
160
+ this . router . navigate ( [ '/' ] ) ;
161
+ firebase . auth ( ) . currentUser . getIdToken ( )
162
+ . then (
163
+ ( token : string ) => this . token = token
164
+ ) ;
165
+ this . alertService . showToaster ( 'Login succesful' ) ;
166
+ } )
201
167
. catch (
202
- error => console . error ( error )
168
+ error => this . alertService . showToaster ( error )
203
169
) ;
204
170
}
205
171
206
172
public signInAnonymous ( ) : Promise < void > {
207
173
return firebase . auth ( ) . signInAnonymously ( )
208
- . then (
209
- response => {
210
- this . router . navigate ( [ '/' ] ) ;
211
- firebase . auth ( ) . onAuthStateChanged ( currentUser => {
212
- firebase . auth ( ) . currentUser . getIdToken ( )
213
- . then (
214
- ( token : string ) => this . token = token
215
- ) ,
216
- this . alertService . showToaster ( 'Anonymous login succesful' ) ;
217
- } ) ;
218
- }
219
- )
174
+ . then ( ( ) => {
175
+ this . router . navigate ( [ '/' ] ) ;
176
+ firebase . auth ( ) . onAuthStateChanged ( ( ) => {
177
+ firebase . auth ( ) . currentUser . getIdToken ( )
178
+ . then (
179
+ ( token : string ) => this . token = token
180
+ ) ,
181
+ this . alertService . showToaster ( 'Anonymous login succesful' ) ;
182
+ } ) ;
183
+ } )
220
184
. catch (
221
- error => console . error ( error )
185
+ error => this . alertService . showToaster ( error )
222
186
) ;
223
187
}
224
188
225
189
// Other
226
190
public logout ( ) : Promise < void > {
227
191
return firebase . auth ( ) . signOut ( )
228
- . then (
229
- response => {
230
- this . token = null ;
231
- this . router . navigate ( [ '/home' ] ) ;
232
- }
233
- )
192
+ . then ( ( ) => {
193
+ this . token = null ;
194
+ this . router . navigate ( [ '/home' ] ) ;
195
+ } )
234
196
. catch (
235
- error => console . error ( error )
197
+ error => this . alertService . showToaster ( error )
236
198
) ;
237
199
}
238
200
@@ -247,4 +209,10 @@ export class AuthService {
247
209
public isAuthenticated ( ) : boolean {
248
210
return this . token != null ;
249
211
}
212
+
213
+ private verificationEmail ( ) : void {
214
+ this . alertService . showToaster ( 'Please check your inbox for a verification email.' ) ;
215
+ this . userService . verificationUserEmail ( ) ;
216
+ this . userService . saveUserInfo ( firebase . auth ( ) . currentUser . uid , name , firebase . auth ( ) . currentUser . email ) ;
217
+ }
250
218
}
0 commit comments