diff --git a/frontend/src/helper/APIUtils.ts b/frontend/src/helper/APIUtils.ts index 1251d129..9f5bec06 100644 --- a/frontend/src/helper/APIUtils.ts +++ b/frontend/src/helper/APIUtils.ts @@ -8,3 +8,5 @@ export const RESEND_VERIFICATION = `${BASE_URL}/user/resend-verification-mail`; export const SEND_OTP = `${BASE_URL}/user/forgotpassword`; export const CHECK_OTP = `${BASE_URL}/user/verifyOtp`; export const CHANGE_PASSWORD_API = `${BASE_URL}/user/verifypassword`; + +export const REFRESH_TOKEN_API = `${BASE_URL}/user/refreshToken`; diff --git a/frontend/src/helper/Utils.ts b/frontend/src/helper/Utils.ts index c888d182..b4a71b36 100644 --- a/frontend/src/helper/Utils.ts +++ b/frontend/src/helper/Utils.ts @@ -516,6 +516,7 @@ Advanced Stage: Total dependence of the person on the caregiver for all personal export const KEYS = { USER_ID: 'USER_ID', USER_TOKEN: 'USER_TOKEN', + USER_TOKEN_EXPIRY_DATE:'USER_TOKEN_EXPIRY_DATE' }; export const createHTMLStructure = ( diff --git a/frontend/src/screens/HomeScreen.tsx b/frontend/src/screens/HomeScreen.tsx index 6d579329..d59143cc 100644 --- a/frontend/src/screens/HomeScreen.tsx +++ b/frontend/src/screens/HomeScreen.tsx @@ -14,12 +14,14 @@ import {PRIMARY_COLOR} from '../helper/Theme'; import AddIcon from '../components/AddIcon'; import ArticleCard from '../components/ArticleCard'; import HomeScreenHeader from '../components/HomeScreenHeader'; -import {articles, Categories} from '../helper/Utils'; +import {articles, Categories, KEYS, retrieveItem} from '../helper/Utils'; import {Article, Category, CategoryType, HomeScreenProps} from '../type'; import axios from 'axios'; import {ARTICLE_TAGS_API, BASE_URL} from '../helper/APIUtils'; import FilterModal from '../components/FilterModal'; import {BottomSheetModal} from '@gorhom/bottom-sheet'; +import { useQuery } from '@tanstack/react-query'; +import Loader from '../components/Loader'; const HomeScreen = ({navigation}: HomeScreenProps) => { const [articleCategories, setArticleCategories] = useState([]); @@ -104,6 +106,38 @@ const HomeScreen = ({navigation}: HomeScreenProps) => { }; // handle filter apply function const handleFilterApply = () => {}; + + + const { + data: articleData, + isLoading, + error, + } = useQuery({ + queryKey: ['get-all-articles'], + queryFn: async () => { + try { + const token = await retrieveItem(KEYS.USER_TOKEN); + if (token == null) { + Alert.alert('No token found'); + return; + } + const response = await axios.get(`${BASE_URL}/articles`, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + console.log('Article Res', response.data); + return response.data.articles; + } catch (err) { + console.error('Error fetching articles:', err); + } + }, + }); + + if (isLoading) { + return ; + } + return ( diff --git a/frontend/src/screens/SplashScreen.tsx b/frontend/src/screens/SplashScreen.tsx index c10ddb69..21bb20af 100644 --- a/frontend/src/screens/SplashScreen.tsx +++ b/frontend/src/screens/SplashScreen.tsx @@ -6,10 +6,33 @@ import {SplashScreenProp} from '../type'; import {KEYS, retrieveItem} from '../helper/Utils'; const SplashScreen = ({navigation}: SplashScreenProp) => { + + + function isDateMoreThanSevenDaysOld(dateString: string) { + const inputDate = new Date(dateString).getTime(); + const currentDate = new Date().getTime(); + const timeDifference = currentDate - inputDate; + const daysDifference = timeDifference / (1000 * 3600 * 24); + return daysDifference >= 7; + } + + + function isDateNotMoreThanTenMinutesOld(dateString:string) { + const inputDate = new Date(dateString); + const currentDate = new Date(); + const timeDifference = currentDate.getTime() - inputDate.getTime(); + const minutesDifference = timeDifference / (1000 * 60); + return minutesDifference <= 10; +} + + + const checkLoginStatus = async () => { try { const user = await retrieveItem(KEYS.USER_TOKEN); - if (user) { + const expiryDate = await retrieveItem(KEYS.USER_TOKEN_EXPIRY_DATE); + + if (user && expiryDate && !isDateMoreThanSevenDaysOld(expiryDate)) { navigation.navigate('TabNavigation'); } else { navigation.navigate('LoginScreen'); diff --git a/frontend/src/screens/article/ArticleScreen.tsx b/frontend/src/screens/article/ArticleScreen.tsx index 59e2def7..68068ac8 100644 --- a/frontend/src/screens/article/ArticleScreen.tsx +++ b/frontend/src/screens/article/ArticleScreen.tsx @@ -41,39 +41,6 @@ const ArticleScreen = ({route}: {route: ArticleScreenProp['route']}) => { } }; - const { - data: articleData, - isLoading, - error, - } = useQuery({ - queryKey: ['get-all-articles'], - queryFn: async () => { - try { - const token = await retrieveItem(KEYS.USER_TOKEN); - console.log('Auth token', token); - if (token == null) { - Alert.alert('No token found'); - return; - } - - const response = await axios.get(`${BASE_URL}/articles`, { - headers: { - Authorization: `Bearer ${token}`, - }, - }); - - console.log('Article Res', response.data); - return response.data.articles; - } catch (err) { - console.error('Error fetching articles:', err); - throw err; // Make sure to throw the error to handle it properly - } - }, - }); - - if (isLoading) { - return ; - } return ( { await storeItem(KEYS.USER_ID, auth.userId.toString()); if (auth.token) { await storeItem(KEYS.USER_TOKEN, auth.token.toString()); + await storeItem( + KEYS.USER_TOKEN_EXPIRY_DATE, + new Date().toISOString(), + ); } navigation.navigate('TabNavigation'); } catch (e) { - console.log('MMKV ERROR', e); + console.log('Async Storage ERROR', e); } },