merge conflicts
This commit is contained in:
commit
262ca85dbc
6 changed files with 45 additions and 10 deletions
|
|
@ -3,25 +3,21 @@ import { Navigate } from "react-router-dom";
|
||||||
import { AuthContext } from "../../contexts/authContext";
|
import { AuthContext } from "../../contexts/authContext";
|
||||||
|
|
||||||
export const ProtectedAdminRoute = ({ children }) => {
|
export const ProtectedAdminRoute = ({ children }) => {
|
||||||
const { isAuthenticated, logout, getAuthentication, userData } =
|
const { isAdmin, isAuthenticated, logout, getAuthentication, userData } =
|
||||||
useContext(AuthContext);
|
useContext(AuthContext);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isAuthenticated && !getAuthentication()) {
|
if (!isAuthenticated && !getAuthentication()) {
|
||||||
window.location.replace("/login");
|
window.location.replace("/login");
|
||||||
logout();
|
logout();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userData && userData?.is_superuser === false) {
|
|
||||||
logout();
|
|
||||||
}
|
|
||||||
}, [isAuthenticated, getAuthentication, logout, userData]);
|
}, [isAuthenticated, getAuthentication, logout, userData]);
|
||||||
|
|
||||||
if (!isAuthenticated && !getAuthentication()) {
|
if (!isAuthenticated && !getAuthentication()) {
|
||||||
return <Navigate to="/login" replace />;
|
return <Navigate to="/login" replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userData && userData?.is_superuser === false) {
|
if (userData && !isAdmin) {
|
||||||
return <Navigate to="/login" replace />;
|
return <Navigate to="/" replace />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return children;
|
return children;
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ const ForwardedIconComponent = forwardRef(
|
||||||
<TargetIcon
|
<TargetIcon
|
||||||
strokeWidth={1.5}
|
strokeWidth={1.5}
|
||||||
className={className}
|
className={className}
|
||||||
style={{ color: iconColor }}
|
style={iconColor ? { color: iconColor } : {}}
|
||||||
ref={ref}
|
ref={ref}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -7,11 +7,12 @@ import { alertContext } from "../../contexts/alertContext";
|
||||||
import { AuthContext } from "../../contexts/authContext";
|
import { AuthContext } from "../../contexts/authContext";
|
||||||
import { darkContext } from "../../contexts/darkContext";
|
import { darkContext } from "../../contexts/darkContext";
|
||||||
import { TabsContext } from "../../contexts/tabsContext";
|
import { TabsContext } from "../../contexts/tabsContext";
|
||||||
import { getRepoStars } from "../../controllers/API";
|
import { getLoggedUser, getRepoStars } from "../../controllers/API";
|
||||||
import IconComponent from "../genericIconComponent";
|
import IconComponent from "../genericIconComponent";
|
||||||
import { Button } from "../ui/button";
|
import { Button } from "../ui/button";
|
||||||
import { Separator } from "../ui/separator";
|
import { Separator } from "../ui/separator";
|
||||||
import MenuBar from "./components/menuBar";
|
import MenuBar from "./components/menuBar";
|
||||||
|
import { Users } from "../../types/api";
|
||||||
|
|
||||||
export default function Header() {
|
export default function Header() {
|
||||||
const { flows, tabId } = useContext(TabsContext);
|
const { flows, tabId } = useContext(TabsContext);
|
||||||
|
|
@ -23,6 +24,8 @@ export default function Header() {
|
||||||
|
|
||||||
const [stars, setStars] = useState(null);
|
const [stars, setStars] = useState(null);
|
||||||
|
|
||||||
|
const { isAdmin, setIsAdmin } = useContext(AuthContext);
|
||||||
|
|
||||||
// Get and set numbers of stars on header
|
// Get and set numbers of stars on header
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchStars() {
|
async function fetchStars() {
|
||||||
|
|
@ -31,6 +34,7 @@ export default function Header() {
|
||||||
}
|
}
|
||||||
fetchStars();
|
fetchStars();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="header-arrangement">
|
<div className="header-arrangement">
|
||||||
<div className="header-start-display">
|
<div className="header-start-display">
|
||||||
|
|
@ -50,6 +54,16 @@ export default function Header() {
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{isAdmin && (
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
onClick={() => navigate("/admin")}
|
||||||
|
>
|
||||||
|
Admin page
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
|
||||||
|
|
||||||
{flows.findIndex((f) => tabId === f.id) !== -1 && tabId !== "" && (
|
{flows.findIndex((f) => tabId === f.id) !== -1 && tabId !== "" && (
|
||||||
<MenuBar flows={flows} tabId={tabId} />
|
<MenuBar flows={flows} tabId={tabId} />
|
||||||
)}
|
)}
|
||||||
|
|
@ -132,6 +146,12 @@ export default function Header() {
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</AlertDropdown>
|
</AlertDropdown>
|
||||||
|
<button>
|
||||||
|
<IconComponent
|
||||||
|
name="Key"
|
||||||
|
className="side-bar-button-size text-muted-foreground hover:text-accent-foreground"
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,8 @@ export default function InputComponent({
|
||||||
? "input-component-true-button"
|
? "input-component-true-button"
|
||||||
: "input-component-false-button"
|
: "input-component-false-button"
|
||||||
)}
|
)}
|
||||||
onClick={() => {
|
onClick={(event) => {
|
||||||
|
event.preventDefault();
|
||||||
setPwdVisible(!pwdVisible);
|
setPwdVisible(!pwdVisible);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,11 @@ import { createContext, useEffect, useState } from "react";
|
||||||
import Cookies from "universal-cookie";
|
import Cookies from "universal-cookie";
|
||||||
import { Users } from "../types/api";
|
import { Users } from "../types/api";
|
||||||
import { AuthContextType } from "../types/contexts/auth";
|
import { AuthContextType } from "../types/contexts/auth";
|
||||||
|
import { getLoggedUser } from "../controllers/API";
|
||||||
|
|
||||||
const initialValue: AuthContextType = {
|
const initialValue: AuthContextType = {
|
||||||
|
isAdmin: false,
|
||||||
|
setIsAdmin: () => false,
|
||||||
isAuthenticated: false,
|
isAuthenticated: false,
|
||||||
accessToken: null,
|
accessToken: null,
|
||||||
refreshToken: null,
|
refreshToken: null,
|
||||||
|
|
@ -24,6 +27,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const [accessToken, setAccessToken] = useState<string | null>(null);
|
const [accessToken, setAccessToken] = useState<string | null>(null);
|
||||||
const [refreshToken, setRefreshToken] = useState<string | null>(null);
|
const [refreshToken, setRefreshToken] = useState<string | null>(null);
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
|
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
|
||||||
|
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
||||||
const [userData, setUserData] = useState<Users>(null);
|
const [userData, setUserData] = useState<Users>(null);
|
||||||
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
||||||
const cookies = new Cookies();
|
const cookies = new Cookies();
|
||||||
|
|
@ -35,6 +39,15 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (accessToken) {
|
||||||
|
getLoggedUser().then((user) => {
|
||||||
|
const isSuperUser = user.is_superuser
|
||||||
|
setIsAdmin(isSuperUser);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [accessToken, isAdmin])
|
||||||
|
|
||||||
function getAuthentication() {
|
function getAuthentication() {
|
||||||
const storedRefreshToken = cookies.get("refresh_token");
|
const storedRefreshToken = cookies.get("refresh_token");
|
||||||
const storedAccess = cookies.get("access_token");
|
const storedAccess = cookies.get("access_token");
|
||||||
|
|
@ -72,6 +85,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
login(data.accessToken, refreshToken);
|
login(data.accessToken, refreshToken);
|
||||||
|
getLoggedUser().then((user) => { console.log('oi')});
|
||||||
} else {
|
} else {
|
||||||
logout();
|
logout();
|
||||||
}
|
}
|
||||||
|
|
@ -84,6 +98,8 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
// !! to convert string to boolean
|
// !! to convert string to boolean
|
||||||
<AuthContext.Provider
|
<AuthContext.Provider
|
||||||
value={{
|
value={{
|
||||||
|
isAdmin,
|
||||||
|
setIsAdmin,
|
||||||
isAuthenticated: !!accessToken,
|
isAuthenticated: !!accessToken,
|
||||||
accessToken,
|
accessToken,
|
||||||
refreshToken,
|
refreshToken,
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import { Users } from "../api";
|
import { Users } from "../api";
|
||||||
|
|
||||||
export type AuthContextType = {
|
export type AuthContextType = {
|
||||||
|
isAdmin: boolean;
|
||||||
|
setIsAdmin: (isAdmin: boolean) => void;
|
||||||
isAuthenticated: boolean;
|
isAuthenticated: boolean;
|
||||||
accessToken: string | null;
|
accessToken: string | null;
|
||||||
refreshToken: string | null;
|
refreshToken: string | null;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue