Skip to content

Commit

Permalink
feat: add authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
jwchoi-kr committed Sep 19, 2024
1 parent b084fb4 commit d7dadd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
41 changes: 27 additions & 14 deletions src/components/admin/AuthRequiredRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import { Navigate, Outlet } from "react-router-dom";
import styled from "styled-components";

import { useCourses } from "../../api/course.ts";
import { useUser } from "../../api/user.ts";
import { useClassId } from "../../hooks/urlParse.tsx";
import SideBar from "../SideBar.tsx";

function AuthRequiredRoute() {
// TODO: handle isError
const { data: user, isLoading } = useUser();
const { data: user, isLoading: isUserLoading } = useUser();
const { data: classList, isLoading: isClassListLoading } = useCourses();

if (isLoading) {
// TODO: apply design
const classId = useClassId();

if (isUserLoading || isClassListLoading) {
return <div></div>;
} else {
if (user) {
return (
<Container>
<SideBar />
<Outlet />
</Container>
);
} else {
return <Navigate to="/admin/signin" replace />;
}

if (!user) {
return <Navigate to="/admin/signin" replace />;
}

if (classId) {
const isAuthorized = classList?.some(
(classItem) => classItem.id === classId
);

if (!isAuthorized) {
return <Navigate to="/class" replace />;
}
}

return (
<Container>
<SideBar />
<Outlet />
</Container>
);
}

const Container = styled.div`
Expand Down
2 changes: 1 addition & 1 deletion src/pages/class/ClassList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useRef, useState } from "react";
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import styled from "styled-components";

Expand Down

0 comments on commit d7dadd6

Please sign in to comment.