Commit bc387faf authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

protect admin page from normal user

parent 04c66909
......@@ -8,19 +8,22 @@ import UserLayout from "@/components/layout/UserLayout";
import userCookie from "@/components/lib/userCookie";
import AdminLayout from "@/components/layout/AdminLayout";
import { usePathname } from "next/navigation";
import Error from "./_error";
// axios.defaults.baseURL = ""
export const UserContext = createContext(null);
export default function App({ Component, pageProps }) {
const [user, setUser] = useState({});
const pathname = usePathname()
const pathname = usePathname();
useEffect(() => {
const usrcookie = new userCookie();
let token = usrcookie.token.split(".")[1];
if (token) {
let user_info = JSON.parse(Buffer.from(token, "base64").toString("utf-8"));
let user_info = JSON.parse(
Buffer.from(token, "base64").toString("utf-8")
);
user_info.token = usrcookie.token;
setUser(user_info);
}
......@@ -28,10 +31,18 @@ export default function App({ Component, pageProps }) {
return (
<UserContext.Provider value={{ value: user, set: setUser }}>
{(user.rank && pathname.split("/")[1] === 'admin') ? (
<AdminLayout>
<Component {...pageProps} />
</AdminLayout>
{pathname.split("/")[1] === "admin" ? (
<>
{user.rank ? (
<AdminLayout>
<Component {...pageProps} />
</AdminLayout>
) : (
<UserLayout>
<Error />
</UserLayout>
)}
</>
) : (
<UserLayout>
<Component {...pageProps} />
......
import React from "react";
export default function Error() {
return <div>Error</div>;
return <div>Not found</div>;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment