@@ -4,117 +4,127 @@ import {
4
4
signOut ,
5
5
onAuthStateChanged ,
6
6
sendPasswordResetEmail ,
7
- } from "firebase/auth" ;
8
-
9
- import { auth } from "./firebase" ;
10
-
11
- import setFirebaseUserCredentials from "./AuthenticationState" ;
12
-
13
- import { setLocalUserState } from "../User/UserStateController" ;
14
-
15
- var unsubscribeAuthenticationStateObserver = null ;
16
-
17
- async function registerUserUsingFirebase ( userEmail , userPassword ) {
7
+ } from "firebase/auth" ;
8
+
9
+ import { auth } from "./firebase" ;
10
+
11
+ import setFirebaseUserCredentials from "./AuthenticationState" ;
12
+
13
+ import { setLocalUserState } from "../User/UserStateController" ;
14
+
15
+ var unsubscribeAuthenticationStateObserver = null ;
16
+
17
+ var errorMap = {
18
+ "auth/weak-password" : "Password should be atleast 6 characters!" ,
19
+ "auth/invalid-email" : "Please enter valid email!" ,
20
+ } ;
21
+ async function registerUserUsingFirebase ( userEmail , userPassword ) {
22
+ var res = {
23
+ success : false ,
24
+ message : "" ,
25
+ } ;
26
+
18
27
try {
19
- const userCredential = await createUserWithEmailAndPassword (
20
- auth ,
21
- userEmail ,
22
- userPassword
23
- ) ;
24
-
25
- console . log ( "User Created Successfully: " + userCredential . user ) ;
26
-
27
- return true ;
28
+ const userCredential = await createUserWithEmailAndPassword (
29
+ auth ,
30
+ userEmail ,
31
+ userPassword
32
+ ) ;
33
+
34
+ console . log ( "User Created Successfully: " + userCredential . user ) ;
35
+ res . success = true ;
36
+ return res ;
28
37
} catch ( error ) {
29
- // Error
30
- console . log ( "User Creation Unsuccessful: " + error ) ;
31
-
32
- return false ;
38
+ // Error
39
+ console . log ( "User Creation Unsuccessful: " + error ) ;
40
+ res . success = false ;
41
+ res . message = errorMap [ error [ "code" ] ] ;
42
+ return res ;
33
43
}
34
- }
35
-
36
- async function loginUserUsingFirebase ( userEmail , userPassword ) {
44
+ }
45
+
46
+ async function loginUserUsingFirebase ( userEmail , userPassword ) {
37
47
try {
38
- const userCredential = await signInWithEmailAndPassword (
39
- auth ,
40
- userEmail ,
41
- userPassword
42
- ) ;
43
-
44
- console . log ( "User Logged In Successfully: " + userCredential . user ) ;
45
-
46
- return true ;
48
+ const userCredential = await signInWithEmailAndPassword (
49
+ auth ,
50
+ userEmail ,
51
+ userPassword
52
+ ) ;
53
+
54
+ console . log ( "User Logged In Successfully: " + userCredential . user ) ;
55
+
56
+ return true ;
47
57
} catch ( error ) {
48
- console . log ( "User Login Unsuccessful: " + error ) ;
49
-
50
- // Invalid Email/Password
51
- return false ;
58
+ console . log ( "User Login Unsuccessful: " + error ) ;
59
+
60
+ // Invalid Email/Password
61
+ return false ;
52
62
}
53
- }
54
-
55
- async function logoutUserUsingFirebase ( ) {
63
+ }
64
+
65
+ async function logoutUserUsingFirebase ( ) {
56
66
try {
57
- await signOut ( auth ) ;
58
-
59
- console . log ( "Signout Successful" ) ;
60
-
61
- return true ;
67
+ await signOut ( auth ) ;
68
+
69
+ console . log ( "Signout Successful" ) ;
70
+
71
+ return true ;
62
72
} catch ( error ) {
63
- console . log ( "Could Not Signout: " + error ) ;
64
-
65
- return false ;
73
+ console . log ( "Could Not Signout: " + error ) ;
74
+
75
+ return false ;
66
76
}
67
- }
68
-
69
- async function resetUserPasswordUsingFirebase ( userEmail ) {
77
+ }
78
+
79
+ async function resetUserPasswordUsingFirebase ( userEmail ) {
70
80
try {
71
- await sendPasswordResetEmail ( auth , userEmail ) ;
72
-
73
- console . log ( "Password Reset Email Sent Successfully" ) ;
74
-
75
- return true ;
81
+ await sendPasswordResetEmail ( auth , userEmail ) ;
82
+
83
+ console . log ( "Password Reset Email Sent Successfully" ) ;
84
+
85
+ return true ;
76
86
} catch ( error ) {
77
- console . log ( "Could Not Send Password Reset Email : " + error ) ;
78
-
79
- return false ;
87
+ console . log ( "Could Not Send Password Reset Email : " + error ) ;
88
+
89
+ return false ;
80
90
}
81
- }
82
-
83
- async function isUserLoggedInUsingFirebase ( ) {
91
+ }
92
+
93
+ async function isUserLoggedInUsingFirebase ( ) {
84
94
return new Promise ( ( resolve , reject ) => {
85
- const unsubscribe = onAuthStateChanged ( auth , ( user ) => {
86
- unsubscribe ( ) ; // Stop listening to further changes
87
- if ( user ) {
88
- resolve ( true ) ; // User is logged in
89
- } else {
90
- resolve ( false ) ; // User is not logged in
91
- }
92
- } ) ;
95
+ const unsubscribe = onAuthStateChanged ( auth , ( user ) => {
96
+ unsubscribe ( ) ; // Stop listening to further changes
97
+ if ( user ) {
98
+ resolve ( true ) ; // User is logged in
99
+ } else {
100
+ resolve ( false ) ; // User is not logged in
101
+ }
102
+ } ) ;
93
103
} ) ;
94
- }
95
-
96
- function observeAuthState ( ) {
104
+ }
105
+
106
+ function observeAuthState ( ) {
97
107
if ( unsubscribeAuthenticationStateObserver !== null ) {
98
- unsubscribeAuthenticationStateObserver ( ) ;
108
+ unsubscribeAuthenticationStateObserver ( ) ;
99
109
}
100
-
110
+
101
111
unsubscribeAuthenticationStateObserver = onAuthStateChanged (
102
- auth ,
103
- async ( user ) => {
104
- setFirebaseUserCredentials ( user ) ;
105
-
106
- // Possible to add setUserState Here (Issue: Higher Coupling)
107
- await setLocalUserState ( user ) ;
108
- }
112
+ auth ,
113
+ async ( user ) => {
114
+ setFirebaseUserCredentials ( user ) ;
115
+
116
+ // Possible to add setUserState Here (Issue: Higher Coupling)
117
+ await setLocalUserState ( user ) ;
118
+ }
109
119
) ;
110
- }
111
-
112
- observeAuthState ( ) ;
113
-
114
- export {
120
+ }
121
+
122
+ observeAuthState ( ) ;
123
+
124
+ export {
115
125
registerUserUsingFirebase ,
116
126
loginUserUsingFirebase ,
117
127
logoutUserUsingFirebase ,
118
128
resetUserPasswordUsingFirebase ,
119
129
isUserLoggedInUsingFirebase ,
120
- } ;
130
+ } ;
0 commit comments