Skip to content

Commit

Permalink
persistent login session works for 1. closed app && 2. restart phone …
Browse files Browse the repository at this point in the history
…- phone restart causes font issues, look into it
  • Loading branch information
cfiguer055 committed Oct 18, 2024
1 parent 4252770 commit bf89167
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
62 changes: 46 additions & 16 deletions SmartClothingApp/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { onAuthStateChanged } from 'firebase/auth';
import {
startLoadUserData,
updateUserMetricsData,
restoreUUID,
} from "./src/actions/userActions.js";
import SplashScreen from "react-native-splash-screen";

Expand All @@ -37,15 +38,35 @@ export default function App() {

// Listen for changes in authentication state
const unsubscribe = onAuthStateChanged(auth, (user) => {
if (user) {
console.log("User is logged in:", user.uid);
setIsLoggedIn(true);
// Dispatch Redux actions or fetch data if necessary
store.dispatch(startLoadUserData()); // Load user data if logged in
} else {
console.log("No user is logged in.");
setIsLoggedIn(false);
}
setLoading(true); // Start loading while checking auth state
const handleAuthChange = async () => {
if (user) {
console.log("User is logged in:", user.uid);
setIsLoggedIn(true);

const storedUID = await checkUID()

// Dispatch Redux actions or fetch data if necessary
if (storedUID) {
// Restore UUID on app launch
store.dispatch(restoreUUID(storedUID));
console.log("User UUID restored - MainTab render")
} else {
console.log("User UUID not restored - MainTab won't render")
}

// Load user data if logged in
// store.dispatch(startLoadUserData()); // Done in checkMetrics
checkMetrics();
} else {
console.log("No user is logged in.");
setIsLoggedIn(false);
checkUID();
checkMetrics();
}
setLoading(false); // End loading when auth check completes
};
handleAuthChange();
});

// Check if there's a stored token on app launch
Expand All @@ -56,41 +77,50 @@ export default function App() {
if (storedUID) {
// If there's a token, try to refresh the user's session
console.log("Stored storedUID found");
return storedUID;

} else {
console.log("No stored UID");
return null;
}
} catch (error) {
console.error("Error checking stored UID:", error);
}
};
checkUID();
// checkUID();

// Check if there's a stored user Matrics Data in local storage
const checkMetrics = async () => {
try {
const storedMetrics = await getMetrics();
// console.log("Checking stored metrics");
console.log("Checking stored metrics");

if (storedMetrics) {
// If there's a token, try to refresh the user's session
// console.log("Stored metrics found");
// console.log("Stored metrics is -->", storedMetrics);
console.log("Stored metrics found");
console.log("Stored metrics is -->", storedMetrics);
//Set the user metrics data in the Redux store
store.dispatch(updateUserMetricsData(storedMetrics));
} else {
// console.log("No stored metrics");
console.log("No stored metrics, loading from Firestore");
//get the user metrics data from the database
store.dispatch(startLoadUserData());
}
} catch (error) {
console.error("Error checking stored metrics:", error);
}
};
checkMetrics();
//checkMetrics();

// Loading fonts
const loadFont = async () => {
const res = await useAppFonts();
setLoading(false);

//setLoading(false);
// Set a 0.5 second delay before setting loading to false
setTimeout(() => {
setLoading(false);
}, 500);
};
loadFont();

Expand Down
25 changes: 25 additions & 0 deletions SmartClothingApp/src/actions/userActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,31 @@ export const startLoginWithEmail = (email, password) => {
};
};

// Function to restore UUID from AsyncStorage
export const restoreUUID = (storedUID) => {
return async (dispatch) => {
try {
//const storedUID = await getUID();
if (storedUID) {
// Dispatch the login action to update the UUID in Redux store
dispatch(
loginWithEmail({
uuid: storedUID,
firstName: null, // If needed, fetch other user info from Firestore or AsyncStorage
lastName: null,
email: null,
})
);
console.log("UUID restored successfully:", storedUID);
} else {
console.log("No UUID found in AsyncStorage.");
}
} catch (error) {
console.error("Error restoring UUID:", error);
}
};
};

export const fetchUserData = async (database, uid) => {
try {
const userDocRef = doc(database, "Users", uid);
Expand Down
1 change: 1 addition & 0 deletions SmartClothingApp/src/components/DataCollectModal/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const DataCollectModal = (props) => {
// if(currentUserMetricsData.dob.seconds !== undefined && currentUserMetricsData.dob.nanoseconds !== undefined) {
// dobDate = dobDate.seconds * 1000;
// }

// Check if dob exists and has 'seconds' and 'nanoseconds' properties
if (dobDate && dobDate.seconds !== undefined && dobDate.nanoseconds !== undefined) {
dobDate = dobDate.seconds * 1000; // Convert Firestore Timestamp to milliseconds
Expand Down

0 comments on commit bf89167

Please sign in to comment.