From f7a5a7e9d1126806fd52e15f7b850a536a9cf2d0 Mon Sep 17 00:00:00 2001 From: FEguare <123463013+FEguare@users.noreply.github.com> Date: Thu, 3 Apr 2025 18:22:55 +0100 Subject: [PATCH 1/7] draft 1 --- client/src/AccountScreen.js | 77 ++++++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/client/src/AccountScreen.js b/client/src/AccountScreen.js index 0ee6483..da6f939 100644 --- a/client/src/AccountScreen.js +++ b/client/src/AccountScreen.js @@ -2,34 +2,65 @@ import * as React from 'react'; import { View, SafeAreaView, TouchableOpacity, Text } from 'react-native'; import styles from './components/styles/AccountScreen.styles'; import buttonStyles from './components/common/button'; +import { retrieveData, removeData } from './caching'; export default function AccountScreen({ navigation }) { + const logout = async () => { + try { + await removeData('username'); + } catch (error) { + console.error('Error logging out:', error); + } + navigation.replace('AccountScreen'); + // navigation.navigate('Map'); ???? + }; return ( - navigation.navigate('LoginScreen')} - color="#841584" - > - Log In - - - navigation.navigate('SignUpScreen')} - color="#841584" - > - Sign Up - - - navigation.navigate('Map')} - color="#841584" - > - Continue as Guest - + {retrieveData('username') === null ? ( + <> + navigation.navigate('LoginScreen')} + color="#841584" + > + Log In + + navigation.navigate('SignUpScreen')} + color="#841584" + > + Sign Up + + navigation.navigate('Map')} + color="#841584" + > + Continue as Guest + + + ) : ( + <> + + Log Out + + {/* maybe just have the logout button here????? */} + You are logged in + navigation.navigate('Map')} + color="#841584" + > + Go to Map + + + )} ); From 27206ec132bc5ac1412d1dd1a835bbfc32e1e8e6 Mon Sep 17 00:00:00 2001 From: FEguare <123463013+FEguare@users.noreply.github.com> Date: Fri, 4 Apr 2025 09:15:14 +0100 Subject: [PATCH 2/7] button name change --- client/src/Map.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/Map.js b/client/src/Map.js index 1adf087..35a70f1 100644 --- a/client/src/Map.js +++ b/client/src/Map.js @@ -108,7 +108,7 @@ export default function MapScreen({ navigation }) { style={MapStyles.loginButton} onPress={() => navigation.navigate('AccountScreen')} > - Log In + Account Date: Fri, 4 Apr 2025 09:28:41 +0100 Subject: [PATCH 3/7] nav logic fixed on open for logged in user. redundant file deleted --- client/App.js | 19 ++- client/src/map(temp).js | 294 ---------------------------------------- 2 files changed, 14 insertions(+), 299 deletions(-) delete mode 100644 client/src/map(temp).js diff --git a/client/App.js b/client/App.js index d3c38fe..9300972 100644 --- a/client/App.js +++ b/client/App.js @@ -12,6 +12,7 @@ import AccountScreen from './src/AccountScreen'; import SignUpScreen from './src/SignUp'; import DisplayRouteScreen from './src/DisplayRoute'; import FriendsScreen from './src/FriendsScreen'; +import { retrieveData } from './src/caching'; const Stack = createNativeStackNavigator(); @@ -24,11 +25,19 @@ export default function App() { name="Map" component={MapScreen} /> - + {retrieveData('username') === null ? ( + + ) : ( + + )} diff --git a/client/src/map(temp).js b/client/src/map(temp).js deleted file mode 100644 index 04100dc..0000000 --- a/client/src/map(temp).js +++ /dev/null @@ -1,294 +0,0 @@ -// import React, { useState, useEffect, useRef } from 'react'; -// import { -// StyleSheet, -// View, -// Text, -// TouchableOpacity, -// Alert, -// Platform, -// } from 'react-native'; -// import MapView, { Marker, Polyline } from 'react-native-maps'; -// import * as Location from 'expo-location'; -// import { decode } from '@googlemaps/polyline-codec'; // For decoding the polyline - -// export default function MapScreen({ navigation }) { -// const [webSocket, setWebSocket] = useState(null); -// const [location, setLocation] = useState(null); // No hardcoded initial location -// const [errorMessage, setErrorMessage] = useState(''); -// const [polylineCoordinates, setPolylineCoordinates] = useState([]); // State for polyline coordinates -// const mapRef = useRef(null); // Reference to the MapView - -// // WebSocket Setup -// useEffect(() => { -// const wsUrl = `${process.env.EXPO_PUBLIC_API_URL.replace(/^http/, 'ws')}/ws/location`; -// console.log('Connecting to WebSocket:', wsUrl); - -// const socket = new WebSocket(wsUrl); - -// socket.onopen = () => { -// console.log('WebSocket connection opened'); -// setWebSocket(socket); -// }; - -// socket.onmessage = (event) => { -// console.log('Message from server:', event.data); -// }; - -// socket.onerror = (error) => { -// console.error('WebSocket error:', error); -// }; - -// socket.onclose = () => { -// console.log('WebSocket connection closed'); -// }; - -// return () => { -// socket.close(); -// }; -// }, []); - -// // Location Setup -// useEffect(() => { -// (async () => { -// const { status } = await Location.requestForegroundPermissionsAsync(); -// if (status !== 'granted') { -// setErrorMessage('Permission to access location was denied.'); -// return; -// } - -// // Fetch initial location -// const currentLocation = await Location.getCurrentPositionAsync({ -// accuracy: Location.Accuracy.BestForNavigation, -// }); -// const { latitude, longitude } = currentLocation.coords; -// setLocation({ latitude, longitude }); - -// // Set up continuous location tracking -// const locationSubscription = await Location.watchPositionAsync( -// { -// accuracy: Location.Accuracy.BestForNavigation, -// timeInterval: 5000, // Update every 5 seconds -// distanceInterval: 5, // Update if device moves 5 meters -// }, -// (newLocation) => { -// const { latitude, longitude } = newLocation.coords; -// console.log('Updated Location:', { latitude, longitude }); -// setLocation({ latitude, longitude }); - -// // Animate the map to the new region -// if (mapRef.current) { -// mapRef.current.animateToRegion( -// { -// latitude, -// longitude, -// latitudeDelta: 0.01, -// longitudeDelta: 0.01, -// }, -// 1000, // Animation duration in milliseconds -// ); -// } -// }, -// ); - -// return () => locationSubscription.remove(); -// })(); -// }, []); - -// // Function to Fetch and Decode Polyline -// const fetchPolyline = async () => { -// try { -// const response = await fetch( -// `${process.env.EXPO_PUBLIC_API_URL}/wayfinding/get_routes`, -// { -// method: 'POST', -// headers: { 'Content-Type': 'application/json' }, -// body: JSON.stringify({ -// origin: 'Tara Street, Dublin', -// destination: 'Ashbourne, Ireland', -// mode: 'walking', -// alternatives: false, -// }), -// }, -// ); - -// const data = await response.json(); - -// if (data.route) { -// setPolylineCoordinates(data.route); // The response is already a list of { latitude, longitude } -// } -// } catch (error) { -// console.error('Error fetching polyline:', error); -// } -// }; - -// // Function to Send Location to WebSocket -// const sendLocation = () => { -// if (webSocket && location) { -// const locationData = JSON.stringify(location); -// webSocket.send(locationData); -// console.log('Sent location:', locationData); -// } else if (!location) { -// Alert.alert( -// 'Location not available', -// 'Please wait for the GPS to fetch your location.', -// ); -// } else { -// Alert.alert( -// 'WebSocket not connected', -// 'Please wait for the WebSocket connection to establish.', -// ); -// } -// }; - -// // Fetch polyline when the component mounts -// useEffect(() => { -// fetchPolyline(); -// }, []); - -// return ( -// -// {errorMessage ? ( -// {errorMessage} -// ) : location ? ( -// <> -// -// -// -// {/* Render the polyline */} -// {polylineCoordinates.length > 0 && ( -// -// )} -// -// -// Send Location -// -// navigation.navigate('LoginScreen')} -// > -// Log In -// -// navigation.navigate('FindRouteScreen')} -// > -// Find Route -// -// navigation.navigate('WeatherScreen')} -// > -// Weather -// -// -// ) : ( -// Fetching your location... -// )} -// -// ); -// } - -// const styles = StyleSheet.create({ -// container: { -// flex: 1, -// ...(Platform.OS === 'web' ? { height: '100vh' } : {}), -// }, -// map: { -// flex: 1, -// minHeight: 300, -// }, -// sendButton: { -// position: 'absolute', -// bottom: 20, -// left: '50%', -// transform: [{ translateX: -50 }], -// backgroundColor: '#007bff', -// paddingVertical: 10, -// paddingHorizontal: 20, -// borderRadius: 10, -// }, -// buttonText: { -// color: '#fff', -// fontWeight: 'bold', -// textAlign: 'center', -// }, -// TouchableOpacity: { -// position: 'absolute', -// alignItems: 'center', -// left: '70%', -// top: '0%', -// backgroundColor: '#007bff', -// paddingVertical: 10, -// paddingHorizontal: 40, -// borderRadius: 10, -// shadowColor: '#000', -// shadowOpacity: 0.2, -// shadowOffset: { width: 0, height: 2 }, -// shadowRadius: 5, -// elevation: 5, -// }, -// TouchableOpacity1: { -// position: 'absolute', -// alignItems: 'center', -// left: '0%', -// top: '0%', -// backgroundColor: '#007bff', -// paddingVertical: 10, -// paddingHorizontal: 40, -// borderRadius: 10, -// shadowColor: '#000', -// shadowOpacity: 0.2, -// shadowOffset: { width: 0, height: 2 }, -// shadowRadius: 5, -// elevation: 5, -// }, -// findRouteButton: { -// position: 'absolute', -// alignItems: 'center', -// left: '34%', -// top: '0%', -// backgroundColor: '#007bff', -// paddingVertical: 10, -// paddingHorizontal: 35, -// borderRadius: 10, -// shadowColor: '#000', -// shadowOpacity: 0.2, -// shadowOffset: { width: 0, height: 2 }, -// shadowRadius: 5, -// elevation: 5, -// }, -// error: { -// flex: 1, -// textAlign: 'center', -// textAlignVertical: 'center', -// fontSize: 18, -// color: 'red', -// }, -// loadingText: { -// flex: 1, -// textAlign: 'center', -// textAlignVertical: 'center', -// fontSize: 18, -// }, -// }); From 77b6b30e913044880c10a3489cb12d3c3c6b19de Mon Sep 17 00:00:00 2001 From: FEguare <123463013+FEguare@users.noreply.github.com> Date: Fri, 4 Apr 2025 09:36:13 +0100 Subject: [PATCH 4/7] initial screen routing fix --- client/App.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/client/App.js b/client/App.js index 9300972..94e25a6 100644 --- a/client/App.js +++ b/client/App.js @@ -17,27 +17,21 @@ import { retrieveData } from './src/caching'; const Stack = createNativeStackNavigator(); export default function App() { + const firstScreen = + retrieveData('username') === null ? 'AccountScreen' : 'Map'; return ( - + - {retrieveData('username') === null ? ( - - ) : ( - - )} + From 2d49966acf3cbb633c2a901706cb792e892d9958 Mon Sep 17 00:00:00 2001 From: JasonBoutlas Date: Fri, 4 Apr 2025 12:44:30 +0100 Subject: [PATCH 5/7] logout changes --- client/src/AccountScreen.js | 31 ++++++++++++++++++++----------- client/src/caching.jsx | 1 - client/src/utils/accountUtils.js | 1 - 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/client/src/AccountScreen.js b/client/src/AccountScreen.js index da6f939..5e3651a 100644 --- a/client/src/AccountScreen.js +++ b/client/src/AccountScreen.js @@ -1,23 +1,39 @@ -import * as React from 'react'; +import React, { useEffect, useState } from 'react'; import { View, SafeAreaView, TouchableOpacity, Text } from 'react-native'; import styles from './components/styles/AccountScreen.styles'; import buttonStyles from './components/common/button'; import { retrieveData, removeData } from './caching'; + export default function AccountScreen({ navigation }) { + + const [usernameValid, setUsernameValid] = useState('') + + useEffect(() => { + const fetchUsername = async () => { + try { + const username = await retrieveData('username'); + setUsernameValid(username); + } catch (error) { + console.error('Error retrieving username:', error); + } + }; + fetchUsername(); +},[]); + const logout = async () => { try { await removeData('username'); + setUsernameValid(null); + navigation.navigate('Map'); } catch (error) { console.error('Error logging out:', error); } - navigation.replace('AccountScreen'); - // navigation.navigate('Map'); ???? }; return ( - {retrieveData('username') === null ? ( + {usernameValid === null ? ( <> {/* maybe just have the logout button here????? */} You are logged in - navigation.navigate('Map')} - color="#841584" - > - Go to Map - )} diff --git a/client/src/caching.jsx b/client/src/caching.jsx index da5c5d9..6b1871a 100644 --- a/client/src/caching.jsx +++ b/client/src/caching.jsx @@ -25,7 +25,6 @@ export const retrieveData = async (key) => { export const removeData = async (key) => { try { await AsyncStorage.removeItem(JSON.stringify(key)); - console.log('Removed successfully'); } catch { console.error('Error removing data from cache'); } diff --git a/client/src/utils/accountUtils.js b/client/src/utils/accountUtils.js index 78b17ae..a405efc 100644 --- a/client/src/utils/accountUtils.js +++ b/client/src/utils/accountUtils.js @@ -30,7 +30,6 @@ export const handleLogin = async (username, password, navigation) => { // Await response and print message from server const data = await response.json(); - alert(data.message); if (retrieveData('username') !== null) { await removeData('username'); From 82f1bb12d182cde4bdbca4ec3d20a9cfa8238ccf Mon Sep 17 00:00:00 2001 From: JasonBoutlas Date: Fri, 4 Apr 2025 12:58:22 +0100 Subject: [PATCH 6/7] more logout fixes --- client/src/AccountScreen.js | 4 +- client/src/LogIn.js | 19 +---- client/src/utils/accountUtils.js | 122 +++++++++++++++---------------- 3 files changed, 63 insertions(+), 82 deletions(-) diff --git a/client/src/AccountScreen.js b/client/src/AccountScreen.js index 5e3651a..0f34f10 100644 --- a/client/src/AccountScreen.js +++ b/client/src/AccountScreen.js @@ -6,8 +6,7 @@ import { retrieveData, removeData } from './caching'; export default function AccountScreen({ navigation }) { - - const [usernameValid, setUsernameValid] = useState('') + const [usernameValid, setUsernameValid] = useState(null) useEffect(() => { const fetchUsername = async () => { @@ -66,7 +65,6 @@ export default function AccountScreen({ navigation }) { > Log Out - {/* maybe just have the logout button here????? */} You are logged in )} diff --git a/client/src/LogIn.js b/client/src/LogIn.js index 509b2dd..ee10cfb 100644 --- a/client/src/LogIn.js +++ b/client/src/LogIn.js @@ -9,7 +9,7 @@ import { } from 'react-native'; import { handleLogin } from './utils/accountUtils'; import styles from './components/styles/Login.styles'; -import { retrieveData } from './caching'; + export default function LoginScreen({ navigation }) { const [username, setUsername] = useState(''); @@ -19,8 +19,6 @@ export default function LoginScreen({ navigation }) { return ( - {retrieveData('username') === null ? ( - <> ref2.current.focus()} /> - - handleLogin(username, password, navigation)} @@ -45,19 +41,6 @@ export default function LoginScreen({ navigation }) { > Log In - - ) : ( - <> - You are logged in - navigation.navigate('Map')} - color="#841584" - > - Go to Map - - - )} ); diff --git a/client/src/utils/accountUtils.js b/client/src/utils/accountUtils.js index bbc2b77..4d39099 100644 --- a/client/src/utils/accountUtils.js +++ b/client/src/utils/accountUtils.js @@ -1,65 +1,49 @@ import { Platform } from 'react-native'; -import CryptoJS from 'crypto-js'; import { storeData, retrieveData, removeData, updateData } from '../caching'; -export const postConnection = async (url, payload, customBaseUrl = null) => { - try { - const baseUrl = - customBaseUrl || - (Platform.OS === 'web' - ? 'http://localhost:8000' - : process.env.EXPO_PUBLIC_API_URL); - console.log(`Sending request to ${baseUrl}/${url}`); - - const response = await fetch(`${baseUrl}/${url}`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(payload), - }); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const data = await response.json(); - alert(data.message); - - return data; - } catch (error) { - console.error('Error details:', error); - throw error; - } -}; - export const handleLogin = async (username, password, navigation) => { if (username === '' || password === '') { alert('All fields have to be filled before logging in!'); } else { console.log('username: ', username); - - // hash password - const hashedPassword = CryptoJS.SHA256(password).toString(); - console.log('Hashed password:', hashedPassword); - + console.log('password: ', password); navigation.navigate('Map'); - const url = 'login'; - const payload = { username, password: hashedPassword }; + try { + const baseUrl = + Platform.OS === 'web' + ? 'http://localhost:8000' + : process.env.EXPO_PUBLIC_API_URL; + console.log(`Sending request to ${baseUrl}/login`); + + const response = await fetch(`${baseUrl}/login`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } - const data = await postConnection(url, payload); - alert(data.message); + // Await response and print message from server + const data = await response.json(); - if (data.message === `Login successful for user: ${username}`) { if (retrieveData('username') !== null) { await removeData('username'); await updateData('username', username); } else { await storeData('username', username); } + + // if the data is "success", then cache details + + console.log('Server response:', data); + } catch (error) { + console.error('Error details:', error); } - console.log('Server response:', data); } }; @@ -68,28 +52,44 @@ export const handleSignup = async (username, password, navigation) => { alert('All fields have to be filled before signing up!'); } else { console.log('username: ', username); - - // hash password - const hashedPassword = CryptoJS.SHA256(password).toString(); - console.log('Hashed password:', hashedPassword); - + console.log('password: ', password); navigation.navigate('Map'); - const url = 'signup'; - const payload = { username, password: hashedPassword }; - - const data = await postConnection(url, payload); - alert(data.message); + try { + const baseUrl = + Platform.OS === 'web' + ? 'http://localhost:8000' + : process.env.EXPO_PUBLIC_API_URL; + console.log(`Sending request to ${baseUrl}/signup`); + + const response = await fetch(`${baseUrl}/signup`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } - if (data.message === `Signup successful for user: ${username}`) { - if (retrieveData('username') !== null) { - await removeData('username'); - await updateData('username', username); - } else { - await storeData('username', username); + // Await response and print message from server + const data = await response.json(); + //alert(data.message); + + if (data.message === `Signup successful for user: ${username}`) { + if (retrieveData('username') !== null) { + await removeData('username'); + await updateData('username', username); + } else { + await storeData('username', username); + } } - } - console.log('Server response:', data); + console.log('Server response:', data); + } catch (error) { + console.error('Error details:', error); + } } }; From 2800ea9d0492f482115cd0d9e7fb6a5e4876913d Mon Sep 17 00:00:00 2001 From: JasonBoutlas Date: Fri, 4 Apr 2025 13:03:31 +0100 Subject: [PATCH 7/7] more fixes --- client/src/utils/accountUtils.js | 122 +++++++++++++++---------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/client/src/utils/accountUtils.js b/client/src/utils/accountUtils.js index 4d39099..bbc2b77 100644 --- a/client/src/utils/accountUtils.js +++ b/client/src/utils/accountUtils.js @@ -1,49 +1,65 @@ import { Platform } from 'react-native'; +import CryptoJS from 'crypto-js'; import { storeData, retrieveData, removeData, updateData } from '../caching'; +export const postConnection = async (url, payload, customBaseUrl = null) => { + try { + const baseUrl = + customBaseUrl || + (Platform.OS === 'web' + ? 'http://localhost:8000' + : process.env.EXPO_PUBLIC_API_URL); + console.log(`Sending request to ${baseUrl}/${url}`); + + const response = await fetch(`${baseUrl}/${url}`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(payload), + }); + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + + const data = await response.json(); + alert(data.message); + + return data; + } catch (error) { + console.error('Error details:', error); + throw error; + } +}; + export const handleLogin = async (username, password, navigation) => { if (username === '' || password === '') { alert('All fields have to be filled before logging in!'); } else { console.log('username: ', username); - console.log('password: ', password); + + // hash password + const hashedPassword = CryptoJS.SHA256(password).toString(); + console.log('Hashed password:', hashedPassword); + navigation.navigate('Map'); - try { - const baseUrl = - Platform.OS === 'web' - ? 'http://localhost:8000' - : process.env.EXPO_PUBLIC_API_URL; - console.log(`Sending request to ${baseUrl}/login`); - - const response = await fetch(`${baseUrl}/login`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ username, password }), - }); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } + const url = 'login'; + const payload = { username, password: hashedPassword }; - // Await response and print message from server - const data = await response.json(); + const data = await postConnection(url, payload); + alert(data.message); + if (data.message === `Login successful for user: ${username}`) { if (retrieveData('username') !== null) { await removeData('username'); await updateData('username', username); } else { await storeData('username', username); } - - // if the data is "success", then cache details - - console.log('Server response:', data); - } catch (error) { - console.error('Error details:', error); } + console.log('Server response:', data); } }; @@ -52,44 +68,28 @@ export const handleSignup = async (username, password, navigation) => { alert('All fields have to be filled before signing up!'); } else { console.log('username: ', username); - console.log('password: ', password); + + // hash password + const hashedPassword = CryptoJS.SHA256(password).toString(); + console.log('Hashed password:', hashedPassword); + navigation.navigate('Map'); - try { - const baseUrl = - Platform.OS === 'web' - ? 'http://localhost:8000' - : process.env.EXPO_PUBLIC_API_URL; - console.log(`Sending request to ${baseUrl}/signup`); - - const response = await fetch(`${baseUrl}/signup`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ username, password }), - }); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } + const url = 'signup'; + const payload = { username, password: hashedPassword }; - // Await response and print message from server - const data = await response.json(); - //alert(data.message); - - if (data.message === `Signup successful for user: ${username}`) { - if (retrieveData('username') !== null) { - await removeData('username'); - await updateData('username', username); - } else { - await storeData('username', username); - } - } + const data = await postConnection(url, payload); + alert(data.message); - console.log('Server response:', data); - } catch (error) { - console.error('Error details:', error); + if (data.message === `Signup successful for user: ${username}`) { + if (retrieveData('username') !== null) { + await removeData('username'); + await updateData('username', username); + } else { + await storeData('username', username); + } } + + console.log('Server response:', data); } };