Commit 9f568dc5 by Nguyễn Thị Thúy

logout when session end

parent 113cb583
...@@ -50,6 +50,7 @@ const Login = (props) => { ...@@ -50,6 +50,7 @@ const Login = (props) => {
}; };
useEffect(() => { useEffect(() => {
props.hideLoading();
getAccount(); getAccount();
getTokenDevice(); getTokenDevice();
}, []); }, []);
......
import KEY from '../assets/AsynStorage'; import KEY from '../assets/AsynStorage';
import axios from 'axios'; import axios from 'axios';
import AsyncStorage from '@react-native-community/async-storage'; import AsyncStorage from '@react-native-community/async-storage';
import {showAlert, TYPE} from '../components/DropdownAlert/index';
import I18n from '../helper/i18/i18n';
import {DeviceEventEmitter} from 'react-native';
axios.defaults.timeout = 10000; axios.defaults.timeout = 10000;
export async function GetData(url, data) { export async function GetData(url, data) {
const token = await AsyncStorage.getItem(KEY.TOKEN); const token = await AsyncStorage.getItem(KEY.TOKEN);
let myRequest = { let myRequest = {
method: 'get', method: 'get',
url, url,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: 'Bearer ' + token, Authorization: 'Bearer ' + token,
}, },
params: { params: {
...data, ...data,
}, },
timeout: 30 * 1000, timeout: 30 * 1000,
// withCredentials: true, // withCredentials: true,
}; };
console.log('My request', myRequest); console.log('My request', myRequest);
return await axios(myRequest) return await axios(myRequest)
.then((response) => response) .then((response) => {
.then((response) => response) if (response.data?.code === 401) {
.catch((error) => { logout();
console.log(error.request); return;
const err = { } else {
message: 'error', return response;
status: error.request.status, }
}; })
return err; .then((response) => {
}); if (response.data?.code === 401) {
logout();
return;
} else {
return response;
}
})
.catch((error) => {
console.log(error.request);
const err = {
message: 'error',
status: error.request.status,
};
return err;
});
} }
export async function PostLogin(url, json) { export async function PostLogin(url, json) {
let myRequest = { let myRequest = {
method: 'post', method: 'post',
url, url,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
timeout: 30 * 1000, timeout: 30 * 1000,
data: JSON.stringify(json), data: JSON.stringify(json),
}; };
console.log('post data mobile', myRequest); console.log('post data mobile', myRequest);
return await axios(myRequest) return await axios(myRequest)
.then((response) => response) .then((response) => {
.then((response) => response) if (response.data?.code === 401) {
.catch((error) => { logout();
console.log(error); return;
const err = { } else {
message: 'error', return response;
status: error.request.status, }
}; })
return err; .then((response) => {
}); if (response.data?.code === 401) {
logout();
return;
} else {
return response;
}
})
.catch((error) => {
console.log(error);
const err = {
message: 'error',
status: error.request.status,
};
return err;
});
} }
export async function PostData(url, json, isAuth = true) { export async function PostData(url, json, isAuth = true) {
const token = await AsyncStorage.getItem(KEY.TOKEN); const token = await AsyncStorage.getItem(KEY.TOKEN);
console.log(token); console.log(token);
let myRequest = { let myRequest = {
method: 'post', method: 'post',
url, url,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: 'Bearer ' + token, Authorization: 'Bearer ' + token,
}, },
timeout: 30 * 1000, timeout: 30 * 1000,
data: JSON.stringify(json), data: JSON.stringify(json),
}; };
console.log('post data mobile', myRequest); console.log('post data mobile', myRequest);
return await axios(myRequest) return await axios(myRequest)
.then((response) => response) .then((response) => {
.then((response) => response) if (response.data?.code === 401) {
.catch((error) => { logout();
console.log(error.request); return;
const err = { } else {
message: 'error', return response;
status: error.request.status, }
}; })
return err; .then((response) => {
}); if (response.data?.code === 401) {
logout();
return;
} else {
return response;
}
})
.catch((error) => {
console.log(error.request);
const err = {
message: 'error',
status: error.request.status,
};
return err;
});
} }
export async function PostFormData(url, data) { export async function PostFormData(url, data) {
const token = await AsyncStorage.getItem(KEY.TOKEN); const token = await AsyncStorage.getItem(KEY.TOKEN);
const source = axios.CancelToken.source(); const source = axios.CancelToken.source();
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
source.cancel(); source.cancel();
// Timeout Logic // Timeout Logic
}, 60000); }, 60000);
console.log(token); console.log(token);
let myRequest = { let myRequest = {
method: 'post', method: 'post',
url, url,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'multipart/form-data', 'Content-Type': 'multipart/form-data',
Authorization: 'Bearer ' + token, Authorization: 'Bearer ' + token,
}, },
timeout: 60000, timeout: 60000,
data: data, data: data,
cancelToken: source.token, cancelToken: source.token,
}; };
console.log('post data mobile', myRequest.data); console.log('post data mobile', myRequest.data);
return await axios(myRequest) return await axios(myRequest)
.then((response) => { .then((response) => {
clearTimeout(timeout); clearTimeout(timeout);
return response; if (response.data?.code === 401) {
}) logout();
.then((response) => { return;
clearTimeout(timeout); } else {
return response; return response;
}) }
.catch((error) => { })
clearTimeout(timeout); .then((response) => {
console.log('errorTimeout', error); clearTimeout(timeout);
const err = { if (response.data?.code === 401) {
message: 'error', logout();
status: error.request.status, return;
}; } else {
return err; return response;
}); }
})
.catch((error) => {
clearTimeout(timeout);
console.log('errorTimeout', error);
const err = {
message: 'error',
status: error.request.status,
};
return err;
});
} }
/** /**
...@@ -135,27 +190,46 @@ export async function PostFormData(url, data) { ...@@ -135,27 +190,46 @@ export async function PostFormData(url, data) {
* @param {*} isAuth is state auth * @param {*} isAuth is state auth
*/ */
export async function PutData(url, json, isAuth = true) { export async function PutData(url, json, isAuth = true) {
const token = await AsyncStorage.getItem(KEY.TOKEN); const token = await AsyncStorage.getItem(KEY.TOKEN);
let myRequest = { let myRequest = {
method: 'put', method: 'put',
url, url,
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: 'Bearer ' + token, Authorization: 'Bearer ' + token,
}, },
data: JSON.stringify(json), data: JSON.stringify(json),
}; };
console.log('PutData', myRequest); console.log('PutData', myRequest);
return await axios(myRequest) return await axios(myRequest)
.then((response) => response) .then((response) => {
.then((response) => response) if (response.data?.code === 401) {
.catch((error) => { logout();
console.log(error.request); return;
const err = { } else {
message: 'error', return response;
status: error.request.status, }
}; })
return err; .then((response) => {
}); if (response.data?.code === 401) {
logout();
return;
} else {
return response;
}
})
.catch((error) => {
console.log(error.request);
const err = {
message: 'error',
status: error.request.status,
};
return err;
});
}
export function logout() {
showAlert(TYPE.ERROR, I18n.t('Notification'), I18n.t('LoginSessionEnd'));
DeviceEventEmitter.emit('logout');
} }
...@@ -289,4 +289,5 @@ export default { ...@@ -289,4 +289,5 @@ export default {
WarnMinReqestWithdraw: WarnMinReqestWithdraw:
'The withdrawal amount must be more than 1 million dong', 'The withdrawal amount must be more than 1 million dong',
Overtime: 'Overtime OTP', Overtime: 'Overtime OTP',
LoginSessionEnd: 'Login session end, please login again',
}; };
...@@ -289,4 +289,5 @@ export default { ...@@ -289,4 +289,5 @@ export default {
FirstEscrow: 'Ký quỹ ban đầu (VNĐ) ', FirstEscrow: 'Ký quỹ ban đầu (VNĐ) ',
WarnMinReqestWithdraw: 'Số tiền rút phải lớn hơn 1 triệu đồng', WarnMinReqestWithdraw: 'Số tiền rút phải lớn hơn 1 triệu đồng',
Overtime: 'Mã OTP hết hạn sử dụng', Overtime: 'Mã OTP hết hạn sử dụng',
LoginSessionEnd: "Hết phiên đăng nhập, vui lòng đăng nhập lại",
}; };
import React, {useEffect} from 'react'; import React, {useEffect} from 'react';
import {View, Text, Linking, Image} from 'react-native'; import {View, Text, Linking, Image, DeviceEventEmitter} from 'react-native';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs'; import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import Icon from 'react-native-vector-icons/FontAwesome5'; import Icon from 'react-native-vector-icons/FontAwesome5';
import Ionicons from 'react-native-vector-icons/Ionicons'; import Ionicons from 'react-native-vector-icons/Ionicons';
...@@ -23,152 +23,165 @@ import {changeLanguage} from '../actions/language'; ...@@ -23,152 +23,165 @@ import {changeLanguage} from '../actions/language';
import KEY from '../assets/AsynStorage'; import KEY from '../assets/AsynStorage';
import AsyncStorage from '@react-native-community/async-storage'; import AsyncStorage from '@react-native-community/async-storage';
import {DETAILNEW} from '../routers/ScreenNames'; import {DETAILNEW} from '../routers/ScreenNames';
import {getHeight, getWidth} from '../Config/Functions'; import {getHeight, getWidth, logout} from '../Config/Functions';
const Tab = createBottomTabNavigator(); const Tab = createBottomTabNavigator();
const PayScreenComponent = () => { const PayScreenComponent = () => {
return null; return null;
}; };
const TabNavigator = (props) => { const TabNavigator = (props) => {
console.log(getHeight(), getWidth()); console.log(getHeight(), getWidth());
const navigate = useNavigation(); const navigate = useNavigation();
useEffect(() => { useEffect(() => {
setInitLanguage(); setInitLanguage();
checkDeepking(); checkDeepking();
setTimeout(() => { setTimeout(() => {
showPopUp(); showPopUp();
}, 3000); }, 3000);
}, []); let logoutListener = DeviceEventEmitter.addListener('logout', (e) => {
console.log('run in to logout');
const checkDeepking = () => { logout(navigate);
Linking.getInitialURL().then((url) => { });
if (url) deeplinkScreen(url); return () => {
}); logoutListener.remove();
Linking.addEventListener('url', (url) => { };
if (url) handleOpenURL(url); }, []);
});
}; const checkDeepking = () => {
Linking.getInitialURL().then((url) => {
const handleOpenURL = (event) => { if (url) {
deeplinkScreen(event.url); deeplinkScreen(url);
}; }
});
const deeplinkScreen = (url) => { Linking.addEventListener('url', (url) => {
const route = url.replace(/.*?:\/\//g, ''); if (url) {
const id = route.match(/\/([^\/]+)\/?$/)[1]; handleOpenURL(url);
const root = route.split('/')[0]; }
// const routeName = route.split('/')[1]; });
if (root == 'screen') { };
navigate.navigate(DETAILNEW, {id});
} const handleOpenURL = (event) => {
}; deeplinkScreen(event.url);
};
useEffect(() => {
checkScreenInit(); const deeplinkScreen = (url) => {
}, [props.screenInit]); const route = url.replace(/.*?:\/\//g, '');
const id = route.match(/\/([^\/]+)\/?$/)[1];
const checkScreenInit = () => { const root = route.split('/')[0];
if (props.screenInit.screen) { // const routeName = route.split('/')[1];
const {screen, id} = props.screenInit; if (root == 'screen') {
navigate.navigate(screen, {id: id}); navigate.navigate(DETAILNEW, {id});
} }
}; };
const setInitLanguage = async () => { useEffect(() => {
const laguage = await AsyncStorage.getItem(KEY.LANGUAGE); checkScreenInit();
if (laguage) props.changeLanguage(laguage); }, [props.screenInit]);
setLocation(I18n, laguage);
}; const checkScreenInit = () => {
if (props.screenInit.screen) {
const showPopUp = () => { const {screen, id} = props.screenInit;
if (props.user.status == 1) { navigate.navigate(screen, {id: id});
return confirmAlert(I18n.t('PopupVerifyAccount'), () => { }
navigate.navigate(AccountVerification); };
});
} else if (props.user.status == 3 && !props.user.contract_code) { const setInitLanguage = async () => {
return confirmAlert(I18n.t('PopupOpenCQG'), () => { const laguage = await AsyncStorage.getItem(KEY.LANGUAGE);
navigate.navigate(PACKETCQG); if (laguage) {
}); props.changeLanguage(laguage);
} }
}; setLocation(I18n, laguage);
return ( };
<View style={{flex: 1}}>
<Tab.Navigator const showPopUp = () => {
initialRouteName="Screen1" if (props.user.status == 1) {
tabBarOptions={{activeTintColor: R.colors.main}}> return confirmAlert(I18n.t('PopupVerifyAccount'), () => {
<Tab.Screen navigate.navigate(AccountVerification);
name="Screen1" });
component={Home} } else if (props.user.status == 3 && !props.user.contract_code) {
options={{ return confirmAlert(I18n.t('PopupOpenCQG'), () => {
tabBarLabel: 'Overview', navigate.navigate(PACKETCQG);
tabBarIcon: ({color, size}) => ( });
<Icon name="landmark" size={size} color={color} /> }
), };
}} return (
/> <View style={{flex: 1}}>
<Tab.Navigator
<Tab.Screen initialRouteName="Screen1"
name="Screen2" tabBarOptions={{activeTintColor: R.colors.main}}>
component={Transaction} <Tab.Screen
options={{ name="Screen1"
tabBarLabel: component={Home}
props.language.language == 'vi' ? 'Giao dịch' : 'Tradding', options={{
tabBarIcon: ({color, size}) => ( tabBarLabel: 'Overview',
<Image tabBarIcon: ({color, size}) => (
source={R.images.iconTransaction2} <Icon name="landmark" size={size} color={color}/>
style={{width: size, height: size, tintColor: color}} ),
/> }}
), />
}}
/> <Tab.Screen
name="Screen2"
<Tab.Screen component={Transaction}
name="Pay" options={{
component={PayScreenComponent} tabBarLabel:
options={{ props.language.language == 'vi' ? 'Giao dịch' : 'Tradding',
tabBarButton: () => <PlussModal />, tabBarIcon: ({color, size}) => (
}} <Image
/> source={R.images.iconTransaction2}
style={{width: size, height: size, tintColor: color}}
<Tab.Screen />
name="Screen3" ),
component={Notification} }}
options={{ />
tabBarLabel:
props.language.language == 'vi' ? 'Thông báo' : 'Notification', <Tab.Screen
tabBarIcon: ({color, size}) => ( name="Pay"
<Ionicons component={PayScreenComponent}
name="ios-notifications-outline" options={{
size={size} tabBarButton: () => <PlussModal/>,
color={color} }}
/> />
),
}} <Tab.Screen
/> name="Screen3"
<Tab.Screen component={Notification}
name="Screen4" options={{
component={NewFeed} tabBarLabel:
options={{ props.language.language == 'vi' ? 'Thông báo' : 'Notification',
tabBarLabel: props.language.language == 'vi' ? 'Tin tức' : 'News', tabBarIcon: ({color, size}) => (
tabBarIcon: ({color, size}) => ( <Ionicons
<Ionicons name="newspaper-outline" size={size} color={color} /> name="ios-notifications-outline"
), size={size}
}} color={color}
/> />
</Tab.Navigator> ),
</View> }}
); />
<Tab.Screen
name="Screen4"
component={NewFeed}
options={{
tabBarLabel: props.language.language == 'vi' ? 'Tin tức' : 'News',
tabBarIcon: ({color, size}) => (
<Ionicons name="newspaper-outline" size={size} color={color}/>
),
}}
/>
</Tab.Navigator>
</View>
);
}; };
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
return { return {
user: state.userReducer, user: state.userReducer,
notifi: state.CountNotificationReducer, notifi: state.CountNotificationReducer,
screenInit: state.ScreenInitReducer, screenInit: state.ScreenInitReducer,
language: state.languageReducer, language: state.languageReducer,
}; };
}; };
export default connect(mapStateToProps, {changeLanguage})(TabNavigator); export default connect(mapStateToProps, {changeLanguage})(TabNavigator);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment