mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 19:59:20 +00:00
🗂Better folder structure on frontend
This commit is contained in:
19
frontend/src/context/Auth/AuthContext.js
Normal file
19
frontend/src/context/Auth/AuthContext.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import React, { createContext } from "react";
|
||||
|
||||
import useAuth from "./useAuth";
|
||||
|
||||
const AuthContext = createContext();
|
||||
|
||||
const AuthProvider = ({ children }) => {
|
||||
const { isAuth, loading, handleLogin, handleLogout } = useAuth();
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{ loading, isAuth, handleLogin, handleLogout }}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export { AuthContext, AuthProvider };
|
||||
70
frontend/src/context/Auth/useAuth.js
Normal file
70
frontend/src/context/Auth/useAuth.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useHistory } from "react-router-dom";
|
||||
|
||||
import api from "../../services/api";
|
||||
|
||||
const useAuth = () => {
|
||||
const history = useHistory();
|
||||
const [isAuth, setIsAuth] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem("token");
|
||||
|
||||
if (token) {
|
||||
api.defaults.headers.Authorization = `Bearer ${JSON.parse(token)}`;
|
||||
setIsAuth(true);
|
||||
}
|
||||
const checkAuth = async () => {
|
||||
if (
|
||||
history.location.pathname === "/login" ||
|
||||
history.location.pathname === "/signup"
|
||||
) {
|
||||
setLoading(false);
|
||||
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = await api.get("/auth/check");
|
||||
if (res.status === 200) {
|
||||
setIsAuth(true);
|
||||
setLoading(false);
|
||||
}
|
||||
} catch (err) {
|
||||
setLoading(false);
|
||||
setIsAuth(false);
|
||||
alert("Erro de autenticação. Por favor, faça login novamente");
|
||||
}
|
||||
};
|
||||
checkAuth();
|
||||
}, [history.location.pathname]);
|
||||
|
||||
const handleLogin = async (e, user) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
const res = await api.post("/auth/login", user);
|
||||
localStorage.setItem("token", JSON.stringify(res.data.token));
|
||||
localStorage.setItem("username", res.data.username);
|
||||
localStorage.setItem("userId", res.data.userId);
|
||||
api.defaults.headers.Authorization = `Bearer ${res.data.token}`;
|
||||
setIsAuth(true);
|
||||
history.push("/chat");
|
||||
} catch (err) {
|
||||
alert(err);
|
||||
}
|
||||
};
|
||||
|
||||
const handleLogout = e => {
|
||||
e.preventDefault();
|
||||
setIsAuth(false);
|
||||
localStorage.removeItem("token");
|
||||
localStorage.removeItem("username");
|
||||
localStorage.removeItem("userId");
|
||||
api.defaults.headers.Authorization = undefined;
|
||||
history.push("/login");
|
||||
};
|
||||
|
||||
return { isAuth, loading, handleLogin, handleLogout };
|
||||
};
|
||||
|
||||
export default useAuth;
|
||||
Reference in New Issue
Block a user