feat: agregar request para profile
This commit is contained in:
parent
a42e769c3e
commit
6a9b719226
3 changed files with 43 additions and 8 deletions
|
|
@ -9,7 +9,7 @@ const UserProvider = ({ children }) => {
|
|||
const storeData = (email, token) => {
|
||||
setEmail(email);
|
||||
setToken(token);
|
||||
localStorage.setItem("loginToken", data.token);
|
||||
localStorage.setItem("loginToken", token);
|
||||
};
|
||||
const login = async (user, password) => {
|
||||
const res = await fetch("http://localhost:5000/api/auth/login", {
|
||||
|
|
@ -40,8 +40,29 @@ const UserProvider = ({ children }) => {
|
|||
setToken(null);
|
||||
setEmail(null);
|
||||
};
|
||||
const getProfile = async () => {
|
||||
const res = await fetch("http://localhost:5000/api/auth/me", {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
if (!res.ok) {
|
||||
throw new Error("Error al obtener perfil", res.status);
|
||||
}
|
||||
const data = await res.json();
|
||||
return data;
|
||||
};
|
||||
return (
|
||||
<UserContext.Provider value={{ token, setToken, email, setEmail, login, register, logout }}>
|
||||
<UserContext.Provider
|
||||
value={{
|
||||
token,
|
||||
setToken,
|
||||
email,
|
||||
setEmail,
|
||||
login,
|
||||
register,
|
||||
getProfile,
|
||||
logout,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</UserContext.Provider>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -4,12 +4,15 @@ import { useContext } from "react";
|
|||
// Librería recomendada en tutoría
|
||||
import { z } from "zod";
|
||||
import { UserContext } from "../context/UserContext";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
// Esquema de validación de login
|
||||
const loginSchema = z.object({
|
||||
user: z.email({ error: "Email inválido" }).nonempty({ error: "Se requiere un email" }),
|
||||
password: z.string().min(6, { error: "La contraseña debe tener al menos 6 caracteres" }),
|
||||
user: z
|
||||
.email({ error: "Email inválido" })
|
||||
.nonempty({ error: "Se requiere un email" }),
|
||||
password: z
|
||||
.string()
|
||||
.min(6, { error: "La contraseña debe tener al menos 6 caracteres" }),
|
||||
});
|
||||
|
||||
const Login = () => {
|
||||
|
|
@ -21,6 +24,7 @@ const Login = () => {
|
|||
try {
|
||||
await login(user, password);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
alert("Error de login");
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,9 +1,19 @@
|
|||
import { useContext } from "react";
|
||||
import { useContext, useState } from "react";
|
||||
import { UserContext } from "../context/UserContext";
|
||||
import { useEffect } from "react";
|
||||
|
||||
const Profile = () => {
|
||||
const email = "test@example.com";
|
||||
const { logout } = useContext(UserContext);
|
||||
const [email, setEmail] = useState("");
|
||||
const { getProfile, logout } = useContext(UserContext);
|
||||
useEffect(() => {
|
||||
getProfile()
|
||||
.then((data) => {
|
||||
setEmail(data.email);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(error);
|
||||
});
|
||||
});
|
||||
return (
|
||||
<div>
|
||||
<p>
|
||||
|
|
|
|||
Loading…
Reference in a new issue