Skip to content

Commit 970b82f

Browse files
author
Aishwarya Nair
committed
push updated version
1 parent 4802cd3 commit 970b82f

File tree

4 files changed

+395
-545
lines changed

4 files changed

+395
-545
lines changed

Frontend/src/Authentication/UserAuthenticationController.js

Lines changed: 101 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,117 +4,127 @@ import {
44
signOut,
55
onAuthStateChanged,
66
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+
1827
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;
2837
} 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;
3343
}
34-
}
35-
36-
async function loginUserUsingFirebase(userEmail, userPassword) {
44+
}
45+
46+
async function loginUserUsingFirebase(userEmail, userPassword) {
3747
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;
4757
} 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;
5262
}
53-
}
54-
55-
async function logoutUserUsingFirebase() {
63+
}
64+
65+
async function logoutUserUsingFirebase() {
5666
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;
6272
} catch (error) {
63-
console.log("Could Not Signout: " + error);
64-
65-
return false;
73+
console.log("Could Not Signout: " + error);
74+
75+
return false;
6676
}
67-
}
68-
69-
async function resetUserPasswordUsingFirebase(userEmail) {
77+
}
78+
79+
async function resetUserPasswordUsingFirebase(userEmail) {
7080
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;
7686
} 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;
8090
}
81-
}
82-
83-
async function isUserLoggedInUsingFirebase() {
91+
}
92+
93+
async function isUserLoggedInUsingFirebase() {
8494
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+
});
93103
});
94-
}
95-
96-
function observeAuthState() {
104+
}
105+
106+
function observeAuthState() {
97107
if (unsubscribeAuthenticationStateObserver !== null) {
98-
unsubscribeAuthenticationStateObserver();
108+
unsubscribeAuthenticationStateObserver();
99109
}
100-
110+
101111
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+
}
109119
);
110-
}
111-
112-
observeAuthState();
113-
114-
export {
120+
}
121+
122+
observeAuthState();
123+
124+
export {
115125
registerUserUsingFirebase,
116126
loginUserUsingFirebase,
117127
logoutUserUsingFirebase,
118128
resetUserPasswordUsingFirebase,
119129
isUserLoggedInUsingFirebase,
120-
};
130+
};

0 commit comments

Comments
 (0)