-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
132 lines (112 loc) · 2.81 KB
/
auth.js
File metadata and controls
132 lines (112 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
createUserWithEmailAndPassword,
EmailAuthProvider,
onAuthStateChanged,
reauthenticateWithCredential,
sendEmailVerification,
signInWithEmailAndPassword,
signOut,
verifyBeforeUpdateEmail,
updatePassword,
} from "firebase/auth";
import { auth, db } from "./firebaseConfig";
import { doc, setDoc, updateDoc } from "firebase/firestore";
export const signUp = async (email, password, userData = {}) => {
try {
const userCredential = await createUserWithEmailAndPassword(
auth,
email,
password
);
const user = userCredential.user;
await setDoc(doc(db, 'users', user.uid), {
email: email,
createdAt: new Date(),
emailVerified: false,
...userData
});
await sendEmailVerification(user);
return user;
} catch (error) {
throw error;
}
};
export const signIn = async (email, password) => {
try {
const userCredential = await signInWithEmailAndPassword(
auth,
email,
password
);
if (!userCredential.user.emailVerified) {
await signOut(auth);
throw new Error("EMAIL_NOT_VERIFIED");
}
return userCredential.user;
} catch (error) {
throw error;
}
};
export const logOut = async () => {
try {
await signOut(auth);
} catch (error) {
throw error;
}
};
export const onAuthChange = (callback) => {
return onAuthStateChanged(auth, callback);
};
export const changePassword = async (currentPassword, newPassword) => {
try {
const user = auth.currentUser;
if (!user || !user.email) {
throw new Error("No user logged in");
}
const credential = EmailAuthProvider.credential(
user.email,
currentPassword
);
await reauthenticateWithCredential(user, credential);
await updatePassword(user, newPassword);
console.log("Password updated successfully!");
} catch (error) {
throw error;
}
};
export const changeEmail = async (currentPassword, newEmail) => {
try {
const user = auth.currentUser;
if (!user || !user.email) {
throw new Error("No user logged in");
}
const credential = EmailAuthProvider.credential(
user.email,
currentPassword
);
await reauthenticateWithCredential(user, credential);
await verifyBeforeUpdateEmail(user, newEmail);
} catch (error) {
throw error;
}
};
export const checkEmailVerified = async () => {
const user = auth.currentUser;
if (user) {
await user.reload();
if (user.emailVerified) {
try {
await updateDoc(doc(db, 'users', user.uid), {
emailVerified: true
});
} catch (error) {
console.error("Error updating emailVerified in Firestore:", error);
}
}
return user.emailVerified;
}
return false;
};
export const getCurrentUser = () => {
return auth.currentUser;
};