forked from PriyaGhosal/BuddyTrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.js
133 lines (114 loc) · 4.4 KB
/
firebase.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
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
133
// Firebase configuration
const firebaseConfig = {
apiKey: "",
authDomain: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: "",
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Initialize Firebase Auth
const auth = firebase.auth();
// Google Authentication function
function signInWithGoogle() {
var provider = new firebase.auth.GoogleAuthProvider();
auth
.signInWithPopup(provider)
.then((result) => {
const user = result.user;
console.log("User signed in:", user);
// Redirect to dashboard or any page after successful login
window.location.href = "book.html";
})
.catch((error) => {
console.log("Error during sign-in:", error);
});
}
function signUpWithEmailAndPassword(email, password, name) {
auth.createUserWithEmailAndPassword(email, password)
.then((userCredential) => {
// Successfully created new user
const user = userCredential.user;
console.log("User signed up:", user);
// Optionally, save additional user information (like name) in your database
// Redirect to dashboard or any page after successful sign-up
window.location.href = "book.html";
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.error("Error during sign-up:", errorCode, errorMessage);
// Optionally, display an error message to the user
alert(errorMessage);
});
}
// Password validation function
function validatePassword(password) {
const minLength = 8; // Minimum length
const hasUpperCase = /[A-Z]/.test(password); // At least one uppercase letter
const hasLowerCase = /[a-z]/.test(password); // At least one lowercase letter
const hasNumber = /\d/.test(password); // At least one digit
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password); // At least one special character
if (password.length < minLength) {
return "Password must be at least 8 characters long.";
}
if (!hasUpperCase) {
return "Password must contain at least one uppercase letter.";
}
if (!hasLowerCase) {
return "Password must contain at least one lowercase letter.";
}
if (!hasNumber) {
return "Password must contain at least one number.";
}
if (!hasSpecialChar) {
return "Password must contain at least one special character.";
}
return null; // Return null if all conditions are met
}
function loginWithEmailAndPassword(email, password) {
auth.signInWithEmailAndPassword(email, password)
.then((userCredential) => {
const user = userCredential.user;
console.log("User logged in:", user);
window.location.href = "book.html";
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
console.error("Error during login:", errorCode, errorMessage);
alert(errorMessage);
});
}
// Add event listener to Google sign-up button
document.querySelector(".google-btn").addEventListener("click", function (e) {
e.preventDefault(); // Prevent default link behavior
signInWithGoogle();
});
document.getElementById('signupForm').addEventListener('submit', function(e) {
e.preventDefault(); // Prevent default form submission
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
const confirmPassword = document.getElementById('confirm-password').value;
if (password !== confirmPassword) {
alert("Passwords do not match!");
return;
}
const passwordError = validatePassword(password);
if (passwordError) {
alert(passwordError);
return;
}
signUpWithEmailAndPassword(email, password, name);
});
document.getElementById('loginForm').addEventListener('submit', function (e) {
e.preventDefault(); // Prevent default form submission
const email = document.getElementById('email').value;
const password = document.getElementById('password').value;
// Call the login function
loginWithEmailAndPassword(email, password);
});