Skip to content

Commit f1e69d7

Browse files
committed
Update auth.service.ts
1 parent 16756ea commit f1e69d7

File tree

1 file changed

+119
-93
lines changed

1 file changed

+119
-93
lines changed
Lines changed: 119 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,55 @@
11
import { Injectable } from '@angular/core';
22
import { FirebaseApp, initializeApp } from 'firebase/app';
3-
import { AppCheck, AppCheckTokenResult, getToken, initializeAppCheck, ReCaptchaV3Provider } from 'firebase/app-check';
4-
import { AngularFireAuth } from '@angular/fire/compat/auth';
5-
import { AngularFirestore } from '@angular/fire/compat/firestore';
6-
import { GoogleAuthProvider } from 'firebase/auth';
7-
8-
import { ActivatedRoute, Router } from "@angular/router";
3+
import {
4+
AppCheck,
5+
AppCheckTokenResult,
6+
getToken,
7+
initializeAppCheck,
8+
ReCaptchaV3Provider,
9+
} from 'firebase/app-check';
10+
import {
11+
Auth,
12+
getAuth,
13+
signInWithEmailAndPassword,
14+
createUserWithEmailAndPassword,
15+
sendEmailVerification,
16+
sendPasswordResetEmail,
17+
signInWithPopup,
18+
GoogleAuthProvider,
19+
} from 'firebase/auth';
20+
import { Firestore, getFirestore } from 'firebase/firestore';
21+
import { ActivatedRoute, Router } from '@angular/router';
922
import { environment } from 'src/environments/environment';
1023
import { SharedService } from './shared.service';
1124

1225
@Injectable({
13-
providedIn: 'root'
26+
providedIn: 'root',
1427
})
15-
1628
export class AuthService {
1729
app: FirebaseApp;
1830
appCheck: AppCheck;
31+
auth: Auth;
32+
firestore: Firestore;
1933
userData: any = {
2034
uid: null,
2135
email: null,
2236
displayName: null,
2337
photoURL: './assets/dummy-user.png',
24-
emailVerified: false
38+
emailVerified: false,
2539
}; // Save logged in user data
2640

27-
constructor(public afs: AngularFirestore, public afAuth: AngularFireAuth, public router: Router, public route: ActivatedRoute,
28-
public sharedService: SharedService) {
41+
constructor(
42+
public router: Router,
43+
public route: ActivatedRoute,
44+
public sharedService: SharedService
45+
) {
2946
this.app = initializeApp(environment.firebase);
3047
this.appCheck = initializeAppCheck(this.app, {
3148
provider: new ReCaptchaV3Provider(environment.captchaKey),
32-
isTokenAutoRefreshEnabled: true
49+
isTokenAutoRefreshEnabled: true,
3350
});
51+
this.auth = getAuth(this.app);
52+
this.firestore = getFirestore(this.app);
3453
}
3554

3655
async getToken(): Promise<AppCheckTokenResult | undefined> {
@@ -47,88 +66,97 @@ export class AuthService {
4766
return new Promise(async (resolve, reject) => {
4867
const token = await this.getToken();
4968
if (!token) {
50-
// reject('No Token Re-Captcha Failed');
5169
resolve(true);
5270
} else {
5371
resolve(token);
5472
}
5573
});
5674
}
5775

58-
signIn(email: any, password: any) {
59-
this.checkToken().then(() => {
60-
return this.afAuth.signInWithEmailAndPassword(email, password)
61-
.then((result: any) => {
62-
if (result.user && result.user.emailVerified) {
63-
this.SetUserData(result.user);
64-
this.router.navigate(['dashboard']);
65-
} else {
66-
this.router.navigate(['verify-email-address']);
67-
}
68-
}).catch((error: any) => {
69-
this.sharedService.showAlert({
70-
class: 'error',
71-
title: error.message,
72-
iconClass: 'fa-solid fa-phone'
73-
});
76+
signIn(email: string, password: string) {
77+
this.checkToken()
78+
.then(() => {
79+
return signInWithEmailAndPassword(this.auth, email, password)
80+
.then((result) => {
81+
if (result.user && result.user.emailVerified) {
82+
this.SetUserData(result.user);
83+
this.router.navigate(['dashboard']);
84+
} else {
85+
this.router.navigate(['verify-email-address']);
86+
}
87+
})
88+
.catch((error) => {
89+
this.sharedService.showAlert({
90+
class: 'error',
91+
title: error.message,
92+
iconClass: 'fa-solid fa-phone',
93+
});
94+
});
7495
})
75-
}).catch((error: any) => {
76-
console.error(error);
77-
});
96+
.catch((error) => {
97+
console.error(error);
98+
});
7899
}
79100

80-
signUp(email: any, password: any) {
81-
this.checkToken().then(() => {
82-
return this.afAuth.createUserWithEmailAndPassword(email, password)
83-
.then((result: any) => {
84-
this.sendVerificationMail(result.user);
85-
this.SetUserData(result.user);
86-
}).catch((error: any) => {
87-
this.sharedService.showAlert({
88-
class: 'error',
89-
title: error.message,
90-
iconClass: 'fa-solid fa-phone'
91-
});
101+
signUp(email: string, password: string) {
102+
this.checkToken()
103+
.then(() => {
104+
return createUserWithEmailAndPassword(this.auth, email, password)
105+
.then((result) => {
106+
this.sendVerificationMail(result.user);
107+
this.SetUserData(result.user);
108+
})
109+
.catch((error) => {
110+
this.sharedService.showAlert({
111+
class: 'error',
112+
title: error.message,
113+
iconClass: 'fa-solid fa-phone',
114+
});
115+
});
92116
})
93-
}).catch((error: any) => {
94-
console.error(error);
95-
});
117+
.catch((error) => {
118+
console.error(error);
119+
});
96120
}
97121

98122
sendVerificationMail(user: any) {
99-
this.checkToken().then(() => {
100-
return user.sendEmailVerification()
123+
this.checkToken()
101124
.then(() => {
102-
this.router.navigate(['verify-email-address']);
125+
return sendEmailVerification(user).then(() => {
126+
this.router.navigate(['verify-email-address']);
127+
});
103128
})
104-
}).catch((error: any) => {
105-
console.error(error);
106-
});
129+
.catch((error) => {
130+
console.error(error);
131+
});
107132
}
108133

109-
forgotPassword(passwordResetEmail: any) {
110-
this.checkToken().then(() => {
111-
return this.afAuth.sendPasswordResetEmail(passwordResetEmail)
134+
forgotPassword(passwordResetEmail: string) {
135+
this.checkToken()
112136
.then(() => {
113-
this.sharedService.showAlert({
114-
class: 'info',
115-
title: 'Password reset email sent, check your inbox.',
116-
iconClass: 'fa-solid fa-phone'
117-
});
118-
}).catch((error: any) => {
137+
return sendPasswordResetEmail(this.auth, passwordResetEmail)
138+
.then(() => {
139+
this.sharedService.showAlert({
140+
class: 'info',
141+
title: 'Password reset email sent, check your inbox.',
142+
iconClass: 'fa-solid fa-phone',
143+
});
144+
})
145+
.catch((error) => {
146+
this.sharedService.showAlert({
147+
class: 'error',
148+
title: error.message,
149+
iconClass: 'fa-solid fa-phone',
150+
});
151+
});
152+
})
153+
.catch((error) => {
119154
this.sharedService.showAlert({
120155
class: 'error',
121-
title: error,
122-
iconClass: 'fa-solid fa-phone'
156+
title: error.message,
157+
iconClass: 'fa-solid fa-phone',
123158
});
124-
})
125-
}).catch((error: any) => {
126-
this.sharedService.showAlert({
127-
class: 'error',
128-
title: error,
129-
iconClass: 'fa-solid fa-phone'
130159
});
131-
});
132160
}
133161

134162
get isLoggedIn(): boolean {
@@ -137,29 +165,32 @@ export class AuthService {
137165
user = JSON.parse(user);
138166
this.userData = user;
139167
}
140-
return (user && user.emailVerified !== false) ? true : false;
168+
return user && user.emailVerified !== false ? true : false;
141169
}
142170

143171
GoogleAuth() {
144-
this.checkToken().then(() => {
145-
return this.AuthLogin(new GoogleAuthProvider());
146-
}).catch((error: any) => {
147-
console.error(error);
148-
});
172+
this.checkToken()
173+
.then(() => {
174+
return this.AuthLogin(new GoogleAuthProvider());
175+
})
176+
.catch((error) => {
177+
console.error(error);
178+
});
149179
}
150180

151181
AuthLogin(provider: any) {
152-
return this.afAuth.signInWithPopup(provider)
153-
.then((result: any) => {
182+
return signInWithPopup(this.auth, provider)
183+
.then((result) => {
154184
this.SetUserData(result.user);
155185
this.router.navigate(['dashboard']);
156-
}).catch((error: any) => {
186+
})
187+
.catch((error) => {
157188
this.sharedService.showAlert({
158189
class: 'error',
159-
title: error,
160-
iconClass: 'fa-solid fa-phone'
190+
title: error.message,
191+
iconClass: 'fa-solid fa-phone',
161192
});
162-
})
193+
});
163194
}
164195

165196
SetUserData(user: any) {
@@ -168,22 +199,17 @@ export class AuthService {
168199
email: user.email,
169200
displayName: user.displayName ? user.displayName : user.email,
170201
photoURL: user.photoURL,
171-
emailVerified: user.emailVerified
172-
}
202+
emailVerified: user.emailVerified,
203+
};
173204
localStorage.setItem('WebRTCVideoCallUser', JSON.stringify(userData));
174-
// const userRef: AngularFirestoreDocument < any > = this.afs.doc(`users/${user.uid}`);
175-
// return userRef.set(userData, {
176-
// merge: true
177-
// })
178-
// Uncomment If you want to store user in a document
179205
}
180206

181207
SignOut() {
182-
this.afAuth.signOut().then(() => {
208+
this.auth.signOut().then(() => {
183209
this.userData = null;
184210
localStorage.removeItem('WebRTCVideoCallUser');
185211
this.router.navigate(['sign-in']);
186-
})
212+
});
187213
}
188214

189215
getValidationMessage(input: any) {
@@ -197,4 +223,4 @@ export class AuthService {
197223
}
198224
}
199225
}
200-
}
226+
}

0 commit comments

Comments
 (0)