forked from BeehiveCommunity/Frontend_Mentor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignUp.html
156 lines (140 loc) · 6.21 KB
/
signUp.html
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="signUp.css" />
<title>Sign Up</title>
</head>
<body>
<div class="container">
<h1>Sign Up</h1>
<form id="signupForm">
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" name="name" required>
<div class="error" id="nameError"></div>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<div class="error" id="emailError"></div>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required>
<div class="error" id="passwordError"></div>
</div>
<div class="form-group">
<label for="confirmPassword">Confirm Password</label>
<input type="password" id="confirmPassword" name="confirmPassword" required>
<div class="error" id="confirmPasswordError"></div>
</div>
<div class="buttons">
<button type="button" class="cancelbtn">Cancel</button>
<button type="submit" class="signupbtn">Sign Up</button>
</div>
</form>
</div>
<script type="module">
// Firebase imports
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.14.0/firebase-app.js";
import { getFirestore, addDoc, collection } from "https://www.gstatic.com/firebasejs/10.14.0/firebase-firestore.js";
import { getAuth, createUserWithEmailAndPassword, updateProfile } from "https://www.gstatic.com/firebasejs/10.14.0/firebase-auth.js";
// Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyC7ihLRIl47Iu6nk6qh_Ak8i3pSYw3tO4A",
authDomain: "frontend-mentor-cceee.firebaseapp.com",
projectId: "frontend-mentor-cceee",
storageBucket: "frontend-mentor-cceee.appspot.com",
messagingSenderId: "51303965383",
appId: "1:51303965383:web:2f8c20fb952bd844dd94e2",
measurementId: "G-S2BV64K72R",
databaseURL: "https://frontend-mentor-cceee-default-rtdb.firebaseio.com"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const auth = getAuth();
// Form handling
const form = document.getElementById('signupForm');
const nameInput = document.getElementById('name');
const emailInput = document.getElementById('email');
const passwordInput = document.getElementById('password');
const confirmPasswordInput = document.getElementById('confirmPassword');
form.addEventListener('submit', async (e) => {
e.preventDefault();
if (validateForm()) {
try {
// Sign up user with Firebase Authentication
const userCredential = await createUserWithEmailAndPassword(auth, emailInput.value, passwordInput.value);
const user = userCredential.user;
// Update the user's display name
await updateProfile(user, { displayName: nameInput.value });
// Save user info to Firestore
await addDoc(collection(db, "users"), {
uid: user.uid,
name: nameInput.value,
email: emailInput.value
});
// Success message
alert('Signed up successfully and data stored in Firestore!');
form.reset();
} catch (error) {
// Error handling
alert('Error: ' + error.message);
}
}
});
function validateForm() {
let isValid = true;
// Name validation
if (nameInput.value.trim() === '') {
showError('nameError', 'Name is required');
isValid = false;
} else {
showError('nameError', '');
}
// Email validation
if (emailInput.value.trim() === '') {
showError('emailError', 'Email is required');
isValid = false;
} else if (!isValidEmail(emailInput.value)) {
showError('emailError', 'Please enter a valid email');
isValid = false;
} else {
showError('emailError', '');
}
// Password validation
if (passwordInput.value === '') {
showError('passwordError', 'Password is required');
isValid = false;
} else if (passwordInput.value.length < 6) {
showError('passwordError', 'Password must be at least 6 characters');
isValid = false;
} else {
showError('passwordError', '');
}
// Confirm password validation
if (confirmPasswordInput.value === '') {
showError('confirmPasswordError', 'Please confirm your password');
isValid = false;
} else if (confirmPasswordInput.value !== passwordInput.value) {
showError('confirmPasswordError', 'Passwords do not match');
isValid = false;
} else {
showError('confirmPasswordError', '');
}
return isValid;
}
function showError(id, message) {
const errorElement = document.getElementById(id);
errorElement.textContent = message;
}
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
</script>
</body>
</html>