loginPage.tsx 4.3 KB

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