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
04c66909
Commit
04c66909
authored
Sep 30, 2023
by
Nawasan Wisitsingkhon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
settup middleware and fix middleware
parent
da199b03
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
27 deletions
+32
-27
middleware.js
app/routes/middleware.js
+23
-0
web.js
app/routes/web.js
+3
-3
jwttoken.js
src/components/lib/jwttoken.js
+0
-21
member.js
src/pages/admin/member.js
+6
-3
No files found.
app/routes/middleware.js
0 → 100644
View file @
04c66909
import
{
Request
,
Response
}
from
"express"
;
import
{
JwtCheck
}
from
"@/components/lib/jwttoken"
;
/**
*
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
export
const
JwtAdminMiddleware
=
(
req
,
res
,
next
)
=>
{
try
{
const
{
token
}
=
req
.
headers
;
if
(
!
token
)
throw
403
;
let
verify
=
JwtCheck
(
token
);
console
.
log
(
verify
.
data
);
if
(
!
verify
.
data
.
rank
)
throw
403
;
next
();
}
catch
(
err
)
{
return
res
.
status
(
403
).
json
({
status
:
403
,
message
:
"access denined"
,
});
}
};
app/routes/web.js
View file @
04c66909
...
@@ -2,12 +2,12 @@ import express from "express";
...
@@ -2,12 +2,12 @@ import express from "express";
import
WelcomeController
from
"../controllers/WelcomeController"
;
import
WelcomeController
from
"../controllers/WelcomeController"
;
import
UserController
from
"../controllers/UserController"
;
import
UserController
from
"../controllers/UserController"
;
import
adminRouter
from
"./admin"
;
import
adminRouter
from
"./admin"
;
import
{
JwtAdminMiddleware
}
from
"
@/components/lib/jwttoken
"
;
import
{
JwtAdminMiddleware
}
from
"
./middleware
"
;
const
route
=
express
.
Router
();
const
route
=
express
.
Router
();
route
.
use
(
'/admin/'
,
JwtAdminMiddleware
,
adminRouter
);
route
.
use
(
"/admin/"
,
JwtAdminMiddleware
,
adminRouter
);
route
.
get
(
"/"
,
WelcomeController
.
index
);
route
.
get
(
"/"
,
WelcomeController
.
index
);
route
.
get
(
'/user'
,
UserController
.
index
);
route
.
get
(
"/user"
,
UserController
.
index
);
route
.
post
(
"/user/auth"
,
UserController
.
login
);
route
.
post
(
"/user/auth"
,
UserController
.
login
);
route
.
post
(
"/user"
,
UserController
.
create
);
route
.
post
(
"/user"
,
UserController
.
create
);
...
...
src/components/lib/jwttoken.js
View file @
04c66909
import
jwt
from
"jsonwebtoken"
;
import
jwt
from
"jsonwebtoken"
;
import
{
Request
,
Response
,
NextFunction
}
from
"express"
;
/**
/**
*
*
* @param {{id: number, name: string, email: string, phone: string, photo: string, username: string }} data
* @param {{id: number, name: string, email: string, phone: string, photo: string, username: string }} data
...
@@ -15,23 +14,3 @@ export const JwtCheck = (token) => {
...
@@ -15,23 +14,3 @@ export const JwtCheck = (token) => {
return
{
status
:
true
,
data
:
decoded
};
return
{
status
:
true
,
data
:
decoded
};
});
});
};
};
/**
*
* @param {Request} req
* @param {Response} res
* @param {NextFunction} next
*/
export
const
JwtAdminMiddleware
=
(
req
,
res
,
next
)
=>
{
try
{
const
{
token
}
=
req
.
headers
;
if
(
!
token
)
throw
403
;
let
verify
=
JwtCheck
(
token
);
if
(
!
verify
.
status
)
throw
403
;
next
();
}
catch
(
err
)
{
return
res
.
status
(
403
).
json
({
status
:
403
,
message
:
"access denined"
,
});
}
};
src/pages/admin/member.js
View file @
04c66909
...
@@ -11,6 +11,7 @@ export default function Member() {
...
@@ -11,6 +11,7 @@ export default function Member() {
let
response
=
await
axios
.
get
(
"/api/admin/user"
,
{
let
response
=
await
axios
.
get
(
"/api/admin/user"
,
{
headers
:
{
token
:
user
.
value
.
token
},
headers
:
{
token
:
user
.
value
.
token
},
});
});
console
.
log
(
response
.
data
);
setUserAll
(
response
.
data
);
setUserAll
(
response
.
data
);
}
catch
(
err
)
{}
}
catch
(
err
)
{}
}
}
...
@@ -19,9 +20,11 @@ export default function Member() {
...
@@ -19,9 +20,11 @@ export default function Member() {
},
[]);
},
[]);
return
(
return
(
<
div
>
<
div
>
{
userAll
.
map
((
usr
,
idx
)
=>
(
{
userAll
.
map
((
usr
,
idx
)
=>
(
<
div
>
{
usr
.
name
}@{
usr
.
username
}
{
usr
.
email
}
<
/div
>
<
div
key
=
{
idx
}
>
))}
{
usr
.
name
}@{
usr
.
username
}
{
usr
.
email
}
<
/div
>
))}
<
button
onClick
=
{
fetchApi
}
>
submit
<
/button
>
<
button
onClick
=
{
fetchApi
}
>
submit
<
/button
>
<
/div
>
<
/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