-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistration.js
249 lines (211 loc) · 9.34 KB
/
registration.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-app.js";
import { getDatabase, ref, set, push, get, child } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-database.js";
import { getStorage, ref as storageRef, uploadBytes, getDownloadURL } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-storage.js";
import { getAuth, createUserWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js";
import firebaseConfig from "./firebaseConfig.js";
// Initialize Firebase & getting the database reference
const app = initializeApp(firebaseConfig);
const database = getDatabase();
const auth = getAuth();
var allFormValues, profilePictureUrl;
let loading;
//after loading - add event listener for the registration, and for the pop up close button
window.onload = function(){
var form = document.getElementById("registrationForm");
form.addEventListener("submit", handleRegistration);
var closeButton = document.getElementById("closeButton");
closeButton.addEventListener("click", function(){
var errorCorrection = document.getElementById("errorCorrection");
errorCorrection.style.display = "none";
});
var profilePicture = document.getElementById("profilePicture");
profilePicture.addEventListener("change", getFile);
}
//used for getting the profile picture so it can be uploded later
//it is stored in a global variable so that we can use it later in the upload file function
var fileItem, fileName;
function getFile(e){
console.log("picture captured.");
fileItem = e.target.files[0];
var extension = fileItem.name.split('.').pop(); //gets the extension of the file
fileName = crypto.randomUUID()+'.'+extension; //generates a new name for the file to be uploaded to avoid conflicting names.
}
function registerUser(){
if(fileItem != null){ //if a picture has been captured then we need to upload
uploadFile(); //uploads the profile pic and then calls write the user data with the profile pic url
}
else{
writeUserData(); //just calls the writeUserData and then so that the avatar's url is the default url.
}
}
function uploadFile(){
var storage = getStorage(); //gets the storage
var imageRef = storageRef(storage, "images/"+fileName); //using the storage get a refernce to the images folder
//then upload the image content
uploadBytes(imageRef, fileItem).then((snapshot) => {
getDownloadURL(imageRef).then((url)=>{
//writeUserData(formValues, url);
profilePictureUrl = url;
writeUserData();
});
});
}
function writeUserData() {
//get the email and password from the formValues array
var email = allFormValues[2];
var password = allFormValues[5];
createUserWithEmailAndPassword(auth, email, password)
.then((credentials) => {
var userId = credentials.user.uid;
if(profilePictureUrl == null){
profilePictureUrl = "https://firebasestorage.googleapis.com/v0/b/tigrayjobs-3f65f.appspot.com/o/images%2Favatar.jpg?alt=media&token=c8916d9a-7d40-4943-96fa-773a34376e54";
}
set(ref(database, 'users/'+userId), {
firstName: allFormValues[0],
lastName: allFormValues[1],
phoneNumber: allFormValues[3],
userName: allFormValues[4],
region: allFormValues[7],
cityName: allFormValues[8],
studyField: allFormValues[9],
educationLevel: allFormValues[10],
profession: allFormValues[11],
experience: allFormValues[12],
preferences: "",
profilePicture: profilePictureUrl
}).then((ob)=>{
alert("Account created Successfully.");
var databaseRef = ref(database);
get(child(databaseRef, 'users/'+credentials.user.uid)).then((snapshot)=>{
if(snapshot.exists){
var userInfo = JSON.stringify(snapshot.val());
sessionStorage.setItem("seekerInfo", userInfo);
sessionStorage.setItem("seekerCreds", JSON.stringify(credentials.user));
sessionStorage.setItem("loggedIn", "true");
document.location.href="seeker/seekerPage.html";
}else{
alert("snapshot doesn't exist");
}
});
});
console.log("Created the User successfully. With Email/Password.");
})
.catch((error) => {
if(error.code == 'auth/email-already-in-use'){
loading.classList.remove("show");
alert("Sorry! This email is already in use. Try another.");
}
});
}
function validateUserInput(){
var firstName = document.getElementById("firstName");
if(!checkValidity(firstName.value, "name")){
generateErrorMessage("Input Invalid (First Name)! Please make sure that the First Name input only has letters and spaces");
return false;
}
var lastName = document.getElementById("lastName");
if(!checkValidity(lastName.value, "name")){
generateErrorMessage("Input Invalid (Last Name)! Please make sure that the Last Name input only has letters and spaces");
return false;
}
var phoneNumber = document.getElementById("phoneNumber");
if(phoneNumber.value.length > 0){ //if there is an input
if(!checkValidity(phoneNumber.value, "phoneNumber")){
generateErrorMessage("Input invalid (Phone No)! Please check the examples in the input to follow one of the two ways to write phone numbers.");
return false;
}
}
var userName = document.getElementById("userName");
if(!checkValidity(userName.value.trim(), "userName")){
generateErrorMessage("Input invalid (User Name)! Please make sure that the input user name only contains alphabets and numbers in those order.");
return false;
}
var password = document.getElementById("password");
var confirm = document.getElementById("confirmPassword");
if(!checkValidity(password.value, "password")){
generateErrorMessage("Input invalid (password)! Please make sure that the password doesn't contain spaces.!");
return false;
}
if(password.value != confirm.value){
generateErrorMessage("The Two password inputs don't match. Please make sure they are equal.");
return false;
}
var cityName = document.getElementById("cityName");
if(cityName.value.length > 0){
if(!checkValidity(cityName.value, "name")){
generateErrorMessage("Input Invalid (City Name)! Please make sure that the City Name input only has letters and spaces");
return false;
}
}
var studyField = document.getElementById("studyField");
if(studyField.value.length > 0){
if(!checkValidity(studyField.value, "name")){
generateErrorMessage("Input Invalid (Field Of Study)! Please make sure that the Field Of Study input only has letters and spaces");
return false;
}
}
var profession = document.getElementById("profession");
if(profession.value.length > 0){
if(!checkValidity(profession.value, "name")){
generateErrorMessage("Input Invalid (Main Profession)! Please make sure that the Main Profession input only has letters and spaces");
return false;
}
}
return true;
}
function checkValidity(input, typeInput){
if(typeInput == "name"){
if(!(/^[a-zA-Z ]+$/.test(input))){
return false;
}
}else if(typeInput == "email"){
return true;
}else if(typeInput == "phoneNumber"){
if(!(/^09[0-9]{8}$|^2519[0-9]{8}$/.test(input))){
return false;
}
}else if(typeInput == "userName"){
if(!(/^[a-zA-Z]{1}[a-zA-Z0-9]*$/.test(input))){
return false;
}
}else if(typeInput == "password"){
if(!(/^\S+$/.test(input))){
return false;
}
}
return true;
}
function generateErrorMessage(msg){
var errorContainer = document.getElementById("errorCorrection");
var errorContent = document.getElementById("errorContent");
errorContent.innerHTML = msg;
errorContainer.style.display = "block";
}
function handleRegistration(event){
event.preventDefault(); //prevent the default submission behavior
loading = document.querySelector("#loading");
console.log(loading);
console.log(loading.classList);
loading.classList.add("show");
console.log("staring now");
var validated = validateUserInput();
if(validated){
console.log("validated");
var ids = ["firstName", "lastName", "email", "phoneNumber", "userName", "password", "confirmPassword", "region", "cityName", "studyField", "educationLevel", "profession", "experience"];
var formElements = []; //stores the objects themselves not the values of the form inputs
for(let i = 0; i < ids.length; i++){
formElements[i] = document.getElementById(ids[i]);
}
var formValues = []; //stores the values of the form inputs
for(let i = 0; i < formElements.length; i++){
formValues[i] = formElements[i].value;
}
allFormValues = formValues;
registerUser();
}
else{
return;
}
}
// }