loginPage.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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')}: ${response}`);
  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. <LanguageSwitcher></LanguageSwitcher>
  102. <View className="flex-1 py-4">
  103. <View className="items-center">
  104. {/* <View className="flex-3 items-center justify-end" style={{}}> */}
  105. <Image
  106. source={require('../../../../assets/ccLogo.png')}
  107. resizeMode="contain"
  108. style={{
  109. width: screenHeight > 750 ? 250 : 180,
  110. height: screenHeight > 750 ? 250 : 180
  111. }}
  112. />
  113. </View>
  114. <View
  115. style={{
  116. gap: 10
  117. // paddingBottom: Math.max(insets.bottom, 20)
  118. // marginTop: screenHeight > 750 ? 40 : 0
  119. }}
  120. className="mx-[5%] mt-4"
  121. >
  122. <Text className="text-lg text-center text-[#888888] font-[500]">
  123. {t('login.tip1')}<Text className="font-bold"> {t('login.tip2')} </Text>{t('login.tip3')}
  124. </Text>
  125. <Pressable onPress={goToBindingPhoneNumberPage}>
  126. <Text className="text-xl text-center text-[#02677D] font-[800] underline">
  127. {t('login.binding')}
  128. </Text>
  129. </Pressable>
  130. <PhoneInput
  131. value={loginPhone}
  132. onChangeText={(phone) => setLoginPhone(phone)}
  133. placeholder={t('login.phone')}
  134. extendedStyle={{ borderRadius: 12, padding: 20 }}
  135. textContentType="telephoneNumber"
  136. autoComplete="tel"
  137. keyboardType="phone-pad"
  138. autoCapitalize="none"
  139. />
  140. <View className="relative">
  141. <NormalInput
  142. value={loginPassword}
  143. placeholder={t('login.password')}
  144. onChangeText={(password) => setLoginPassword(password)}
  145. secureTextEntry={!showPassword}
  146. extendedStyle={{ borderRadius: 12, padding: 20, paddingRight: 50 }}
  147. textContentType="password"
  148. autoComplete="password"
  149. />
  150. <Pressable
  151. className="absolute right-4 top-0 bottom-0 flex justify-center"
  152. onPress={() => setShowPassword(!showPassword)}
  153. >
  154. <Ionicons
  155. name={showPassword ? 'eye-outline' : 'eye-off-outline'}
  156. size={24}
  157. color="#02677D"
  158. />
  159. </Pressable>
  160. </View>
  161. <View className="flex flex-row items-center ">
  162. <Checkbox
  163. style={styles.checkbox}
  164. value={saveAccount}
  165. color={saveAccount ? '#02677D' : '#02677D'}
  166. onValueChange={(newValue) => {
  167. setSaveAccount(newValue);
  168. }}
  169. />
  170. <Text style={styles.text}>{t('login.rememberPhone')}</Text>
  171. </View>
  172. <View className="flex flex-row items-center">
  173. <Checkbox
  174. style={styles.checkbox}
  175. value={userTerms}
  176. color={userTerms ? '#02677D' : '#02677D'}
  177. onValueChange={(newValue) => {
  178. setUserTerms(newValue);
  179. }}
  180. />
  181. <Text style={styles.text}>{t('login.termsofService')}
  182. <Text style={styles.userTerms} onPress={goToUserTermsPage}>{t('login.termsofService1')}</Text>
  183. </Text>
  184. </View>
  185. <NormalButton
  186. extendedStyle={{ padding: 20 }}
  187. onPress={() => _login(loginPhone, loginPassword)}
  188. title={
  189. isLoading ? (
  190. <Text
  191. style={{
  192. fontWeight: '700',
  193. fontSize: 20,
  194. color: '#fff'
  195. }}
  196. >
  197. <ActivityIndicator />
  198. </Text>
  199. ) : (
  200. <Text
  201. style={{
  202. fontWeight: '700',
  203. fontSize: 20,
  204. color: '#fff'
  205. }}
  206. >
  207. {t('login.signIn')}
  208. </Text>
  209. )
  210. }
  211. />
  212. <View className="flex flex-row justify-between relative">
  213. <Pressable className="self-start" onPress={goToNextPage}>
  214. <Text style={styles.text}>{t('login.register')}</Text>
  215. </Pressable>
  216. <Pressable className="self-start" onPress={() => goToForgetPassWordPage()}>
  217. <Text style={styles.text}>{t('login.forgotPassword')}</Text>
  218. </Pressable>
  219. </View>
  220. </View>
  221. <View className="pb-6" />
  222. </View>
  223. </ScrollView>
  224. );
  225. };
  226. const styles = StyleSheet.create({
  227. container: {
  228. flex: 1,
  229. height: Dimensions.get('window').height,
  230. justifyContent: 'center',
  231. marginHorizontal: 20
  232. },
  233. topContainerForLogo: {},
  234. checkbox: {
  235. marginLeft: 8,
  236. marginRight: 8
  237. },
  238. text: {
  239. color: '#02677D',
  240. fontSize: 16,
  241. paddingVertical: 5
  242. },
  243. userTerms: {
  244. color: '#02677D',
  245. fontSize: 16,
  246. paddingVertical: 5,
  247. textDecorationLine: 'underline'
  248. }
  249. });
  250. export default LoginPage;