Skip to content

Commit

Permalink
--redirect-login-on-session-expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
SB2318 committed Sep 14, 2024
1 parent b848360 commit 52ca0ad
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
2 changes: 2 additions & 0 deletions frontend/src/helper/APIUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`;
1 change: 1 addition & 0 deletions frontend/src/helper/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
36 changes: 35 additions & 1 deletion frontend/src/screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category[]>([]);
Expand Down Expand Up @@ -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 <Loader />;
}

return (
<SafeAreaView style={styles.container}>
<HomeScreenHeader handlePresentModalPress={handlePresentModalPress} />
Expand Down
25 changes: 24 additions & 1 deletion frontend/src/screens/SplashScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
33 changes: 0 additions & 33 deletions frontend/src/screens/article/ArticleScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Loader />;
}
return (
<View style={styles.container}>
<ScrollView
Expand Down
9 changes: 6 additions & 3 deletions frontend/src/screens/auth/LoginScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import {
BackHandler,
Alert,
useColorScheme,
Modal,
} from 'react-native';
import React, {useEffect, useState} from 'react';
import {PRIMARY_COLOR} from '../../helper/Theme';
import {useSafeAreaInsets} from 'react-native-safe-area-context';
import {fp, hp, wp} from '../../helper/Metric';
import {Colors} from 'react-native/Libraries/NewAppScreen';
// import {useNavigation} from '@react-navigation/native';
import {Categories, KEYS, storeItem} from '../../helper/Utils';
import {KEYS, storeItem} from '../../helper/Utils';
import EmailInputModal from '../../components/EmailInputModal';
import Icon from 'react-native-vector-icons/Ionicons';
import {AuthData, LoginScreenProp, User} from '../../type';
Expand Down Expand Up @@ -128,10 +127,14 @@ const LoginScreen = ({navigation}: LoginScreenProp) => {
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);
}
},

Expand Down

0 comments on commit 52ca0ad

Please sign in to comment.