loginPage.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import {
  2. View,
  3. Text,
  4. StyleSheet,
  5. Pressable,
  6. Alert,
  7. Dimensions,
  8. ActivityIndicator
  9. } from 'react-native';
  10. import NormalButton from '../../../global/normal_button';
  11. import Logo from '../../../global/logo';
  12. import NormalInput from '../../../global/normal_input';
  13. import { useState } from 'react';
  14. import { useAuth } from '../../../../context/AuthProvider';
  15. type LoginPageProps = {
  16. goToNextPage: () => void;
  17. goToForgetPassWordPage: () => void;
  18. };
  19. const screenHeight = Dimensions.get('window').height;
  20. const LoginPage: React.FC<LoginPageProps> = ({
  21. goToNextPage,
  22. goToForgetPassWordPage
  23. }) => {
  24. const [loginEmail, setLoginEmail] = useState('');
  25. const [loginPassword, setLoginPassword] = useState('');
  26. const [isLoading, setIsLoading] = useState(false);
  27. const { login } = useAuth();
  28. const _login = async (username: string, password: string) => {
  29. setIsLoading(true);
  30. if (username === '' || password === '') {
  31. Alert.alert('Error', 'Please enter a username and password');
  32. } else {
  33. const lowerCaseUsername = username.toLowerCase();
  34. const success = await login(lowerCaseUsername, password);
  35. if (!success) {
  36. Alert.alert('Error', 'Invalid username or password');
  37. }
  38. }
  39. setIsLoading(false);
  40. };
  41. return (
  42. <View style={styles.container} className="">
  43. <View style={styles.topContainerForLogo}>
  44. <Logo />
  45. </View>
  46. <View style={styles.bottomContainer} className="">
  47. <NormalInput
  48. placeholder="電子郵件"
  49. onChangeText={(email) => setLoginEmail(email)}
  50. extendedStyle={{ borderRadius: 12, padding: 20 }}
  51. textContentType="username"
  52. autoComplete="username"
  53. />
  54. <NormalInput
  55. placeholder="密碼"
  56. onChangeText={(password) => setLoginPassword(password)}
  57. secureTextEntry={true}
  58. extendedStyle={{ borderRadius: 12, padding: 20 }}
  59. textContentType="password"
  60. autoComplete="password"
  61. />
  62. <Pressable onPress={() => goToForgetPassWordPage()}>
  63. <Text style={styles.text}>忘記密碼</Text>
  64. </Pressable>
  65. <NormalButton
  66. extendedStyle={{ padding: 20 }}
  67. onPress={() => _login(loginEmail, loginPassword)}
  68. title={
  69. isLoading ? (
  70. <Text
  71. style={{
  72. fontWeight: '700',
  73. fontSize: 20,
  74. color: '#fff'
  75. }}
  76. >
  77. <ActivityIndicator />
  78. </Text>
  79. ) : (
  80. <Text
  81. style={{
  82. fontWeight: '700',
  83. fontSize: 20,
  84. color: '#fff'
  85. }}
  86. >
  87. 登入
  88. </Text>
  89. )
  90. }
  91. />
  92. <Pressable onPress={goToNextPage}>
  93. <Text style={styles.text}>註冊會員</Text>
  94. </Pressable>
  95. </View>
  96. </View>
  97. );
  98. };
  99. const styles = StyleSheet.create({
  100. container: {
  101. flex: 1,
  102. height: Dimensions.get('window').height,
  103. justifyContent: 'center',
  104. marginHorizontal: 20
  105. },
  106. topContainerForLogo: { marginBottom: screenHeight < 700 ? '0%' : '20%' },
  107. bottomContainer: {
  108. marginBottom: '20%',
  109. justifyContent: 'flex-start',
  110. gap: 10
  111. },
  112. text: {
  113. color: '#02677D',
  114. fontSize: 16,
  115. paddingVertical: 5
  116. }
  117. });
  118. export default LoginPage;