From b771a989f1f923bb90652312ad3290c200bc0cb3 Mon Sep 17 00:00:00 2001 From: LeviM0323 Date: Fri, 30 Oct 2026 16:32:34 -0400 Subject: [PATCH] Mobile lab 5 --- .gitignore | 7 - INFO-3170 JavaScript 3/Labs/Lab 2/library-app | 1 + .../Labs/Lab 5/firebase-DB/.expo/README.md | 13 + .../Labs/Lab 5/firebase-DB/.expo/devices.json | 3 + .../Labs/Lab 5/firebase-DB/App.js | 309 + .../Labs/Lab 5/firebase-DB/FirebaseConfig.js | 21 + .../Labs/Lab 5/firebase-DB/app.json | 29 + .../firebase-DB/assets/adaptive-icon.png | Bin 0 -> 17547 bytes .../Labs/Lab 5/firebase-DB/assets/favicon.png | Bin 0 -> 1466 bytes .../Labs/Lab 5/firebase-DB/assets/icon.png | Bin 0 -> 22380 bytes .../Lab 5/firebase-DB/assets/splash-icon.png | Bin 0 -> 17547 bytes .../Labs/Lab 5/firebase-DB/index.js | 8 + .../Labs/Lab 5/firebase-DB/package-lock.json | 10111 ++++++++++++++++ .../Labs/Lab 5/firebase-DB/package.json | 23 + 14 files changed, 10518 insertions(+), 7 deletions(-) create mode 160000 INFO-3170 JavaScript 3/Labs/Lab 2/library-app create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/.expo/README.md create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/.expo/devices.json create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/App.js create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/FirebaseConfig.js create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/app.json create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/assets/adaptive-icon.png create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/assets/favicon.png create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/assets/icon.png create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/assets/splash-icon.png create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/index.js create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/package-lock.json create mode 100644 INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/package.json diff --git a/.gitignore b/.gitignore index 8492408..2948510 100644 --- a/.gitignore +++ b/.gitignore @@ -22,13 +22,6 @@ node_modules/ dist/ coverage/ -# Python -__pycache__/ -*.py[cod] -.venv/ -venv/ -.pytest_cache/ - # Java and Android build output .gradle/ build/ diff --git a/INFO-3170 JavaScript 3/Labs/Lab 2/library-app b/INFO-3170 JavaScript 3/Labs/Lab 2/library-app new file mode 160000 index 0000000..3edf8f2 --- /dev/null +++ b/INFO-3170 JavaScript 3/Labs/Lab 2/library-app @@ -0,0 +1 @@ +Subproject commit 3edf8f21bbdf03e2f2d30bac5dc85e2ad31fce29 diff --git a/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/.expo/README.md b/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/.expo/README.md new file mode 100644 index 0000000..ce8c4b6 --- /dev/null +++ b/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/.expo/README.md @@ -0,0 +1,13 @@ +> Why do I have a folder named ".expo" in my project? + +The ".expo" folder is created when an Expo project is started using "expo start" command. + +> What do the files contain? + +- "devices.json": contains information about devices that have recently opened this project. This is used to populate the "Development sessions" list in your development builds. +- "settings.json": contains the server configuration that is used to serve the application manifest. + +> Should I commit the ".expo" folder? + +No, you should not share the ".expo" folder. It does not contain any information that is relevant for other developers working on the project, it is specific to your machine. +Upon project creation, the ".expo" folder is already added to your ".gitignore" file. diff --git a/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/.expo/devices.json b/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/.expo/devices.json new file mode 100644 index 0000000..5efff6c --- /dev/null +++ b/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/.expo/devices.json @@ -0,0 +1,3 @@ +{ + "devices": [] +} diff --git a/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/App.js b/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/App.js new file mode 100644 index 0000000..3b9967f --- /dev/null +++ b/INFO-3173 Mobile Development 1/Labs/Lab 5/firebase-DB/App.js @@ -0,0 +1,309 @@ +import { StatusBar } from "expo-status-bar"; +import React, { useState, useEffect } from "react"; +import { View, TextInput, Text, Button, Alert, StyleSheet } from "react-native"; +import { db, firestore, auth } from "./FirebaseConfig"; +import { + createUserWithEmailAndPassword, + signOut, + signInWithEmailAndPassword, + onAuthStateChanged, +} from "firebase/auth"; +import { ref, set, onValue } from "firebase/database"; +import { doc, setDoc, onSnapshot } from "firebase/firestore"; + +export default function App() { + const [registrationEmail, setRegistrationEmail] = useState(""); + const [registrationPassword, setRegistrationPassword] = useState(""); + const [loginEmail, setLoginEmail] = useState(""); + const [loginPassword, setLoginPassword] = useState(""); + const [loggedIn, setLoggedIn] = useState(false); + const [databaseData, setDatabaseData] = useState(""); + const [user, setUser] = useState(null); + + // Monitor authentication state + useEffect(() => { + const unsubscribe = onAuthStateChanged(auth, (currentUser) => { + setUser(currentUser); + setLoggedIn(!!currentUser); + }); + return () => unsubscribe(); + }, []); + + const registerWithFirebase = () => { + if (registrationEmail.length < 4) { + Alert.alert("Please enter an email address."); + return; + } + + if (registrationPassword.length < 4) { + Alert.alert("Please enter a password."); + return; + } + + createUserWithEmailAndPassword( + auth, + registrationEmail, + registrationPassword + ) + .then((userCredential) => { + Alert.alert("User registered successfully!"); + setRegistrationEmail(""); + setRegistrationPassword(""); + }) + .catch((error) => { + console.log("Registration error:", error); + if (error.code === "auth/weak-password") { + Alert.alert("The password is too weak."); + } else if (error.code === "auth/email-already-in-use") { + Alert.alert("This email is already registered."); + } else { + Alert.alert(error.message); + } + }); + }; + + const loginWithFirebase = () => { + if (loginEmail.length < 4) { + Alert.alert("Please enter an email address."); + return; + } + + if (loginPassword.length < 4) { + Alert.alert("Please enter a password."); + return; + } + + signInWithEmailAndPassword(auth, loginEmail, loginPassword) + .then((userCredential) => { + Alert.alert("User logged in successfully!"); + setLoginEmail(""); + setLoginPassword(""); + }) + .catch((error) => { + console.log("Login error:", error); + if (error.code === "auth/wrong-password") { + Alert.alert("Wrong password."); + } else if (error.code === "auth/user-not-found") { + Alert.alert("User not found."); + } else { + Alert.alert(error.message); + } + }); + }; + + const signoutWithFirebase = () => { + signOut(auth) + .then(() => { + Alert.alert("User was logged out!"); + setDatabaseData(""); + }) + .catch((error) => { + Alert.alert("Error signing out:", error.message); + }); + }; + + const saveDataWithFirebase = () => { + if (!user) { + Alert.alert("Please log in first."); + return; + } + + const userID = user.uid; + + // Save to Realtime DB + set(ref(db, "users/" + userID), { + text: databaseData, + timestamp: Date.now(), + }) + .then(() => { + console.log("Data saved to Realtime DB"); + }) + .catch((error) => { + console.log("Realtime DB error:", error); + }); + + // Save to Firestore + setDoc( + doc(firestore, "users", userID), + { + text: databaseData, + timestamp: Date.now(), + }, + { merge: true } + ) + .then(() => { + Alert.alert("Data saved successfully!"); + }) + .catch((error) => { + Alert.alert("Error saving data"); + console.log("Firestore error:", error); + }); + }; + + const retrieveDataWithFirebase = () => { + if (!user) { + Alert.alert("Please log in first."); + return; + } + + const userID = user.uid; + const dbRef = ref(db, "users/" + userID); + + // Using Realtime DB + onValue(dbRef, (snapshot) => { + if (snapshot.exists()) { + const data = snapshot.val(); + setDatabaseData(data.text || ""); + Alert.alert("Data retrieved from Realtime DB"); + } else { + Alert.alert("No data found"); + } + }); + + // Uncomment for Firestore + /* + const unsubscribe = onSnapshot(doc(firestore, "users", userID), (doc) => { + if (doc.exists()) { + const data = doc.data(); + setDatabaseData(data.text || ""); + Alert.alert("Data retrieved from Firestore"); + } else { + Alert.alert("No data found in Firestore"); + } + }); + */ + }; + + return ( + + + + {!loggedIn ? ( + + + Lab 5 - l_mclean215318 + Register with Firebase + + +