feat: initial import — ClickerZ formation project (Express + React/Vite)
This commit is contained in:
142
Frontend/src/context/AuthContext.jsx
Executable file
142
Frontend/src/context/AuthContext.jsx
Executable file
@@ -0,0 +1,142 @@
|
||||
import React, {
|
||||
createContext,
|
||||
useContext,
|
||||
useState,
|
||||
useMemo,
|
||||
useEffect,
|
||||
} from "react";
|
||||
import PropTypes from "prop-types";
|
||||
import axios from "axios";
|
||||
import { jwtDecode } from "jwt-decode";
|
||||
|
||||
const AuthContext = createContext();
|
||||
|
||||
const AuthProvider = ({ children }) => {
|
||||
const [user, setUser] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
const jwtToken = localStorage.getItem("token");
|
||||
|
||||
if (jwtToken) {
|
||||
try {
|
||||
const decodedPayload = jwtDecode(jwtToken);
|
||||
const res = await axios.get(
|
||||
`http://localhost:3310/api/users/${decodedPayload.user}`
|
||||
);
|
||||
setUser(res.data);
|
||||
} catch (error) {
|
||||
console.error("Error fetching user data:", error);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
} else {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchData();
|
||||
}, []);
|
||||
|
||||
|
||||
const logout = () => {
|
||||
localStorage.removeItem("token");
|
||||
setUser(null);
|
||||
};
|
||||
const editUser = async (updatedFields) => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${import.meta.env.VITE_BACKEND_URL}/api/users/${user.id}`,
|
||||
{
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${localStorage.getItem("token")}`,
|
||||
},
|
||||
body: JSON.stringify(updatedFields),
|
||||
}
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
const updatedUser = await response.json();
|
||||
setUser((prevUser) => ({
|
||||
...prevUser,
|
||||
...updatedUser.user,
|
||||
}));
|
||||
return "User updated successfully";
|
||||
}
|
||||
if (response.status === 400) {
|
||||
console.error("Bad Request:", response.statusText);
|
||||
throw new Error("Bad Request");
|
||||
} else if (response.status === 401) {
|
||||
console.error("Unauthorized:", response.statusText);
|
||||
throw new Error("Unauthorized");
|
||||
} else {
|
||||
console.error("Error updating user:", response.statusText);
|
||||
throw new Error("Error updating user");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error updating user:", error);
|
||||
throw new Error("An error occurred during user update");
|
||||
}
|
||||
};
|
||||
|
||||
const sendPasswordResetEmail = async (email) => {
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${import.meta.env.VITE_BACKEND_URL}/api/forgot-password`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ mail: email }),
|
||||
}
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
return "Password reset email sent successfully";
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
throw new Error(data.message || "Error sending password reset email");
|
||||
} catch (error) {
|
||||
console.error("Error sending password reset email:", error);
|
||||
throw new Error(
|
||||
"An error occurred while sending the password reset email"
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const authContextValue = useMemo(() => {
|
||||
return {
|
||||
user,
|
||||
loading,
|
||||
logout,
|
||||
editUser,
|
||||
sendPasswordResetEmail,
|
||||
setUser: (newUser) => {
|
||||
setUser(newUser);
|
||||
},
|
||||
};
|
||||
}, [user, loading, logout]);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={authContextValue}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
AuthProvider.propTypes = {
|
||||
children: PropTypes.node.isRequired,
|
||||
};
|
||||
|
||||
const useAuth = () => {
|
||||
const context = useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error("useAuth must be used within an AuthProvider");
|
||||
}
|
||||
return context;
|
||||
};
|
||||
Reference in New Issue
Block a user