Skip to content

Commit 217a0d4

Browse files
committed
Optimized Alert and Auth services
1 parent 2494ae2 commit 217a0d4

File tree

2 files changed

+78
-111
lines changed

2 files changed

+78
-111
lines changed

src/app/shared/services/alert.service.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import { MatSnackBar } from '@angular/material';
44
@Injectable()
55
export class AlertService {
66

7-
constructor(private snackBar: MatSnackBar) {
8-
}
7+
constructor(private snackBar: MatSnackBar) {}
98

109
public showToaster(msg: string): void {
1110
this.snackBar.open(msg, null, {
12-
duration: 3000,
11+
duration: 3500,
1312
});
1413
}
1514
}

src/app/shared/services/auth.service.ts

Lines changed: 76 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -14,225 +14,187 @@ export class AuthService {
1414
private alertService: AlertService,
1515
private userService: UserService) { }
1616

17-
// Signup/register
17+
// Signup/registration
1818
public signUpWithGoogle(): Promise<void> {
1919
const providerGoogle = new firebase.auth.GoogleAuthProvider();
2020
return firebase.auth().signInWithPopup(providerGoogle)
21-
.then((result) => {
22-
})
23-
.then(response => {
21+
.then(() => {
2422
this.router.navigate(['/']);
2523
firebase.auth().currentUser.getIdToken()
2624
.then(
2725
(token: string) => this.token = token
2826
);
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+
})
3429
.catch(
35-
error => console.error(error)
30+
error => this.alertService.showToaster(error)
3631
);
3732
}
3833

3934
public signUpWithTwitter(): Promise<void> {
4035
const providerTwitter = new firebase.auth.TwitterAuthProvider();
4136
return firebase.auth().signInWithPopup(providerTwitter)
42-
.then((result) => {
43-
const currentUser = result.user;
44-
})
45-
.then(response => {
37+
.then(() => {
4638
this.router.navigate(['/']);
4739
firebase.auth().currentUser.getIdToken()
4840
.then(
4941
(token: string) => this.token = token
5042
);
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+
})
5645
.catch(
57-
error => console.error(error)
46+
error => this.alertService.showToaster(error)
5847
);
5948
}
6049

6150
public signUpWithFacebook(): Promise<void> {
6251
const providerFacebook = new firebase.auth.FacebookAuthProvider();
6352
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+
})
7661
.catch(
77-
error => console.error(error)
62+
error => this.alertService.showToaster(error)
7863
);
7964
}
8065

8166
public signUpWithGithub(): Promise<void> {
8267
const providerGithub = new firebase.auth.GithubAuthProvider();
8368
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(() => {
9170
this.router.navigate(['/']);
9271
firebase.auth().currentUser.getIdToken()
9372
.then(
9473
(token: string) => this.token = token
9574
);
96-
}
97-
)
75+
this.verificationEmail();
76+
})
9877
.catch(
99-
error => console.error(error)
78+
error => this.alertService.showToaster(error)
10079
);
10180
}
10281

10382
public signupUser(email: string, password: string): Promise<void> {
10483
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)
11289
);
11390
}
11491

11592
// Signin/login
11693
public signInWithGoogle(): Promise<void> {
11794
const providerGoogle = new firebase.auth.GoogleAuthProvider();
11895
return firebase.auth().signInWithPopup(providerGoogle)
119-
.then((result) => {
120-
})
121-
.then(response => {
96+
.then(() => {
12297
this.router.navigate(['/']);
12398
firebase.auth().currentUser.getIdToken()
12499
.then(
125100
(token: string) => this.token = token
126101
);
127102
this.alertService.showToaster('Google login succesful');
128-
}
129-
)
103+
})
130104
.catch(
131-
error => console.error(error)
105+
error => this.alertService.showToaster(error)
132106
);
133107
}
134108

135109
public signInWithTwitter(): Promise<void> {
136110
const providerTwitter = new firebase.auth.TwitterAuthProvider();
137111
return firebase.auth().signInWithPopup(providerTwitter)
138-
.then(response => {
112+
.then(() => {
139113
this.router.navigate(['/']);
140114
firebase.auth().currentUser.getIdToken()
141115
.then(
142116
(token: string) => this.token = token
143117
);
144118
this.alertService.showToaster('Twitter login succesful');
145-
}
146-
)
119+
})
147120
.catch(
148-
error => console.error(error)
121+
error => this.alertService.showToaster(error)
149122
);
150123
}
151124

152125
public signInWithFacebook(): Promise<void> {
153126
const providerFacebook = new firebase.auth.FacebookAuthProvider();
154127
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+
})
165136
.catch(
166-
error => console.error(error)
137+
error => this.alertService.showToaster(error)
167138
);
168139
}
169140

170141
public signInWithGithub(): Promise<void> {
171142
const providerGithub = new firebase.auth.GithubAuthProvider();
172143
return firebase.auth().signInWithPopup(providerGithub)
173-
.then((result) => {
174-
})
175-
.then(response => {
144+
.then(() => {
176145
this.router.navigate(['/']);
177146
firebase.auth().currentUser.getIdToken()
178147
.then(
179148
(token: string) => this.token = token
180149
);
181150
this.alertService.showToaster('Github login succesful');
182-
}
183-
)
151+
})
184152
.catch(
185-
error => console.error(error)
153+
error => this.alertService.showToaster(error)
186154
);
187155
}
188156

189157
public signinUser(email: string, password: string): Promise<void> {
190158
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+
})
201167
.catch(
202-
error => console.error(error)
168+
error => this.alertService.showToaster(error)
203169
);
204170
}
205171

206172
public signInAnonymous(): Promise<void> {
207173
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+
})
220184
.catch(
221-
error => console.error(error)
185+
error => this.alertService.showToaster(error)
222186
);
223187
}
224188

225189
// Other
226190
public logout(): Promise<void> {
227191
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+
})
234196
.catch(
235-
error => console.error(error)
197+
error => this.alertService.showToaster(error)
236198
);
237199
}
238200

@@ -247,4 +209,10 @@ export class AuthService {
247209
public isAuthenticated(): boolean {
248210
return this.token != null;
249211
}
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+
}
250218
}

0 commit comments

Comments
 (0)