Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LoginScreen.jsx 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import React, { useState, useContext } from "react";
  2. import {
  3. SafeAreaView,
  4. View,
  5. Text,
  6. TouchableOpacity,
  7. StyleSheet,
  8. } from "react-native";
  9. import MaterialIcons from "@expo/vector-icons/MaterialIcons";
  10. import Ionicons from "@expo/vector-icons/Ionicons";
  11. import LoginSVG from "../assets/images/login.svg";
  12. import GoogleSVG from "../assets/images/google.svg";
  13. import FacebookSVG from "../assets/images/facebook.svg";
  14. import TwitterSVG from "../assets/images/twitter.svg";
  15. import CustomButton from "../components/Buttons/CustomButton";
  16. import InputField from "../components/InputField";
  17. import { globalStyles } from "../styles/global";
  18. import { AuthContext } from "../context/AuthContext";
  19. import Loader from "../components/Loader";
  20. import { Formik } from "formik";
  21. import { loginSchema } from "../schemas/loginSchema";
  22. const LoginScreen = ({ navigation }) => {
  23. const [error, setError] = useState(null);
  24. const { isLoading, login, googleAuth } = useContext(AuthContext);
  25. const handleLogin = (values) => {
  26. login(values.email, values.password, function (result) {
  27. setError(result.error.message);
  28. });
  29. };
  30. return (
  31. <SafeAreaView style={globalStyles.safeArea}>
  32. <Loader visible={isLoading} />
  33. <View style={{ paddingHorizontal: 25 }}>
  34. <View style={{ alignItems: "center" }}>
  35. <LoginSVG height={300} width={300} />
  36. </View>
  37. <Formik
  38. initialValues={{
  39. email: "",
  40. password: "",
  41. }}
  42. validationSchema={loginSchema}
  43. onSubmit={handleLogin}
  44. validateOnChange={false}
  45. validateOnBlur={false}
  46. >
  47. {({ handleChange, handleBlur, handleSubmit, values, isValid, errors }) => (
  48. <>
  49. <Text style={globalStyles.boldText}>Sign In</Text>
  50. <InputField
  51. name="email"
  52. label={"Email"}
  53. keyboardType="email-address"
  54. onChangeText={handleChange('email')}
  55. text={values.email}
  56. handleBlur={handleBlur('email')}
  57. icon={
  58. <MaterialIcons
  59. name="alternate-email"
  60. size={20}
  61. color="#666"
  62. style={{ marginRight: 5 }}
  63. />
  64. }
  65. />
  66. {errors.email && <Text style={styles.errorMessage}>{errors.email}</Text>}
  67. <InputField
  68. name="password"
  69. label={"Password"}
  70. filedButtonLabel={"Forgot?"}
  71. fieldButtonFunction={() => {}}
  72. inputType="password"
  73. handleBlur={handleBlur('password')}
  74. text={values.password}
  75. onChangeText={handleChange('password')}
  76. icon={
  77. <Ionicons
  78. name="ios-lock-closed-outline"
  79. size={20}
  80. color="#666"
  81. style={{ marginRight: 5 }}
  82. />
  83. }
  84. />
  85. {errors.password && <Text style={styles.errorMessage}>{errors.password}</Text>}
  86. {error && <Text style={styles.errorMessage}>{error}</Text>}
  87. <CustomButton label={"Login"} onPress={handleSubmit} />
  88. </>
  89. )}
  90. </Formik>
  91. <Text style={globalStyles.regularCenteredText}>Or login with ...</Text>
  92. <View style={styles.providersContainer}>
  93. <TouchableOpacity
  94. onPress={googleAuth}
  95. style={globalStyles.iconButton}
  96. >
  97. <GoogleSVG height={24} width={24} />
  98. </TouchableOpacity>
  99. <TouchableOpacity onPress={() => {}} style={globalStyles.iconButton}>
  100. <FacebookSVG height={24} width={24} />
  101. </TouchableOpacity>
  102. <TouchableOpacity onPress={() => {}} style={globalStyles.iconButton}>
  103. <TwitterSVG height={24} width={24} />
  104. </TouchableOpacity>
  105. </View>
  106. <View style={styles.registerContainer}>
  107. <Text style={globalStyles.regularText}>Need account? </Text>
  108. <TouchableOpacity onPress={() => navigation.navigate("Register")}>
  109. <Text style={styles.registerButtonText}>Sign Up</Text>
  110. </TouchableOpacity>
  111. </View>
  112. </View>
  113. </SafeAreaView>
  114. );
  115. };
  116. const styles = StyleSheet.create({
  117. providerText: {
  118. textAlign: "center",
  119. color: "#666",
  120. marginBottom: 30,
  121. fontFamily: "poppins-regular",
  122. },
  123. providersContainer: {
  124. flexDirection: "row",
  125. justifyContent: "space-between",
  126. marginBottom: 30,
  127. },
  128. registerContainer: {
  129. flexDirection: "row",
  130. justifyContent: "center",
  131. marginBottom: 30,
  132. },
  133. registerButtonText: {
  134. color: "#AD40AF",
  135. fontWeight: "700",
  136. fontFamily: "poppins-semibold",
  137. },
  138. errorMessage: {
  139. marginBottom: 30,
  140. color: "red",
  141. },
  142. });
  143. export default LoginScreen;