-
Notifications
You must be signed in to change notification settings - Fork 5
fix: Login sometimes fails to redirect to index, requiring reload #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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", | ||||||
| }) | ||||||
nhatt-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| .then((response) => { | ||||||
| if (response.ok) { | ||||||
| response.json().then(login); | ||||||
| } | ||||||
| }) | ||||||
| .catch((error) => { | ||||||
| console.error("Error checking auth:", error); | ||||||
| }); | ||||||
| }, []); | ||||||
|
Comment on lines
+18
to
+30
|
||||||
| }, []); | |
| }, [login]); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,32 @@ | ||
| // src/LoginPage.tsx | ||
nhatt-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { useAuth } from "@/context/authContext"; | ||
| import React, { useEffect, useState } from "react"; | ||
| import { View, Text, Button, StyleSheet } from "react-native"; | ||
| import { BACKEND_PORT } from "@env"; | ||
| // import { useRouter } from "expo-router"; | ||
| import { Redirect, useRouter } from "expo-router"; | ||
| import Checking from "@/components/Checking"; | ||
|
|
||
| const LoginPage = () => { | ||
| // 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 <Checking />; | ||
| } | ||
|
|
||
| if (user) { | ||
| return <Redirect href="/" />; | ||
| } | ||
|
|
||
| // 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 ( | ||
| <View style={styles.container}> | ||
| <Text style={styles.title}>Login Page</Text> | ||
| <Text style={styles.message}>Sign in to access your account.</Text> | ||
| <Button title="Sign in with Google" onPress={handleGoogleLogin} /> | ||
| <Button | ||
| title="Sign in with Google" | ||
| onPress={() => { | ||
| router.replace(`http://localhost:${BACKEND_PORT}/auth/google`); | ||
| }} | ||
|
Comment on lines
+26
to
+28
|
||
| /> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { ActivityIndicator, View } from "react-native"; | ||
|
|
||
| export default function Checking() { | ||
| return ( | ||
| <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}> | ||
| <ActivityIndicator size="large" color="#0000ff" /> | ||
| </View> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,24 +1,17 @@ | ||||||||||||||||
| import { | ||||||||||||||||
| createContext, | ||||||||||||||||
| useContext, | ||||||||||||||||
| useState, | ||||||||||||||||
| useEffect, | ||||||||||||||||
| ReactNode, | ||||||||||||||||
| } from "react"; | ||||||||||||||||
| import AsyncStorage from "@react-native-async-storage/async-storage"; | ||||||||||||||||
| import { createContext, useContext, ReactNode } from "react"; | ||||||||||||||||
| import { useRouter } from "expo-router"; | ||||||||||||||||
| import { BACKEND_PORT } from "@env"; | ||||||||||||||||
| import useStorageState, { AuthUserSession } from "@/hooks/useStorageState"; | ||||||||||||||||
|
|
||||||||||||||||
| interface AuthContextType { | ||||||||||||||||
| user: any | null; | ||||||||||||||||
| userId: any | null; | ||||||||||||||||
| interface AuthContextType extends AuthUserSession { | ||||||||||||||||
| isLoading: boolean; | ||||||||||||||||
| login: (userData: any) => Promise<void>; | ||||||||||||||||
| logout: () => Promise<void>; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| const AuthContext = createContext<AuthContextType>({ | ||||||||||||||||
| user: null, | ||||||||||||||||
| userId: null, | ||||||||||||||||
| isLoading: false, | ||||||||||||||||
| login: async () => {}, | ||||||||||||||||
| logout: async () => {}, | ||||||||||||||||
| }); | ||||||||||||||||
|
|
@@ -28,61 +21,29 @@ interface AuthProviderProps { | |||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => { | ||||||||||||||||
| const [user, setUser] = useState<any | null>(null); | ||||||||||||||||
| const [userId, setUserId] = useState<any | null>(null); | ||||||||||||||||
| const [[isLoading, session], setSession] = useStorageState(); | ||||||||||||||||
| const router = useRouter(); | ||||||||||||||||
| const [loading, setLoading] = useState(true); | ||||||||||||||||
|
|
||||||||||||||||
| useEffect(() => { | ||||||||||||||||
| const loadUser = async () => { | ||||||||||||||||
| const storedUser = await AsyncStorage.getItem("user"); | ||||||||||||||||
| if (storedUser) { | ||||||||||||||||
| try { | ||||||||||||||||
| const userData = JSON.parse(storedUser); | ||||||||||||||||
|
|
||||||||||||||||
| // Validate session with backend | ||||||||||||||||
| const res = await fetch(`http://localhost:${BACKEND_PORT}/auth/me`, { | ||||||||||||||||
| credentials: "include", | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| if (!res.ok) throw new Error("Session invalid"); | ||||||||||||||||
| setUser(userData); | ||||||||||||||||
| setUserId(userData.id); | ||||||||||||||||
| } catch (err) { | ||||||||||||||||
| await AsyncStorage.clear(); | ||||||||||||||||
| setUser(null); | ||||||||||||||||
| setUserId(null); | ||||||||||||||||
| router.replace("/Login"); | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| setLoading(false); | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| loadUser(); | ||||||||||||||||
| }, []); | ||||||||||||||||
|
|
||||||||||||||||
| const login = async (userData: any) => { | ||||||||||||||||
| await Promise.all([ | ||||||||||||||||
| AsyncStorage.setItem("user", JSON.stringify(userData)), | ||||||||||||||||
| AsyncStorage.setItem("userId", userData.id), | ||||||||||||||||
| ]); | ||||||||||||||||
| setUser(userData); | ||||||||||||||||
| setUserId(userData.id); | ||||||||||||||||
| setSession({ user: userData, userId: userData.id }); | ||||||||||||||||
|
||||||||||||||||
| setSession({ user: userData, userId: userData.id }); | |
| const userId = | |
| userData && typeof userData === "object" && "id" in userData | |
| ? (userData as { id: string | number }).id | |
| : null; | |
| setSession({ user: userData, userId }); |
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using any type for the userData parameter defeats TypeScript's type safety. Consider defining a proper User interface that describes the expected structure of the user data being passed from the OAuth callback.
Copilot
AI
Jan 26, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logout function sets the session to { user: null, userId: null } which is inconsistent with how setStorageItem handles null values. Looking at setStorageItem, passing null removes the items from storage, but passing an object with null properties stores those null values. Consider passing null directly to setSession instead of an object with null properties for consistency with the intended logout behavior.
| setSession({ user: null, userId: null }); | |
| setSession(null); |
Uh oh!
There was an error while loading. Please reload this page.