From 8e7f50ce9ff710027762b0fa8d05335cac83d39f Mon Sep 17 00:00:00 2001 From: Nhat Tran Date: Sun, 25 Jan 2026 02:34:27 -0800 Subject: [PATCH] fix: Login sometimes fails to redirect to index, requiring reload Refactor authContext.tsx, moving session storage logic to a separate hook. --- backend/src/server.ts | 20 ++----- frontend/app/(tabs)/_layout.tsx | 26 ++++++++- frontend/app/Login.tsx | 51 ++++++------------ frontend/app/_layout.tsx | 39 +------------- frontend/components/Checking.tsx | 9 ++++ frontend/context/authContext.tsx | 73 ++++++------------------- frontend/hooks/useStorageState.ts | 90 +++++++++++++++++++++++++++++++ 7 files changed, 162 insertions(+), 146 deletions(-) create mode 100644 frontend/components/Checking.tsx create mode 100644 frontend/hooks/useStorageState.ts diff --git a/backend/src/server.ts b/backend/src/server.ts index 2e3bb3f..7215eff 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -27,22 +27,10 @@ app.get( // Google callback route app.get( "/auth/google/callback", - passport.authenticate("google", { failureRedirect: "http://localhost:8081/NotAuthorized" }), - (req, res, next) => { - if (!req.user) { - console.error("User is undefined during login."); - return res.status(401).json({ error: "User not authenticated" }); - } - req.login(req.user, (err) => { - if (err) { - console.error("Error during req.login:", err); - return next(err); // Pass the error to the error handler - } - console.log("User logged in:", req.user); // Debug log - console.log("Session data:", req.session); // Debug log - res.redirect("http://localhost:8081/"); // Redirect after successful login - }); - }, + passport.authenticate("google", { + successRedirect: "http://localhost:8081/", + failureRedirect: "http://localhost:8081/NotAuthorized", + }), ); app.get("/auth/me", (req, res) => { if (!req.isAuthenticated()) return res.status(401).json({ error: "Not Authenticated" }); diff --git a/frontend/app/(tabs)/_layout.tsx b/frontend/app/(tabs)/_layout.tsx index 9040e3b..9ec1af3 100644 --- a/frontend/app/(tabs)/_layout.tsx +++ b/frontend/app/(tabs)/_layout.tsx @@ -1,20 +1,42 @@ -import React from "react"; import { createBottomTabNavigator } from "@react-navigation/bottom-tabs"; import { Ionicons } from "@expo/vector-icons"; import Home from "."; import History from "./History"; // Import the History component +import { useEffect } from "react"; // import LoginPage from "../Login"; import Account from "./Account"; import Goals from "./Goals"; import { useAuth } from "@/context/authContext"; import { Redirect } from "expo-router"; +import Checking from "@/components/Checking"; +import { BACKEND_PORT } from "@env"; const Tab = createBottomTabNavigator(); export default function TabLayout() { - const { user } = useAuth(); + const { user, isLoading, login } = useAuth(); + + useEffect(() => { + fetch(`http://localhost:${BACKEND_PORT}/auth/me`, { + credentials: "include", + }) + .then((response) => { + if (response.ok) { + response.json().then(login); + } + }) + .catch((error) => { + console.error("Error checking auth:", error); + }); + }, []); + + if (isLoading) { + return ; + } + if (!user) { return ; } + return ( { - // const router = useRouter(); - const [isRedirected, setIsRedirected] = useState(false); - const { login } = useAuth(); - const handleGoogleLogin = () => { - // Redirect to backend Google OAuth URL - window.location.href = `http://localhost:${BACKEND_PORT}/auth/google`; - }; - useEffect(() => { - const checkAuth = async () => { - try { - const response = await fetch( - `http://localhost:${BACKEND_PORT}/auth/me`, - { - credentials: "include", - }, - ); + const router = useRouter(); + const { isLoading, user } = useAuth(); - if (response.ok) { - const userData = await response.json(); - await login(userData); - } - } catch (error) { - console.error("Error checking auth:", error); - } - }; + if (isLoading) { + return ; + } + + if (user) { + return ; + } - // Check if we've been redirected back from OAuth - // You can also check for specific query parameters from your OAuth callback - if (window.location.pathname === "/Login" && !isRedirected) { - setIsRedirected(true); - checkAuth(); - } - }, [isRedirected]); return ( Login Page Sign in to access your account. -