import React, { useState, useRef, useEffect, useGlobal } from "reactn"; import { Text, View, KeyboardAvoidingView, Image, Pressable, } from "react-native"; import AsyncStorage from "@react-native-async-storage/async-storage"; import { responsiveHeight, responsiveWidth, } from "../actions/responsiveSizes.js"; import firebase from "../config/firebase"; import Button from "../components/Button"; import { Input } from "../components/Input"; import { Fonts, gutters, Palette, Style } from "../styles"; import { Routes } from "../navigation"; import { art, mockups } from "../assets"; import { checkIfEmailIsValid, checkIfPasswordIsStrongEnough, handleFirebaseError, } from "../actions/signupActions"; import useLayoutType from "../hooks/useLayoutType.js"; import { capitalize } from "../helpers/index.js"; export default ({ navigation }) => { console.log("LOGINSCREEN"); const [, setTooltip] = useGlobal("_tooltip"); const [, setIsLoading] = useGlobal("_isLoading"); const [mode, setMode] = useState("LOGIN"); const [emailIsStatic, setEmailIsStatic] = useState(false); const [email, setEmail] = useState("login@minuit.com"); const [password, setPassword] = useState("password"); const [firstName, setFirstName] = useState(""); const [lastName, setLastName] = useState(""); const passwordInputRef = useRef(); const { isDesktop } = useLayoutType(); useEffect(() => { const checkIfUserIsLoggedIn = async () => { const userEmail = await AsyncStorage.getItem("userEmail"); if (userEmail) { setEmail(userEmail); setEmailIsStatic(true); } }; checkIfUserIsLoggedIn(); }, []); const onLogin = async () => { try { setIsLoading(true); let trimmedEmail = email.trim(); await firebase.auth().signInWithEmailAndPassword(trimmedEmail, password); await AsyncStorage.setItem("userEmail", trimmedEmail); onAuthSuccess(); } catch (error) { console.log(error); setTooltip({ text: handleFirebaseError(error.code), type: "error", }); } finally { setIsLoading(false); } }; const onSignup = async () => { try { setIsLoading(true); if (!checkIfPasswordIsStrongEnough({ password })) { throw new Error( "Votre mot de passe doit contenir au moins 6 caractères, une majuscule et un chiffre." ); } if (!checkIfEmailIsValid({ email })) { throw new Error("Veuillez renseigner une adresse email valide."); } const { user = null } = await firebase .auth() .createUserWithEmailAndPassword(email, password); if (!user) { throw new Error("Erreur lors de la création du compte"); } let userObject = { firstName: capitalize(firstName.trim()), lastName: capitalize(lastName.trim()), }; // push user data to firestore onAuthSuccess(); setTooltip({ text: "Votre compte a été créé avec succès!", type: "success", }); } catch (error) { console.log(error); setTooltip({ text: error?.code?.startsWith("auth/") ? handleFirebaseError(error.code) : error.message, type: "error", }); } finally { setIsLoading(false); } }; const onAuthSuccess = async () => { navigation.reset({ index: 0, routes: [{ name: Routes.Splash }], }); }; return ( minuit.starter {emailIsStatic && mode === "LOGIN" ? ( <> {`Vous êtes connecté(e) avec l'adresse ${email},`} { await AsyncStorage.removeItem("userEmail"); setEmailIsStatic(false); }} > Modifier ) : ( passwordInputRef.current.focus(), }} /> )} {mode === "LOGIN" ? ( navigation.navigate("ForgotPassword")} style={{ marginBottom: gutters }} > Mot de passe oublié? ) : ( <> )}