loginPage.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import {
  2. View,
  3. Text,
  4. Image,
  5. StyleSheet,
  6. Pressable,
  7. Alert,
  8. Dimensions,
  9. ActivityIndicator,
  10. ScrollView,
  11. Switch
  12. } from 'react-native';
  13. import { Ionicons } from '@expo/vector-icons';
  14. import Logo from '../../../global/logo';
  15. import NormalButton from '../../../global/normal_button';
  16. import NormalInput from '../../../global/normal_input';
  17. import { useEffect, useState } from 'react';
  18. import { useAuth } from '../../../../context/AuthProvider';
  19. import AsyncStorage from '@react-native-async-storage/async-storage';
  20. import Checkbox from 'expo-checkbox';
  21. import { router } from 'expo-router';
  22. import PhoneInput from '../../../global/phone_input';
  23. import LanguageSwitcher from './languageSwitcher';
  24. import { useTranslation } from '../../../../util/hooks/useTranslation';
  25. type LoginPageProps = {
  26. goToNextPage: () => void;
  27. goToForgetPassWordPage: () => void;
  28. goToBindingPhoneNumberPage: () => void;
  29. };
  30. const screenHeight = Dimensions.get('window').height;
  31. const LoginPage: React.FC<LoginPageProps> = ({ goToNextPage, goToForgetPassWordPage, goToBindingPhoneNumberPage }) => {
  32. const { t, ready, currentLanguage } = useTranslation();
  33. // const [loginEmail, setLoginEmail] = useState('');
  34. const [loginPhone, setLoginPhone] = useState('');
  35. const [loginPassword, setLoginPassword] = useState('');
  36. const [saveAccount, setSaveAccount] = useState(false);
  37. const [userTerms, setUserTerms] = useState(false);
  38. const [isLoading, setIsLoading] = useState(false);
  39. const { login } = useAuth();
  40. const [isChecked, setChecked] = useState(false);
  41. const [showPassword, setShowPassword] = useState(false);
  42. useEffect(() => {
  43. loadSavedCredentials();
  44. }, []);
  45. const loadSavedCredentials = async () => {
  46. try {
  47. const savedPhone = await AsyncStorage.getItem('savedPhone');
  48. // const savedEmail = await AsyncStorage.getItem('savedEmail');
  49. const savedPassword = await AsyncStorage.getItem('savedPassword');
  50. if (savedPhone && savedPassword) {
  51. // setLoginEmail(savedEmail);
  52. setLoginPhone(savedPhone);
  53. setLoginPassword(savedPassword);
  54. setSaveAccount(true);
  55. setUserTerms(true);
  56. setChecked(true);
  57. }
  58. } catch (error) {
  59. console.error('Error loading saved credentials:', error);
  60. }
  61. };
  62. const _login = async (username: string, password: string) => {
  63. setIsLoading(true);
  64. if (username === '' || password === '') {
  65. Alert.alert(t('login.alert.pleaseEnter'));
  66. } else if (username.includes('@')) {
  67. Alert.alert(t('login.alert.binding'), t('login.alert.binding1'), [
  68. { text: t('login.alert.binding2'), onPress: () => goToBindingPhoneNumberPage() },
  69. { text: t('login.alert.binding3'), onPress: () => router.replace('/login') }
  70. ]);
  71. } else {
  72. if (userTerms) {
  73. const isBinding = false;
  74. const response = await login(username, password, isBinding);
  75. if (response) {
  76. if (saveAccount) {
  77. await AsyncStorage.setItem('savedPhone', username);
  78. await AsyncStorage.setItem('savedPassword', password);
  79. } else {
  80. await AsyncStorage.removeItem('savedPhone');
  81. await AsyncStorage.removeItem('savedPassword');
  82. }
  83. } else {
  84. Alert.alert(t('login.alert.error'), `${t('login.alert.reason')}`);
  85. }
  86. } else {
  87. Alert.alert(t('login.alert.agree'));
  88. }
  89. }
  90. setIsLoading(false);
  91. };
  92. const goToUserTermsPage = () => {
  93. router.push('(public)/userTermsPage');
  94. };
  95. return (
  96. <ScrollView
  97. contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }}
  98. keyboardShouldPersistTaps="handled"
  99. className={`flex-1 ${screenHeight < 750 ? '' : 'space-y-8'}`}
  100. >
  101. <View className='ml-5 mt-2'>
  102. <LanguageSwitcher></LanguageSwitcher>
  103. </View>
  104. <View className="flex-1 py-3">
  105. <View className="items-center">
  106. {/* <View className="flex-3 items-center justify-end" style={{}}> */}
  107. <Image
  108. source={require('../../../../assets/ccLogo.png')}
  109. resizeMode="contain"
  110. style={{
  111. width: screenHeight > 750 ? 250 : 180,
  112. height: screenHeight > 750 ? 250 : 180
  113. }}
  114. />
  115. </View>
  116. <View
  117. style={{
  118. gap: 8
  119. // paddingBottom: Math.max(insets.bottom, 20)
  120. // marginTop: screenHeight > 750 ? 40 : 0
  121. }}
  122. className="mx-[5%] mt-3"
  123. >
  124. <Text className="text-lg text-center text-[#888888] font-[500]">
  125. {t('login.tip1')}<Text className="font-bold"> {t('login.tip2')} </Text>{t('login.tip3')}
  126. </Text>
  127. <Pressable onPress={goToBindingPhoneNumberPage}>
  128. <Text className="text-xl text-center text-[#02677D] font-[800] underline">
  129. {t('login.binding')}
  130. </Text>
  131. </Pressable>
  132. <PhoneInput
  133. value={loginPhone}
  134. onChangeText={(phone) => setLoginPhone(phone)}
  135. placeholder={t('login.phone')}
  136. extendedStyle={{ borderRadius: 12, padding: 20 }}
  137. textContentType="telephoneNumber"
  138. autoComplete="tel"
  139. keyboardType="phone-pad"
  140. autoCapitalize="none"
  141. />
  142. <View className="relative">
  143. <NormalInput
  144. value={loginPassword}
  145. placeholder={t('login.password')}
  146. onChangeText={(password) => setLoginPassword(password)}
  147. secureTextEntry={!showPassword}
  148. extendedStyle={{ borderRadius: 12, padding: 20, paddingRight: 50 }}
  149. textContentType="password"
  150. autoComplete="password"
  151. />
  152. <Pressable
  153. className="absolute right-4 top-0 bottom-0 flex justify-center"
  154. onPress={() => setShowPassword(!showPassword)}
  155. >
  156. <Ionicons
  157. name={showPassword ? 'eye-outline' : 'eye-off-outline'}
  158. size={24}
  159. color="#02677D"
  160. />
  161. </Pressable>
  162. </View>
  163. <View className="flex flex-row items-center ">
  164. <Checkbox
  165. style={styles.checkbox}
  166. value={saveAccount}
  167. color={saveAccount ? '#02677D' : '#02677D'}
  168. onValueChange={(newValue) => {
  169. setSaveAccount(newValue);
  170. }}
  171. />
  172. <Text style={styles.text}>{t('login.rememberPhone')}</Text>
  173. </View>
  174. <View className="flex flex-row items-center">
  175. <Checkbox
  176. style={styles.checkbox}
  177. value={userTerms}
  178. color={userTerms ? '#02677D' : '#02677D'}
  179. onValueChange={(newValue) => {
  180. setUserTerms(newValue);
  181. }}
  182. />
  183. <Text style={styles.text}>{t('login.termsofService')}
  184. <Text style={styles.userTerms} onPress={goToUserTermsPage}>{t('login.termsofService1')}</Text>
  185. </Text>
  186. </View>
  187. <NormalButton
  188. extendedStyle={{ padding: 20 }}
  189. onPress={() => _login(loginPhone, loginPassword)}
  190. title={
  191. isLoading ? (
  192. <Text
  193. style={{
  194. fontWeight: '700',
  195. fontSize: 20,
  196. color: '#fff'
  197. }}
  198. >
  199. <ActivityIndicator />
  200. </Text>
  201. ) : (
  202. <Text
  203. style={{
  204. fontWeight: '700',
  205. fontSize: 20,
  206. color: '#fff'
  207. }}
  208. >
  209. {t('login.signIn')}
  210. </Text>
  211. )
  212. }
  213. />
  214. <View className="flex flex-row justify-between relative">
  215. <Pressable className="self-start" onPress={goToNextPage}>
  216. <Text style={styles.text}>{t('login.register')}</Text>
  217. </Pressable>
  218. <Pressable className="self-start" onPress={() => goToForgetPassWordPage()}>
  219. <Text style={styles.text}>{t('login.forgotPassword')}</Text>
  220. </Pressable>
  221. </View>
  222. </View>
  223. <View className="pb-6" />
  224. </View>
  225. </ScrollView>
  226. );
  227. };
  228. const styles = StyleSheet.create({
  229. container: {
  230. flex: 1,
  231. height: Dimensions.get('window').height,
  232. justifyContent: 'center',
  233. marginHorizontal: 20
  234. },
  235. topContainerForLogo: {},
  236. checkbox: {
  237. marginLeft: 8,
  238. marginRight: 8
  239. },
  240. text: {
  241. color: '#02677D',
  242. fontSize: 16,
  243. paddingVertical: 5
  244. },
  245. userTerms: {
  246. color: '#02677D',
  247. fontSize: 16,
  248. paddingVertical: 5,
  249. textDecorationLine: 'underline'
  250. }
  251. });
  252. export default LoginPage;