Skip to content

Commit

Permalink
Merge branch 'main' into testBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyDeSantiago committed Sep 23, 2023
2 parents dfd7e58 + 11e1be9 commit 789f4f1
Showing 1 changed file with 77 additions and 38 deletions.
115 changes: 77 additions & 38 deletions public/sprout.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
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, getDocs, addDoc, 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 { 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"

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);
Expand All @@ -47,60 +38,45 @@ console.log("sprout.js loaded!!")
--> 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);
}

Expand All @@ -111,7 +87,7 @@ function showError(input, message) {
small.innerText = message
}

document.getElementById("login_form").addEventListener("submit", function (e) {
document.getElementById("login_form").addEventListener("submit", async function (e) {
e.preventDefault();
const userEmailElement = document.getElementById("user_email");
const firstNameElement = document.getElementById("first_name");
Expand Down Expand Up @@ -188,11 +164,19 @@ document.getElementById("login_form").addEventListener("submit", function (e) {
if (!isValid) {
return false;
}
else{

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);

const newUser = {
userEmail: userEmail,
firstName: firstName,
Expand All @@ -201,15 +185,70 @@ document.getElementById("login_form").addEventListener("submit", function (e) {
DOB : dateOfBirth,
password: password,
createdAt: serverTimestamp(),
username: firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year
username: username
}

addDoc(newUserRequest, newUser);
console.log('Document added or updated successfully!');

let emailAlreadyInUse = await testUserEmail(userEmail);
console.log("emailAlreadyInUse = "+ emailAlreadyInUse);

if(!emailAlreadyInUse){
await setDoc(doc(db, 'new_user_requests', username.toString()), newUser);
console.log('New user request 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)
}
console.log("everything validated")


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(newUserRequest, 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;
}
}

async function testUserName(testUsername){
testUsername = testUsername.toString();
const docRef = doc(db, 'new_user_requests', testUsername);
const docCheck = await getDoc(docRef);
if (docCheck.exists()){
return true;
} else{
return false;
}
}

function testValidationFunctions() {
console.log("code reached here!!");
Expand All @@ -224,10 +263,10 @@ function testValidationFunctions() {
// See if its working
if (validateEmail(userEmail)) {
console.log("User Email: " + userEmail);
console.log("email is Valid");
console.log("Email is Valid");
} else {
console.log("User Email: " + userEmail);
console.log("!!!!!email is NOT Valid!!!!");
console.log("!!!!!Email is NOT Valid!!!!");
}

if (validateFirstName(firstName)) {
Expand Down Expand Up @@ -267,7 +306,7 @@ function testValidationFunctions() {
console.log("password is Valid");
} else {
console.log("Password: " + password);
console.log("!!!!!password NOT Valid!!!!");
console.log("!!!!!Password NOT Valid!!!!");
}


Expand Down

0 comments on commit 789f4f1

Please sign in to comment.