1
+ import { Injectable } from '@angular/core' ;
2
+ import {
3
+ Auth ,
4
+ signInWithEmailAndPassword ,
5
+ createUserWithEmailAndPassword ,
6
+ signOut ,
7
+ GoogleAuthProvider ,
8
+ } from '@angular/fire/auth' ;
9
+ import { AngularFireAuth } from '@angular/fire/compat/auth' ;
10
+ import {
11
+ AngularFirestore ,
12
+ AngularFirestoreCollection ,
13
+ AngularFirestoreDocument ,
14
+ } from '@angular/fire/compat/firestore' ;
15
+ import { Router } from '@angular/router' ;
16
+ import firebase from 'firebase/compat/app' ;
17
+ import { Observable , map } from 'rxjs' ;
18
+ import { User } from 'src/app/models/interface/user.model' ;
19
+
20
+ @Injectable ( {
21
+ providedIn : 'root' ,
22
+ } )
23
+ export class AuthService {
24
+ isLogin : boolean = true ;
25
+ userDoc : AngularFirestoreDocument | undefined ;
26
+ user ! : firebase . User | null ;
27
+ isLoggedIn$ : Observable < boolean > ;
28
+ // userRef = this.afs.collection(`user`)
29
+ constructor (
30
+ private auth : Auth ,
31
+ private afs : AngularFirestore ,
32
+ private afAuth : AngularFireAuth ,
33
+ private router : Router
34
+ ) {
35
+ // Subscribe to authentication state changes
36
+ this . isLoggedIn$ = this . afAuth . authState . pipe (
37
+ map ( user => ! ! user ) // Convert user object to boolean (true if logged in, false if not)
38
+ ) ;
39
+ }
40
+
41
+ async register ( email : any , password : any ) {
42
+ try {
43
+ const user = await createUserWithEmailAndPassword (
44
+ this . auth ,
45
+ email ,
46
+ password
47
+ ) ;
48
+ this . updateUserData ( user . user ) ;
49
+ return user ;
50
+ } catch ( e ) {
51
+ return null ;
52
+ }
53
+ }
54
+
55
+ async login ( email : any , password : any ) {
56
+ try {
57
+ const user = await signInWithEmailAndPassword ( this . auth , email , password ) ;
58
+ await this . getEmailBasedUser ( user . user ) ;
59
+ localStorage . setItem ( 'user' , JSON . stringify ( user . user ) ) ;
60
+ this . isLogin = true ;
61
+ return user ;
62
+ } catch ( e ) {
63
+ return null ;
64
+ }
65
+ }
66
+
67
+ logout ( ) {
68
+ this . isLogin = false ;
69
+ localStorage . clear ( ) ;
70
+ return signOut ( this . auth ) ;
71
+ }
72
+ GoogleAuth ( ) {
73
+ return this . AuthLogin ( new GoogleAuthProvider ( ) ) ;
74
+ }
75
+
76
+ // Auth logic to run auth providers
77
+ AuthLogin ( provider ) {
78
+ return this . afAuth
79
+ . signInWithPopup ( provider )
80
+ . then ( ( result ) => {
81
+ // console.log('You have been successfully logged in!');
82
+ } )
83
+ . catch ( ( error ) => {
84
+ // console.log(error);
85
+ } ) ;
86
+ }
87
+
88
+ async googleSignin ( ) :Promise < any > {
89
+ // this.GoogleAuth()
90
+ let provider = new GoogleAuthProvider ( ) ;
91
+ // console.log(typeof( provider));
92
+ const credential = await this . afAuth . signInWithPopup ( provider ) ;
93
+ if ( credential ) {
94
+ this . isLogin = true ;
95
+ this . updateUserData ( credential . user ) ;
96
+ return credential . user
97
+ }
98
+ else {
99
+ return null
100
+ }
101
+ }
102
+ async getEmailBasedUser ( user : any ) {
103
+ const userRef : AngularFirestoreDocument = this . afs . doc ( `user/${ user . uid } ` ) ;
104
+ if ( userRef . get ( ) ) {
105
+ userRef . get ( ) . subscribe ( ( res : any ) => {
106
+ let responseData = res . data ( ) ;
107
+ if ( responseData === undefined ) {
108
+ const data = JSON . parse ( JSON . stringify ( user ) ) ;
109
+ userRef . set ( data , { merge : true } ) ;
110
+ }
111
+ localStorage . setItem ( 'UserData' , JSON . stringify ( user ) ) ;
112
+ } ) ;
113
+ } else if ( userRef . get ( ) === undefined ) {
114
+ userRef . set ( user , { merge : true } ) ;
115
+ }
116
+ }
117
+
118
+ private updateUserData ( user : any ) {
119
+ // Sets user data to firestore on login
120
+ const userRef : AngularFirestoreDocument = this . afs . doc ( `user/${ user . uid } ` ) ;
121
+
122
+ const data = JSON . parse ( JSON . stringify ( user ) ) ;
123
+ if ( userRef . get ( ) ) {
124
+ userRef . get ( ) . subscribe ( ( res : any ) => {
125
+ let responseData = res . data ( ) ;
126
+ if ( responseData === undefined ) {
127
+ const data = JSON . parse ( JSON . stringify ( user ) ) ;
128
+ userRef . set ( data , { merge : true } ) ;
129
+ }
130
+ localStorage . setItem ( 'UserData' , JSON . stringify ( user ) ) ;
131
+ this . router . navigateByUrl ( '/home' , { replaceUrl : true } ) ;
132
+ } ) ;
133
+ } else {
134
+ userRef . set ( data , { merge : true } ) ;
135
+ }
136
+ }
137
+ }
0 commit comments