-
Notifications
You must be signed in to change notification settings - Fork 7
/
page0.js
96 lines (96 loc) · 3.46 KB
/
page0.js
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
import {firebaseConfig} from './config.js';
const firebaseApp = firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const firestore = firebase.firestore();
const signupForm = document.querySelector('.registration.form');
const loginForm = document.querySelector('.login.form');
const forgotForm=document.querySelector('.forgot.form');
const container=document.querySelector('.container');
const signupBtn = document.querySelector('.signupbtn');
const anchors = document.querySelectorAll('a');
anchors.forEach(anchor => {
anchor.addEventListener('click', () => {
const id = anchor.id;
switch(id){
case 'loginLabel':
signupForm.style.display = 'none';
loginForm.style.display = 'block';
forgotForm.style.display = 'none';
break;
case 'signupLabel':
signupForm.style.display = 'block';
loginForm.style.display = 'none';
forgotForm.style.display = 'none';
break;
case 'forgotLabel':
signupForm.style.display = 'none';
loginForm.style.display = 'none';
forgotForm.style.display = 'block';
break;
}
});
});
signupBtn.addEventListener('click', () => {
const name = document.querySelector('#name').value;
const username = document.querySelector('#username').value;
const email = document.querySelector('#email').value.trim();
const password = document.querySelector('#password').value;
auth.createUserWithEmailAndPassword(email, password)
.then((userCredential) => {
const user = userCredential.user;
const uid = user.uid;
user.sendEmailVerification()
.then(() => {
alert('Verification email sent. Please check your inbox and verify your email before signing in.');
})
.catch((error) => {
alert('Error sending verification email: ' + error.message);
});
console.log('User data saved to Firestore');
firestore.collection('users').doc(uid).set({
name: name,
username: username,
email: email,
})
signupForm.style.display = 'none';
loginForm.style.display = 'block';
forgotForm.style.display = 'none';
})
.catch((error) => {
alert('Error signing up: '+error.message);
});
});
const loginBtn = document.querySelector('.loginbtn');
loginBtn.addEventListener('click', () => {
const email = document.querySelector('#inUsr').value.trim();
const password = document.querySelector('#inPass').value;
auth.signInWithEmailAndPassword(email, password)
.then((userCredential) => {
const user = userCredential.user;
if (user.emailVerified) {
console.log('User is signed in with a verified email.');
location.href = "signout.html";
} else {
alert('Please verify your email before signing in.');
}
})
.catch((error) => {
alert('Error signing in: ' + error.message);
});
});
const forgotBtn=document.querySelector('.forgotbtn');
forgotBtn.addEventListener('click', () => {
const emailForReset = document.querySelector('#forgotinp').value.trim();
if (emailForReset.length>0) {
auth.sendPasswordResetEmail(emailForReset)
.then(() => {
alert('Password reset email sent. Please check your inbox to reset your password.');
signupForm.style.display = 'none';
loginForm.style.display = 'block';
forgotForm.style.display = 'none';
})
.catch((error) => {
alert('Error sending password reset email: ' + error.message);
});
}
});