Skip to content

Commit

Permalink
Update sprout.js
Browse files Browse the repository at this point in the history
  • Loading branch information
camillereaves authored Sep 23, 2023
1 parent c037cd2 commit ccad84b
Showing 1 changed file with 66 additions and 45 deletions.
111 changes: 66 additions & 45 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, getDoc, 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"

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 @@ -42,60 +33,45 @@ console.log("sprout.js loaded!!")
*/

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 Down Expand Up @@ -148,28 +124,73 @@ document.getElementById("login_form").addEventListener("submit", function (e) {
return false;
}
else{
const date = new Date();
let month = String(date.getMonth()+1).padStart(2,"0");
let year = String(date.getFullYear()).slice(2);

const newUser = {
userEmail: userEmail,
firstName: firstName,
lastName: lastName,
address: address,
DOB : dateOfBirth,
password: password,
createdAt: serverTimestamp(),
username: firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year
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 year = String(date.getFullYear()).slice(2);

let userNameCount = testUsername(firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year);

if(userNameCount > 0){
let username = firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year + userNameCount;
}
else{
let username = firstName.slice(0,1).toLowerCase() + lastName.toLowerCase() + month + year;
}

const newUser = {
userEmail: userEmail,
firstName: firstName,
lastName: lastName,
address: address,
DOB : dateOfBirth,
password: password,
createdAt: serverTimestamp(),
username: username;
}

emailAlreadyInUse = testUserEmail(userEmail);

if(!emailAlreadyInUse){
addDoc(newUserRequest, newUser, username);
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)
}

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

return true;
})

async function testUserEmail(testEmail){
const query = await getCountFromServer(newUserRequest.where('UserEmail', '==', testEmail));

if (!query.empty) {
exists = true;
} else {
exists = false;
}

return exists;
}

async function testUserName(testUsername){
const docCheck = await getDoc(doc(db, newUserRequest, username);

if (!query.empty) {
count = query.data().count;
} else {
count = 0;
}

return count;
}

function testValidationFunctions() {
console.log("code reached here!!");
// Get the values
Expand All @@ -183,10 +204,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 @@ -226,7 +247,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 ccad84b

Please sign in to comment.