import React, { useState, useContext } from "react";
import { StyleSheet, View, Text } from "react-native";
import * as Yup from "yup";
import {
Form,
SubmitButton,
FormField,
ErrorMessage,
} from "../components/forms";
import Screen from "../components/Screen";
import colors from "../config/colors";
import { dimensions } from "../config/dimensions";
import jwdDecode from "jwt-decode";
import { AuthContext } from "../auth/context";
import authStorage from "../auth/storage";
import assets from "../config/assets";
import Button from "../components/Button";
import { TouchableNativeFeedback } from "react-native-gesture-handler";
// import authApi from '../api/auth';
import login from "../api/auth";
const phoneRegex = RegExp(
/^\(?[\(]?([0-9]{2})?\)?[)\b]?([0-9]{4,5})[-. ]?([0-9]{4})$/
);
const validationSchema = Yup.object().shape({
name: Yup.string()
.matches(phoneRegex, "Número inválido")
.required("O número de telefone é obrigatório"),
password: Yup.string()
.required("A senha é obrigatória")
.min(8, "Senha muito curta, minimo 8 caracteres")
.matches(/[a-zA-Z]/, "A senha só pode conter letras"),
});
function DashedOrSeparator() {
return (
OU
);
}
export default function LoginScreen(props) {
const authContext = useContext(AuthContext);
const handleSubmit = async (name, password, setLoginFailed) => {
const result = await login(name, password);
console.log(JSON.stringify(result));
if (!result.ok) return setLoginFailed(true);
setLoginFailed(false);
const user = jwdDecode(result.data);
authContext.setUser(user);
console.log("usuário: " + JSON.sstringify(user));
authStorage.setToken(result.data);
};
const [loginFailed, setLoginFailed] = useState(false);
return (
);
}
const styles = StyleSheet.create({
containter: {
justifyContent: "center",
},
});