import React, { useEffect, useState } from "react"; import { View, Text, TouchableOpacity, StyleSheet } from "react-native"; import MaterialIcons from "@expo/vector-icons/MaterialIcons"; import Ionicons from "@expo/vector-icons/Ionicons"; import LoginSVG from "@assets/images/login.svg"; import GoogleSVG from "@assets/images/google.svg"; import FacebookSVG from "@assets/images/facebook.svg"; import TwitterSVG from "@assets/images/twitter.svg"; import CustomButton from "@components/Buttons/CustomButton"; import InputField from "@components/InputField"; import { globalStyles } from "@styles/global"; import Loader from "@components/Loader"; import { Formik } from "formik"; import { loginSchema } from "@schemas/loginSchema"; import { useDispatch } from "react-redux"; import useAuthHook from "../hooks/useAuthHook"; import Layout from "@components/Layout/Layout"; import { useTheme } from "@styles"; import { useTranslation } from "react-i18next"; import { useLoginMutation } from "@features/auth/authApiSlice"; import { setCredentials } from "@features/auth/authSlice"; import { useAuthProviderMutation } from "@features/auth/authApiSlice"; import { storeData } from "@service/asyncStorage"; import { ACCESS_TOKEN } from "@constants/localStorage"; const LoginScreen = ({ navigation }) => { const [authProvider, { isLoading: isLoadingProvider }] = useAuthProviderMutation(); const [currentProvider, setCurrentProvider] = useState(""); const { colors } = useTheme(); const { t } = useTranslation(); const [login, { isLoading, error }] = useLoginMutation(); const { response, promptAsync } = useAuthHook(); const dispatch = useDispatch(); const authProviderHandler = async (response) => { if (response?.type === "success") { const accessToken = response.authentication.accessToken; if (accessToken) { await storeData(ACCESS_TOKEN, accessToken) try { const userResponse = await authProvider({ provider: currentProvider, accessToken, }).unwrap(); if (userResponse) { dispatch(setCredentials(userResponse)); } } catch (e) { console.log(e); } } } }; useEffect(() => { authProviderHandler(response); }, [response, currentProvider]); const handleGoogleAuth = () => { promptAsync(); }; const handleLogin = async (values) => { const { email: identifier, password } = values; try { const userData = await login({ identifier, password }).unwrap(); dispatch(setCredentials(userData)); } catch (e) { console.log("Login error", e); } }; return ( {({ handleChange, handleBlur, handleSubmit, values, isValid, errors, }) => ( <> {t("login.signIn")} } /> {errors.email && ( {errors.email} )} {}} inputType="password" handleBlur={handleBlur("password")} text={values.password} onChangeText={handleChange("password")} icon={ } /> {errors.password && ( {errors.password} )} {error && ( {error?.data?.error?.message} )} )} {t("login.orLoginWith")} { setCurrentProvider("google"); handleGoogleAuth(); }} style={globalStyles.iconButton} > {}} style={globalStyles.iconButton}> {}} style={globalStyles.iconButton}> {t("login.needAccount")}{" "} navigation.navigate("Register")}> {t("login.signUp")} ); }; const styles = StyleSheet.create({ providerText: { textAlign: "center", color: "#666", marginBottom: 30, fontFamily: "poppins-regular", }, providersContainer: { flexDirection: "row", justifyContent: "space-between", marginBottom: 30, }, registerContainer: { flexDirection: "row", justifyContent: "center", marginBottom: 30, }, registerButtonText: { color: "#AD40AF", fontWeight: "700", fontFamily: "poppins-semibold", }, errorMessage: { marginBottom: 30, color: "red", }, }); export default LoginScreen;