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"; ...@@ -8,19 +8,22 @@ import UserLayout from "@/components/layout/UserLayout";
import userCookie from "@/components/lib/userCookie"; import userCookie from "@/components/lib/userCookie";
import AdminLayout from "@/components/layout/AdminLayout"; import AdminLayout from "@/components/layout/AdminLayout";
import { usePathname } from "next/navigation"; import { usePathname } from "next/navigation";
import Error from "./_error";
// axios.defaults.baseURL = "" // axios.defaults.baseURL = ""
export const UserContext = createContext(null); export const UserContext = createContext(null);
export default function App({ Component, pageProps }) { export default function App({ Component, pageProps }) {
const [user, setUser] = useState({}); const [user, setUser] = useState({});
const pathname = usePathname() const pathname = usePathname();
useEffect(() => { useEffect(() => {
const usrcookie = new userCookie(); const usrcookie = new userCookie();
let token = usrcookie.token.split(".")[1]; let token = usrcookie.token.split(".")[1];
if (token) { 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; user_info.token = usrcookie.token;
setUser(user_info); setUser(user_info);
} }
...@@ -28,10 +31,18 @@ export default function App({ Component, pageProps }) { ...@@ -28,10 +31,18 @@ export default function App({ Component, pageProps }) {
return ( return (
<UserContext.Provider value={{ value: user, set: setUser }}> <UserContext.Provider value={{ value: user, set: setUser }}>
{(user.rank && pathname.split("/")[1] === 'admin') ? ( {pathname.split("/")[1] === "admin" ? (
<AdminLayout> <>
<Component {...pageProps} /> {user.rank ? (
</AdminLayout> <AdminLayout>
<Component {...pageProps} />
</AdminLayout>
) : (
<UserLayout>
<Error />
</UserLayout>
)}
</>
) : ( ) : (
<UserLayout> <UserLayout>
<Component {...pageProps} /> <Component {...pageProps} />
......
import React from "react"; import React from "react";
export default function Error() { 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