refactored authentication
This commit is contained in:
parent
f5ae805db1
commit
7f8f5d3f7b
8 changed files with 31 additions and 71 deletions
|
|
@ -130,13 +130,13 @@ export default function App() {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const { getAuthentication } = useContext(AuthContext);
|
const { isAuthenticated } = useContext(AuthContext);
|
||||||
const { refreshFlows, setVersion } = useContext(FlowsContext);
|
const { refreshFlows, setVersion } = useContext(FlowsContext);
|
||||||
const { getTypes } = useContext(typesContext);
|
const { getTypes } = useContext(typesContext);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// If the user is authenticated, fetch the types. This code is important to check if the user is auth because of the execution order of the useEffect hooks.
|
// If the user is authenticated, fetch the types. This code is important to check if the user is auth because of the execution order of the useEffect hooks.
|
||||||
if (getAuthentication() === true) {
|
if (isAuthenticated === true) {
|
||||||
// get data from db
|
// get data from db
|
||||||
refreshFlows();
|
refreshFlows();
|
||||||
getTypes();
|
getTypes();
|
||||||
|
|
@ -145,7 +145,7 @@ export default function App() {
|
||||||
getVersion().then((data) => {
|
getVersion().then((data) => {
|
||||||
setVersion(data.version);
|
setVersion(data.version);
|
||||||
});
|
});
|
||||||
}, [getAuthentication()]);
|
}, [isAuthenticated]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
//need parent component with width and height
|
//need parent component with width and height
|
||||||
|
|
|
||||||
|
|
@ -7,18 +7,12 @@ export const ProtectedAdminRoute = ({ children }) => {
|
||||||
isAdmin,
|
isAdmin,
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
logout,
|
logout,
|
||||||
getAuthentication,
|
|
||||||
userData,
|
userData,
|
||||||
autoLogin,
|
autoLogin,
|
||||||
} = useContext(AuthContext);
|
} = useContext(AuthContext);
|
||||||
useEffect(() => {
|
|
||||||
if (!isAuthenticated && !getAuthentication()) {
|
|
||||||
window.location.replace("/login");
|
|
||||||
logout();
|
|
||||||
}
|
|
||||||
}, [isAuthenticated, getAuthentication, logout, userData]);
|
|
||||||
|
|
||||||
if (!isAuthenticated && !getAuthentication()) {
|
if (!isAuthenticated) {
|
||||||
|
logout();
|
||||||
return <Navigate to="/login" replace />;
|
return <Navigate to="/login" replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@ import { Navigate } from "react-router-dom";
|
||||||
import { AuthContext } from "../../contexts/authContext";
|
import { AuthContext } from "../../contexts/authContext";
|
||||||
|
|
||||||
export const ProtectedRoute = ({ children }) => {
|
export const ProtectedRoute = ({ children }) => {
|
||||||
const { isAuthenticated, logout, getAuthentication } =
|
const { isAuthenticated, logout} =
|
||||||
useContext(AuthContext);
|
useContext(AuthContext);
|
||||||
if (!isAuthenticated && !getAuthentication()) {
|
if (!isAuthenticated) {
|
||||||
logout();
|
logout();
|
||||||
return <Navigate to="/login" replace />;
|
return <Navigate to="/login" replace />;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,14 @@ import { Navigate } from "react-router-dom";
|
||||||
import { AuthContext } from "../../contexts/authContext";
|
import { AuthContext } from "../../contexts/authContext";
|
||||||
|
|
||||||
export const ProtectedLoginRoute = ({ children }) => {
|
export const ProtectedLoginRoute = ({ children }) => {
|
||||||
const { getAuthentication, autoLogin } = useContext(AuthContext);
|
const { isAuthenticated, autoLogin } = useContext(AuthContext);
|
||||||
|
|
||||||
if (autoLogin === true) {
|
if (autoLogin === true) {
|
||||||
window.location.replace("/");
|
window.location.replace("/");
|
||||||
return <Navigate to="/" replace />;
|
return <Navigate to="/" replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getAuthentication()) {
|
if (isAuthenticated) {
|
||||||
window.location.replace("/");
|
window.location.replace("/");
|
||||||
return <Navigate to="/" replace />;
|
return <Navigate to="/" replace />;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@ const initialValue: AuthContextType = {
|
||||||
logout: () => {},
|
logout: () => {},
|
||||||
userData: null,
|
userData: null,
|
||||||
setUserData: () => {},
|
setUserData: () => {},
|
||||||
getAuthentication: () => false,
|
|
||||||
authenticationErrorCount: 0,
|
authenticationErrorCount: 0,
|
||||||
autoLogin: false,
|
autoLogin: false,
|
||||||
setAutoLogin: () => {},
|
setAutoLogin: () => {},
|
||||||
|
|
@ -34,7 +33,9 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const [refreshToken, setRefreshToken] = useState<string | null>(
|
const [refreshToken, setRefreshToken] = useState<string | null>(
|
||||||
cookies.get("refresh_tkn_lflw")
|
cookies.get("refresh_tkn_lflw")
|
||||||
);
|
);
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
|
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(
|
||||||
|
cookies.get("refresh_tkn_lflw") && cookies.get("access_tkn_lflw")
|
||||||
|
);
|
||||||
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
||||||
const [userData, setUserData] = useState<Users | null>(null);
|
const [userData, setUserData] = useState<Users | null>(null);
|
||||||
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
||||||
|
|
@ -72,29 +73,26 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
setAutoLogin(false);
|
setAutoLogin(false);
|
||||||
if (getAuthentication() && !isLoginPage) {
|
if (isAuthenticated && !isLoginPage) {
|
||||||
getLoggedUser()
|
getUser();
|
||||||
.then((user) => {
|
|
||||||
setUserData(user);
|
|
||||||
setLoading(false);
|
|
||||||
const isSuperUser = user!.is_superuser;
|
|
||||||
setIsAdmin(isSuperUser);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.log("auth context");
|
|
||||||
setLoading(false);
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}, [setUserData, setLoading, autoLogin, setIsAdmin]);
|
}, [setUserData, setLoading, autoLogin, setIsAdmin]);
|
||||||
|
|
||||||
function getAuthentication() {
|
function getUser(){
|
||||||
const storedRefreshToken = cookies.get("refresh_tkn_lflw");
|
getLoggedUser()
|
||||||
const storedAccess = cookies.get("access_tkn_lflw");
|
.then((user) => {
|
||||||
const auth = storedAccess && storedRefreshToken ? true : false;
|
setUserData(user);
|
||||||
return auth;
|
setLoading(false);
|
||||||
|
const isSuperUser = user!.is_superuser;
|
||||||
|
setIsAdmin(isSuperUser);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log("auth context");
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function login(newAccessToken: string, refreshToken: string) {
|
function login(newAccessToken: string, refreshToken: string) {
|
||||||
|
|
@ -103,6 +101,8 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
setAccessToken(newAccessToken);
|
setAccessToken(newAccessToken);
|
||||||
setRefreshToken(refreshToken);
|
setRefreshToken(refreshToken);
|
||||||
setIsAuthenticated(true);
|
setIsAuthenticated(true);
|
||||||
|
setTimeout(() => {getUser();}, 500)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
|
|
@ -127,14 +127,13 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
value={{
|
value={{
|
||||||
isAdmin,
|
isAdmin,
|
||||||
setIsAdmin,
|
setIsAdmin,
|
||||||
isAuthenticated: !!accessToken,
|
isAuthenticated,
|
||||||
accessToken,
|
accessToken,
|
||||||
refreshToken,
|
refreshToken,
|
||||||
login,
|
login,
|
||||||
logout,
|
logout,
|
||||||
setUserData,
|
setUserData,
|
||||||
userData,
|
userData,
|
||||||
getAuthentication,
|
|
||||||
authenticationErrorCount: 0,
|
authenticationErrorCount: 0,
|
||||||
setAutoLogin,
|
setAutoLogin,
|
||||||
autoLogin,
|
autoLogin,
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ export default function LoginAdminPage() {
|
||||||
|
|
||||||
const [inputState, setInputState] =
|
const [inputState, setInputState] =
|
||||||
useState<loginInputStateType>(CONTROL_LOGIN_STATE);
|
useState<loginInputStateType>(CONTROL_LOGIN_STATE);
|
||||||
const { login, getAuthentication, setUserData } = useContext(AuthContext);
|
const { login, isAuthenticated, setUserData } = useContext(AuthContext);
|
||||||
|
|
||||||
const { password, username } = inputState;
|
const { password, username } = inputState;
|
||||||
const setErrorData = useAlertStore((state) => state.setErrorData);
|
const setErrorData = useAlertStore((state) => state.setErrorData);
|
||||||
|
|
@ -35,7 +35,6 @@ export default function LoginAdminPage() {
|
||||||
onLogin(user)
|
onLogin(user)
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
login(user.access_token, user.refresh_token);
|
login(user.access_token, user.refresh_token);
|
||||||
getUser();
|
|
||||||
navigate("/admin/");
|
navigate("/admin/");
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
|
@ -46,20 +45,6 @@ export default function LoginAdminPage() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUser() {
|
|
||||||
if (getAuthentication()) {
|
|
||||||
setTimeout(() => {
|
|
||||||
getLoggedUser()
|
|
||||||
.then((user) => {
|
|
||||||
setUserData(user);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.log("login admin page", error);
|
|
||||||
});
|
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex h-full w-full flex-col items-center justify-center bg-muted">
|
<div className="flex h-full w-full flex-col items-center justify-center bg-muted">
|
||||||
<div className="flex w-72 flex-col items-center justify-center gap-2">
|
<div className="flex w-72 flex-col items-center justify-center gap-2">
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ export default function LoginPage(): JSX.Element {
|
||||||
useState<loginInputStateType>(CONTROL_LOGIN_STATE);
|
useState<loginInputStateType>(CONTROL_LOGIN_STATE);
|
||||||
|
|
||||||
const { password, username } = inputState;
|
const { password, username } = inputState;
|
||||||
const { login, getAuthentication, setUserData, setIsAdmin } =
|
const { login, isAuthenticated, setUserData, setIsAdmin } =
|
||||||
useContext(AuthContext);
|
useContext(AuthContext);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const setErrorData = useAlertStore((state) => state.setErrorData);
|
const setErrorData = useAlertStore((state) => state.setErrorData);
|
||||||
|
|
@ -38,7 +38,6 @@ export default function LoginPage(): JSX.Element {
|
||||||
onLogin(user)
|
onLogin(user)
|
||||||
.then((user) => {
|
.then((user) => {
|
||||||
login(user.access_token, user.refresh_token);
|
login(user.access_token, user.refresh_token);
|
||||||
getUser();
|
|
||||||
navigate("/");
|
navigate("/");
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
|
@ -49,22 +48,6 @@ export default function LoginPage(): JSX.Element {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getUser() {
|
|
||||||
if (getAuthentication()) {
|
|
||||||
setTimeout(() => {
|
|
||||||
getLoggedUser()
|
|
||||||
.then((user) => {
|
|
||||||
const isSuperUser = user!.is_superuser;
|
|
||||||
setIsAdmin(isSuperUser);
|
|
||||||
setUserData(user);
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.log("login page", error);
|
|
||||||
});
|
|
||||||
}, 500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form.Root
|
<Form.Root
|
||||||
onSubmit={(event) => {
|
onSubmit={(event) => {
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ export type AuthContextType = {
|
||||||
logout: () => void;
|
logout: () => void;
|
||||||
userData: Users | null;
|
userData: Users | null;
|
||||||
setUserData: (userData: Users | null) => void;
|
setUserData: (userData: Users | null) => void;
|
||||||
getAuthentication: () => boolean;
|
|
||||||
authenticationErrorCount: number;
|
authenticationErrorCount: number;
|
||||||
autoLogin: boolean;
|
autoLogin: boolean;
|
||||||
setAutoLogin: (autoLogin: boolean) => void;
|
setAutoLogin: (autoLogin: boolean) => void;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue