From 3f2bb11f409c5ed6c2521fb8be0257e99d36e197 Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 19:35:35 -0400 Subject: [PATCH 01/11] Update sprout.js Add pickup of security questions/answers by database, add passwordCreated vs userCreated timestamp differentiation --- public/sprout.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/public/sprout.js b/public/sprout.js index 40bbb6d..0ada41e 100644 --- a/public/sprout.js +++ b/public/sprout.js @@ -240,11 +240,16 @@ document.getElementById("new_user_form").addEventListener("submit", async functi userEmail: userEmail, firstName: firstName, lastName: lastName, - address: address, - DOB : dateOfBirth, + username: username, password: password, - createdAt: serverTimestamp(), - username: username + passwordCreatedAt: serverTimestamp(), + question1: question1, + answer1: answer1, + question2: question2, + answer2: answer2, + address: address, + DOB: dateOfBirth, + userCreatedAt: serverTimestamp() } await setDoc(doc(db, 'new_user_requests', username.toString()), newUser); From d3357a47addc29aae670708905cc9dbe9d9788d6 Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 19:44:31 -0400 Subject: [PATCH 02/11] Create createuser.js move create user process into separate .js file --- public/createuser.js | 360 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 public/createuser.js diff --git a/public/createuser.js b/public/createuser.js new file mode 100644 index 0000000..eb0fd75 --- /dev/null +++ b/public/createuser.js @@ -0,0 +1,360 @@ +console.log("createuser.js loaded!!") +/*Passwords must be: +--> a minimum of 8 characters, +--> must start with a letter, +--> must have a letter, +--> a number and special character +*/ + +function validatePassword(password) { + var passwordPattern = /^(?=[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*()-+=<>?]).{8,}$/; + return passwordPattern.test(password); +} + +/*Right now First Name must: +--> contain a letter, +--> contain only capital or lower case letters +*/ +function validateFirstName(name) { + var namePattern = /^[A-Za-z]+$/; + return namePattern.test(name); +} + +/*Right now Last Name must: +--> contain a letter, +--> contain only capital or lower case letters or spaces +*/ +function validateLastName(name) { + var namePattern = /^[A-Za-z ]+$/; + return namePattern.test(name); +} + +/*Right now Dates must: +--> be of this format MM/DD/YYYY +*/ + +function validateDate(date) { + var datePattern = /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/; + return datePattern.test(date); +} + + +function validateAddress(address) { + var addressPattern = /^[A-Za-z0-9\s.,'-]+$/; + return addressPattern.test(address); +} + +function validateEmail(email) { + var emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; + return emailPattern.test(email); +} + +function showError(input, message) { + const formControl = input.parentElement; + formControl.className = "form-control error"; + const small = formControl.querySelector('small'); + small.innerText = message +} + +// document.getElementById("password_form").addEventListener("submit", async function (e) { +// console.log("button was pressed") +// }); + +document.getElementById("new_user_form").addEventListener("submit", async function (e) { + e.preventDefault(); + + const userEmailElement = document.getElementById("user_email"); + const firstNameElement = document.getElementById("first_name"); + const lastNameElement = document.getElementById("last_name"); + const dateOfBirthElement = document.getElementById("dateofbirth"); + const addressElement = document.getElementById("address"); + const passwordElement = document.getElementById("password"); + const answer1Element = document.getElementById("answer1"); + const answer2Element = document.getElementById("answer2"); + const question1Element = document.getElementById('question1_selected'); + const question2Element = document.getElementById("question2_selected"); + + var userEmail = userEmailElement.value; + var firstName = firstNameElement.value; + var lastName = lastNameElement.value; + var address = addressElement.value; + var dateOfBirth = dateOfBirthElement.value; + var password = passwordElement.value; + var answer1 = answer1Element.value; + var answer2 = answer2Element.value; + var question1 = question1Element.value; + var question2 = question2Element.value; + + console.log("Question 1: " + question1); + console.log("Question 2: " + question2); + + var isValid = true; + + if (!validateEmail(userEmail)) { + var errorMessage = 'Please enter a valid email address' + if (userEmail == '') { + errorMessage = "Please enter an email address." + } + showError(userEmailElement, errorMessage) + isValid = false; + } + + if (!validateFirstName(firstName)) { + var errorMessage = 'First name must be only letters and contain no spaces' + if (firstName == '') { + errorMessage = "Please enter a first name." + } + showError(firstNameElement, errorMessage); + isValid = false; + } + + if (!validateLastName(lastName)) { + var errorMessage = 'Last name must be only letters' + if (lastName == '') { + errorMessage = "Please enter a last name." + } + showError(lastNameElement, errorMessage); + isValid = false; + } + + if (!validateAddress(address)) { + var errorMessage = 'Please enter a valid address' + if (address == '') { + errorMessage = 'Please enter an address' + } + showError(addressElement, errorMessage) + isValid = false; + } + + if (!validateDate(dateOfBirth)) { + var errorMessage = 'Date of birth must be in MM/DD/YYYY format' + if (dateOfBirth == '') { + errorMessage = "Please enter a date of birth." + } + showError(dateOfBirthElement, errorMessage); + isValid = false; + } + + if (!validatePassword(password)) { + var errorMessage = 'Passwords must be at least 8 characters, start with a letter, and contain a number and a special character' + if (password == '') { + errorMessage = "Please enter a password." + } + + showError(passwordElement, errorMessage); + isValid = false; + } + + if (answer1 == '') { + var errorMessage = 'Please enter an answer.'; + showError(answer1Element, errorMessage); + isValid = false; + } + + if (answer2 == '') { + var errorMessage = 'Please enter an answer'; + showError(answer2Element, errorMessage); + isValid = false; + } + + + if (!isValid) { + return false; + } + + try{ + //Ideally this date would be populating from the server timestamp, not the client-side date - TBD IN FUTURE UPDATE + const date = new Date(); + let month = String(date.getMonth()+1).padStart(2,"0"); + let day = String(date.getDay()).padStart(2,"0"); + let year = String(date.getFullYear()).slice(2); + //let userNameExists = await testUserName(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year); + //console.log("userNameExists = " + userNameExists); + + let username = await generateUsername(firstName, lastName, month, day, year); + console.log("username = " + username); + + let emailAlreadyInUse = await testUserEmail(userEmail); + console.log("emailAlreadyInUse = "+ emailAlreadyInUse); + + if(!emailAlreadyInUse){ + /* + await createUserWithEmailAndPassword(auth, userEmail, password) + .then((userCredential) => { + // Signed in + const user = userCredential.user; + console.log("user = " + user); + // ... + }) + .catch((error) => { + const errorCode = error.code; + const errorMessage = error.message; + // .. + });*/ + + /*const user = auth.currentUser; + const uid = user.uid; + console.log("UID = " + uid); + + updateProfile(auth.currentUser, { + displayName: String(firstName + " " + lastName), photoURL: "https://example.com/jane-q-user/profile.jpg" + }).then(() => { + console.log("Profile updated"); + // Profile updated! + // ... + }).catch((error) => { + // An error occurred + // ... + });*/ + + const newUser = { + userEmail: userEmail, + firstName: firstName, + lastName: lastName, + username: username, + password: password, + passwordCreatedAt: serverTimestamp(), + question1: question1, + answer1: answer1, + question2: question2, + answer2: answer2, + address: address, + DOB: dateOfBirth, + userCreatedAt: serverTimestamp() + } + + await setDoc(doc(db, 'new_user_requests', username.toString()), newUser); + console.log('New user request for added successfully!'); + + } else{ + alert("User email already in use. Return to the login screen and choose Forgot Password if you are having trouble accessing your account.") + console.log('User email already in use.'); + } + } catch(error) { + console.log(error) + } + + + return true; +}); + +async function generateUsername(firstName, lastName, month, day, year){ + let username = "TBD"; + let userCheck = await testUserName(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year); + let userCount = 0; + if(!userCheck){ + username = String(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year); + return username; + } + while(userCheck){ + userCount++; + username = String(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + userCount + month + year); + userCheck = await testUserName(username); + } + return username; +} + +//BULKY, CAN BE REDUCED IN THE FUTURE - TBD +async function testUserEmail(testEmail){ + testEmail = testEmail.toString(); + const q = query(users, where('userEmail', '==', testEmail)); + const checkEmail = await getDocs(q); + let count = 0; + checkEmail.forEach((doc) => { + count += 1; + }); + console.log("checkEmail = " + count); + if (count > 0){ + return true; + } else{ + return false; + } +} + +//BULKY, CAN BE REDUCED IN THE FUTURE - TBD +async function testUserName(testUsername){ + testUsername = testUsername.toString(); + const docRef = query(users, where('username', '==', testUsername)); + const docCheck = await getDocs(docRef); + let count = 0; + checkEmail.forEach((doc) => { + count += 1; + }); + console.log("checkUsername = " + count); + if (count > 0){ + return true; + } else{ + return false; + } + /*testUsername = testUsername.toString(); + const docRef = doc(db, 'usre', testUsername); + const docCheck = await getDoc(docRef); + if (docCheck.exists()){ + return true; + } else{ + return false; + }*/ +} + +function testValidationFunctions() { + console.log("code reached here!!"); + // Get the values + var userEmail = document.getElementById("user_email").value; + var firstName = document.getElementById("first_name").value; + var lastName = document.getElementById("last_name").value; + var address = document.getElementById("address").value; + var dateOfBirth = document.getElementById("dateofbirth").value; + var password = document.getElementById("password").value; + + // See if its working + if (validateEmail(userEmail)) { + console.log("User Email: " + userEmail); + console.log("Email is Valid"); + } else { + console.log("User Email: " + userEmail); + console.log("!!!!!Email is NOT Valid!!!!"); + } + + if (validateFirstName(firstName)) { + console.log("First Name: " + firstName); + console.log("First Name is Valid"); + } else { + console.log("First Name: " + firstName); + console.log("!!!!!First Name NOT Valid!!!!"); + } + + if (validateLastName(lastName)) { + console.log("Last Name: " + lastName); + console.log("Last Name is Valid"); + } else { + console.log("Last Name: " + lastName); + console.log("!!!!!Last Name NOT Valid!!!!"); + } + + if (validateAddress(address)) { + console.log("Address: " + address); + console.log("Address is Valid"); + } else { + console.log("Address: " + address); + console.log("!!!!!Last Name NOT Valid!!!!"); + } + + if (validateDate(dateOfBirth)) { + console.log("Date of Birth: " + dateOfBirth); + console.log("DOB is Valid"); + } else { + console.log("Date of Birth: " + dateOfBirth); + console.log("!!!!!DOB NOT Valid!!!!"); + } + + if (validatePassword(password)) { + console.log("Password: " + password); + console.log("password is Valid"); + } else { + console.log("Password: " + password); + console.log("!!!!!Password NOT Valid!!!!"); + } + + + return true; +} From f4792d723c818e1869aa484ad929fc2f71203c9b Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 19:45:03 -0400 Subject: [PATCH 03/11] Update sprout.js move create user to separate .js file --- public/sprout.js | 358 ----------------------------------------------- 1 file changed, 358 deletions(-) diff --git a/public/sprout.js b/public/sprout.js index 0ada41e..af2750d 100644 --- a/public/sprout.js +++ b/public/sprout.js @@ -26,364 +26,6 @@ let inputFile = document.getElementById("input_file"); inputFile.onchange = function(){ profilePicture.src = URL.createObjectURL(inputFile.files[0]); } - -console.log("sprout.js loaded!!") -/*Passwords must be: ---> a minimum of 8 characters, ---> must start with a letter, ---> must have a letter, ---> a number and special character -*/ - -function validatePassword(password) { - var passwordPattern = /^(?=[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*()-+=<>?]).{8,}$/; - return passwordPattern.test(password); -} - -/*Right now First Name must: ---> contain a letter, ---> contain only capital or lower case letters -*/ -function validateFirstName(name) { - var namePattern = /^[A-Za-z]+$/; - return namePattern.test(name); -} - -/*Right now Last Name must: ---> contain a letter, ---> contain only capital or lower case letters or spaces -*/ -function validateLastName(name) { - var namePattern = /^[A-Za-z ]+$/; - return namePattern.test(name); -} - -/*Right now Dates must: ---> be of this format MM/DD/YYYY -*/ - -function validateDate(date) { - var datePattern = /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/; - return datePattern.test(date); -} - - -function validateAddress(address) { - var addressPattern = /^[A-Za-z0-9\s.,'-]+$/; - return addressPattern.test(address); -} - -function validateEmail(email) { - var emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; - return emailPattern.test(email); -} - -function showError(input, message) { - const formControl = input.parentElement; - formControl.className = "form-control error"; - const small = formControl.querySelector('small'); - small.innerText = message -} - -// document.getElementById("password_form").addEventListener("submit", async function (e) { -// console.log("button was pressed") -// }); - -document.getElementById("new_user_form").addEventListener("submit", async function (e) { - e.preventDefault(); - - const userEmailElement = document.getElementById("user_email"); - const firstNameElement = document.getElementById("first_name"); - const lastNameElement = document.getElementById("last_name"); - const dateOfBirthElement = document.getElementById("dateofbirth"); - const addressElement = document.getElementById("address"); - const passwordElement = document.getElementById("password"); - const answer1Element = document.getElementById("answer1"); - const answer2Element = document.getElementById("answer2"); - const question1Element = document.getElementById('question1_selected'); - const question2Element = document.getElementById("question2_selected"); - - var userEmail = userEmailElement.value; - var firstName = firstNameElement.value; - var lastName = lastNameElement.value; - var address = addressElement.value; - var dateOfBirth = dateOfBirthElement.value; - var password = passwordElement.value; - var answer1 = answer1Element.value; - var answer2 = answer2Element.value; - var question1 = question1Element.value; - var question2 = question2Element.value; - - console.log("Question 1: " + question1); - console.log("Question 2: " + question2); - - var isValid = true; - - if (!validateEmail(userEmail)) { - var errorMessage = 'Please enter a valid email address' - if (userEmail == '') { - errorMessage = "Please enter an email address." - } - showError(userEmailElement, errorMessage) - isValid = false; - } - - if (!validateFirstName(firstName)) { - var errorMessage = 'First name must be only letters and contain no spaces' - if (firstName == '') { - errorMessage = "Please enter a first name." - } - showError(firstNameElement, errorMessage); - isValid = false; - } - - if (!validateLastName(lastName)) { - var errorMessage = 'Last name must be only letters' - if (lastName == '') { - errorMessage = "Please enter a last name." - } - showError(lastNameElement, errorMessage); - isValid = false; - } - - if (!validateAddress(address)) { - var errorMessage = 'Please enter a valid address' - if (address == '') { - errorMessage = 'Please enter an address' - } - showError(addressElement, errorMessage) - isValid = false; - } - - if (!validateDate(dateOfBirth)) { - var errorMessage = 'Date of birth must be in MM/DD/YYYY format' - if (dateOfBirth == '') { - errorMessage = "Please enter a date of birth." - } - showError(dateOfBirthElement, errorMessage); - isValid = false; - } - - if (!validatePassword(password)) { - var errorMessage = 'Passwords must be at least 8 characters, start with a letter, and contain a number and a special character' - if (password == '') { - errorMessage = "Please enter a password." - } - - showError(passwordElement, errorMessage); - isValid = false; - } - - if (answer1 == '') { - var errorMessage = 'Please enter an answer.'; - showError(answer1Element, errorMessage); - isValid = false; - } - - if (answer2 == '') { - var errorMessage = 'Please enter an answer'; - showError(answer2Element, errorMessage); - isValid = false; - } - - - if (!isValid) { - return false; - } - - try{ - //Ideally this date would be populating from the server timestamp, not the client-side date - TBD IN FUTURE UPDATE - const date = new Date(); - let month = String(date.getMonth()+1).padStart(2,"0"); - let day = String(date.getDay()).padStart(2,"0"); - let year = String(date.getFullYear()).slice(2); - //let userNameExists = await testUserName(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year); - //console.log("userNameExists = " + userNameExists); - - let username = await generateUsername(firstName, lastName, month, day, year); - console.log("username = " + username); - - let emailAlreadyInUse = await testUserEmail(userEmail); - console.log("emailAlreadyInUse = "+ emailAlreadyInUse); - - if(!emailAlreadyInUse){ - /* - await createUserWithEmailAndPassword(auth, userEmail, password) - .then((userCredential) => { - // Signed in - const user = userCredential.user; - console.log("user = " + user); - // ... - }) - .catch((error) => { - const errorCode = error.code; - const errorMessage = error.message; - // .. - });*/ - - /*const user = auth.currentUser; - const uid = user.uid; - console.log("UID = " + uid); - - updateProfile(auth.currentUser, { - displayName: String(firstName + " " + lastName), photoURL: "https://example.com/jane-q-user/profile.jpg" - }).then(() => { - console.log("Profile updated"); - // Profile updated! - // ... - }).catch((error) => { - // An error occurred - // ... - });*/ - - const newUser = { - userEmail: userEmail, - firstName: firstName, - lastName: lastName, - username: username, - password: password, - passwordCreatedAt: serverTimestamp(), - question1: question1, - answer1: answer1, - question2: question2, - answer2: answer2, - address: address, - DOB: dateOfBirth, - userCreatedAt: serverTimestamp() - } - - await setDoc(doc(db, 'new_user_requests', username.toString()), newUser); - console.log('New user request for added successfully!'); - - } else{ - alert("User email already in use. Return to the login screen and choose Forgot Password if you are having trouble accessing your account.") - console.log('User email already in use.'); - } - } catch(error) { - console.log(error) - } - - - return true; -}); - -async function generateUsername(firstName, lastName, month, day, year){ - let username = "TBD"; - let userCheck = await testUserName(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year); - let userCount = 0; - if(!userCheck){ - username = String(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year); - return username; - } - while(userCheck){ - userCount++; - username = String(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + userCount + month + year); - userCheck = await testUserName(username); - } - return username; -} - -//BULKY, CAN BE REDUCED IN THE FUTURE - TBD -async function testUserEmail(testEmail){ - testEmail = testEmail.toString(); - const q = query(users, where('userEmail', '==', testEmail)); - const checkEmail = await getDocs(q); - let count = 0; - checkEmail.forEach((doc) => { - count += 1; - }); - console.log("checkEmail = " + count); - if (count > 0){ - return true; - } else{ - return false; - } -} - -//BULKY, CAN BE REDUCED IN THE FUTURE - TBD -async function testUserName(testUsername){ - testUsername = testUsername.toString(); - const docRef = query(users, where('username', '==', testUsername)); - const docCheck = await getDocs(docRef); - let count = 0; - checkEmail.forEach((doc) => { - count += 1; - }); - console.log("checkUsername = " + count); - if (count > 0){ - return true; - } else{ - return false; - } - /*testUsername = testUsername.toString(); - const docRef = doc(db, 'usre', testUsername); - const docCheck = await getDoc(docRef); - if (docCheck.exists()){ - return true; - } else{ - return false; - }*/ -} - -function testValidationFunctions() { - console.log("code reached here!!"); - // Get the values - var userEmail = document.getElementById("user_email").value; - var firstName = document.getElementById("first_name").value; - var lastName = document.getElementById("last_name").value; - var address = document.getElementById("address").value; - var dateOfBirth = document.getElementById("dateofbirth").value; - var password = document.getElementById("password").value; - - // See if its working - if (validateEmail(userEmail)) { - console.log("User Email: " + userEmail); - console.log("Email is Valid"); - } else { - console.log("User Email: " + userEmail); - console.log("!!!!!Email is NOT Valid!!!!"); - } - - if (validateFirstName(firstName)) { - console.log("First Name: " + firstName); - console.log("First Name is Valid"); - } else { - console.log("First Name: " + firstName); - console.log("!!!!!First Name NOT Valid!!!!"); - } - - if (validateLastName(lastName)) { - console.log("Last Name: " + lastName); - console.log("Last Name is Valid"); - } else { - console.log("Last Name: " + lastName); - console.log("!!!!!Last Name NOT Valid!!!!"); - } - - if (validateAddress(address)) { - console.log("Address: " + address); - console.log("Address is Valid"); - } else { - console.log("Address: " + address); - console.log("!!!!!Last Name NOT Valid!!!!"); - } - - if (validateDate(dateOfBirth)) { - console.log("Date of Birth: " + dateOfBirth); - console.log("DOB is Valid"); - } else { - console.log("Date of Birth: " + dateOfBirth); - console.log("!!!!!DOB NOT Valid!!!!"); - } - - if (validatePassword(password)) { - console.log("Password: " + password); - console.log("password is Valid"); - } else { - console.log("Password: " + password); - console.log("!!!!!Password NOT Valid!!!!"); - } - return true; } From 00387eedd59598591e4fc9d90922c29b451fc827 Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 19:55:51 -0400 Subject: [PATCH 04/11] Update sprout.js upgrade from 9.1.1 to 10.4.0 firebase --- public/sprout.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/public/sprout.js b/public/sprout.js index af2750d..7b6529e 100644 --- a/public/sprout.js +++ b/public/sprout.js @@ -1,7 +1,7 @@ -import { initializeApp } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-app.js"; -import { getFirestore } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-firestore.js" -import { collection, doc, getDoc, getDocs, addDoc, setDoc, Timestamp, serverTimestamp } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-firestore.js" -import { query, orderBy, limit, where, onSnapshot } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-firestore.js" +import { initializeApp } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-app.js"; +import { getFirestore } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-firestore.js" +import { collection, doc, getDoc, getDocs, addDoc, setDoc, Timestamp, serverTimestamp } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-firestore.js" +import { query, orderBy, limit, where, onSnapshot } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-firestore.js" import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-auth.js" const firebaseConfig = { From a7fda94f85bf0434da22a3d4c19d67a97266d579 Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 19:56:11 -0400 Subject: [PATCH 05/11] Update createuser.js --- public/createuser.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/public/createuser.js b/public/createuser.js index eb0fd75..af552b7 100644 --- a/public/createuser.js +++ b/public/createuser.js @@ -1,3 +1,25 @@ +import { initializeApp } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-app.js"; +import { getFirestore } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-firestore.js" +import { collection, doc, getDoc, getDocs, addDoc, setDoc, Timestamp, serverTimestamp } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-firestore.js" +import { query, orderBy, limit, where, onSnapshot } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-firestore.js" +import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-auth.js" + +const firebaseConfig = { + apiKey: "AIzaSyDA5itOehOkeLc9ob3a8GsTJ9VhbWdee7I", + authDomain: "sprout-financials.firebaseapp.com", + databaseURL: "https://sprout-financials-default-rtdb.firebaseio.com", + projectId: "sprout-financials", + storageBucket: "sprout-financials.appspot.com", + messagingSenderId: "864423850272", + appId: "1:864423850272:web:725227e1ed9a578ef36745", + measurementId: "G-Z0E9H5Z16M" +}; +const app = initializeApp(firebaseConfig); +const db = getFirestore(app); +const newUserRequest = collection(db, 'new_user_requests'); +const users = collection(db, 'users'); +const auth = getAuth(); + console.log("createuser.js loaded!!") /*Passwords must be: --> a minimum of 8 characters, From 4ca478b43c14e141731d45efe231d1e77360025c Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 19:58:52 -0400 Subject: [PATCH 06/11] Update usertables.js --- public/usertables.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/usertables.js b/public/usertables.js index b629410..a6b23b2 100644 --- a/public/usertables.js +++ b/public/usertables.js @@ -32,7 +32,7 @@ console.log("usertables.js loaded") //--------------------------------------------------admin -document.addEventListener("DOMContentLoaded", function () { +document.addEventListener("DOMContentLoaded", await function () { const extendableTable = document.querySelector(".extendable-table"); const extendedTable = document.querySelector(".extended-table"); From 48bb181886f3919028b556708fed747808540ea0 Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 20:04:18 -0400 Subject: [PATCH 07/11] Update usertables.js --- public/usertables.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/usertables.js b/public/usertables.js index a6b23b2..b4514fe 100644 --- a/public/usertables.js +++ b/public/usertables.js @@ -32,7 +32,7 @@ console.log("usertables.js loaded") //--------------------------------------------------admin -document.addEventListener("DOMContentLoaded", await function () { +document.addEventListener("DOMContentLoaded", async function () { const extendableTable = document.querySelector(".extendable-table"); const extendedTable = document.querySelector(".extended-table"); From a51eccfcab66cad2fdb2babd1a1433fe91a66982 Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 20:27:08 -0400 Subject: [PATCH 08/11] Update createuser.js --- public/createuser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/createuser.js b/public/createuser.js index af552b7..a0e8c83 100644 --- a/public/createuser.js +++ b/public/createuser.js @@ -309,7 +309,7 @@ async function testUserName(testUsername){ return false; } /*testUsername = testUsername.toString(); - const docRef = doc(db, 'usre', testUsername); + const docRef = doc(db, 'user', testUsername); const docCheck = await getDoc(docRef); if (docCheck.exists()){ return true; From cd4769fbdec01c2c8ce50c5070bfe1826c7b3df0 Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 20:27:38 -0400 Subject: [PATCH 09/11] Update createuser.js --- public/createuser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/createuser.js b/public/createuser.js index a0e8c83..f393153 100644 --- a/public/createuser.js +++ b/public/createuser.js @@ -309,7 +309,7 @@ async function testUserName(testUsername){ return false; } /*testUsername = testUsername.toString(); - const docRef = doc(db, 'user', testUsername); + const docRef = doc(db, 'users', testUsername); const docCheck = await getDoc(docRef); if (docCheck.exists()){ return true; From 6ca94bc162a428bf613f8ea01213040e1141665e Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 20:30:34 -0400 Subject: [PATCH 10/11] Update forgotpassword.js add db check for past passwords, correct answers, and then commit changes to db if all is correct --- public/forgotpassword.js | 132 +++++++++++++++++++++++++++++++++++---- 1 file changed, 121 insertions(+), 11 deletions(-) diff --git a/public/forgotpassword.js b/public/forgotpassword.js index a8bd752..b5baf28 100644 --- a/public/forgotpassword.js +++ b/public/forgotpassword.js @@ -1,3 +1,24 @@ +import { initializeApp } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-app.js"; +import { getFirestore } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-firestore.js" +import { collection, doc, getDoc, getDocs, addDoc, setDoc, updateDoc, Timestamp, serverTimestamp } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-firestore.js" +import { query, orderBy, limit, where, onSnapshot } from "https://www.gstatic.com/firebasejs/10.4.0/firebase-firestore.js" +import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.1.1/firebase-auth.js" + +const firebaseConfig = { + apiKey: "AIzaSyDA5itOehOkeLc9ob3a8GsTJ9VhbWdee7I", + authDomain: "sprout-financials.firebaseapp.com", + databaseURL: "https://sprout-financials-default-rtdb.firebaseio.com", + projectId: "sprout-financials", + storageBucket: "sprout-financials.appspot.com", + messagingSenderId: "864423850272", + appId: "1:864423850272:web:725227e1ed9a578ef36745", + measurementId: "G-Z0E9H5Z16M" +}; +const app = initializeApp(firebaseConfig); +const db = getFirestore(app); +const users = collection(db, 'users'); +const user = await fetchUser(username, userEmail); + console.log("forgotpassword.js has loaded!!!"); function showError(input, message) { @@ -8,15 +29,67 @@ function showError(input, message) { } -// function validateEmail(email) { -// var emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; -// return emailPattern.test(email); -// } +/*Passwords must be: +--> a minimum of 8 characters, +--> must start with a letter, +--> must have a letter, +--> a number and special character +*/ +function validatePassword(password) { + var passwordPattern = /^(?=[A-Za-z])(?=.*\d)(?=.*[!@#$%^&*()-+=<>?]).{8,}$/; + return passwordPattern.test(password); +} -// function validateUserName(username) { +async function validateNewPassword(password, user){ + var passwordUnusued = true; + let currentPassword = user.password; + if(String(password) == String(currentPassword)){ + console.log("current password used"); + return false; + } + if(user.hasOwnProperty('oldPasswords')){ + console.log("has old passwords"); + let oldPasswords = user.oldPasswords; + oldPasswords.forEach((pass) => { + if String(pass) = String(password){ + console.log("old password used"); + return false; + } + }); + } + return true; +} -// } +async function fetchUser(username, userEmail){ + const userData = []; + username = username.toString(); + userEmail = userEmail.toString(); + const q = query(users, where('username', '==', username)); + const getUsers = await getDocs(q).then((querySnapshot) => { + const tempDoc = []; + tempDoc.push({ id: doc.id, ...doc.data() }); + userData = tempDoc; + }) + if(userData.userEmail == userEmail){ + return userData; + } else { + console.log("userData error, userData = " + userData); + return false; + } + +} + +document.addEventListener("DOMContentLoaded", async function () { + console.log("questions are loaded"); + const question1 = user.question1; + const question2 = user.question2; + document.getElementById("question1").textcontent=String(question1); + console.log(question1); + document.getElementById("question2").textcontent=String(question2); + console.log(question2); + return true; +}); document.getElementById("password_form").addEventListener("submit", async function (e) { e.preventDefault(); @@ -26,13 +99,14 @@ document.getElementById("password_form").addEventListener("submit", async functi const userNameElement = document.getElementById("username"); const answer1Element = document.getElementById("answer1"); const answer2Element = document.getElementById("answer2"); - + const answer2Element = document.getElementById("password"); var userEmail = userEmailElement.value; var username = userNameElement.value; var answer1 = answer1Element.value; var answer2 = answer2Element.value; + var isValid = true; if (userEmail == '') { @@ -46,7 +120,7 @@ document.getElementById("password_form").addEventListener("submit", async functi showError(userNameElement, errorMessage); isValid = false; } - + if (answer1 == '') { var errorMessage = "Please enter an answer."; showError(answer1Element, errorMessage); @@ -58,11 +132,47 @@ document.getElementById("password_form").addEventListener("submit", async functi showError(answer2Element, errorMessage); isValid = false; } - - + if (!validatePassword(password)) { + var errorMessage = 'Passwords must be at least 8 characters, start with a letter, and contain a number and a special character' + if (password == '') { + errorMessage = "Please enter a password." + } + + showError(passwordElement, errorMessage); + isValid = false; + } + if (!validateNewPassword(password)) { + var errorMessage = 'Passwords used in the past cannot be re-used.' + if (password == '') { + errorMessage = "Please enter a password." + } + + showError(passwordElement, errorMessage); + isValid = false; + } if (!isValid) { return false; } + let oldPasswords = [] -}); \ No newline at end of file + if(user.hasOwnProperty('oldPasswords')){ + console.log("updating old passwords"); + oldPasswords = user.oldPasswords; + oldPasswords.push(user.password); + }else{ + oldPasswords.push(user.password); + } + + const userRef = doc(db, 'users', username.toString()); + + await updateDoc(userRef, { + password: password, + passwordCreatedAt: serverTimestamp(), + oldPasswords: oldPasswords + }); + + console.log('User updated successfully!'); + + return true; +}); From 190fe5b865f412896d8d99ad56dc2a4ad004d1b7 Mon Sep 17 00:00:00 2001 From: Camille Reaves Date: Sun, 24 Sep 2023 20:31:05 -0400 Subject: [PATCH 11/11] Update and rename password_page_1.html to forgot_password.html --- ...sword_page_1.html => forgot_password.html} | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) rename public/{password_page_1.html => forgot_password.html} (82%) diff --git a/public/password_page_1.html b/public/forgot_password.html similarity index 82% rename from public/password_page_1.html rename to public/forgot_password.html index d3cf8f9..89337f2 100644 --- a/public/password_page_1.html +++ b/public/forgot_password.html @@ -3,10 +3,10 @@ - Document + Sprout Financials - - + + @@ -33,20 +33,29 @@ - +
+ Error Message
-
+ Error Message
+ +
+ + + Error Message +
+ +
@@ -85,4 +94,4 @@ - \ No newline at end of file +