Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
final-exam
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nawasan Wisitsingkhon
final-exam
Commits
bc387faf
Commit
bc387faf
authored
Sep 30, 2023
by
Nawasan Wisitsingkhon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
protect admin page from normal user
parent
04c66909
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
7 deletions
+18
-7
_app.js
src/pages/_app.js
+17
-6
_error.js
src/pages/_error.js
+1
-1
No files found.
src/pages/_app.js
View file @
bc387faf
...
...
@@ -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
}
/
>
...
...
src/pages/_error.js
View file @
bc387faf
import
React
from
"react"
;
export
default
function
Error
()
{
return
<
div
>
Error
<
/div>
;
return
<
div
>
Not
found
<
/div>
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment