Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
survey
Commits
1cc54e56
Commit
1cc54e56
authored
Jul 08, 2022
by
Yoon, Daeki
😅
Browse files
지정된 역할 이상의 권한 확인 기능 추가
parent
ff8805a6
Changes
1
Show whitespace changes
Inline
Side-by-side
src/controllers/auth.controller.ts
View file @
1cc54e56
...
@@ -4,7 +4,7 @@ import jwt, { JwtPayload } from "jsonwebtoken";
...
@@ -4,7 +4,7 @@ import jwt, { JwtPayload } from "jsonwebtoken";
import
isLength
from
"
validator/lib/isLength
"
;
import
isLength
from
"
validator/lib/isLength
"
;
import
isEmail
from
"
validator/lib/isEmail
"
;
import
isEmail
from
"
validator/lib/isEmail
"
;
import
{
asyncWrap
}
from
"
../helpers
"
;
import
{
asyncWrap
}
from
"
../helpers
"
;
import
{
userDb
}
from
"
../db
"
;
import
{
roleDb
,
userDb
}
from
"
../db
"
;
import
{
jwtCofig
,
envConfig
,
cookieConfig
}
from
"
../config
"
;
import
{
jwtCofig
,
envConfig
,
cookieConfig
}
from
"
../config
"
;
export
interface
TypedRequestAuth
<
T
>
extends
Request
{
export
interface
TypedRequestAuth
<
T
>
extends
Request
{
...
@@ -37,6 +37,30 @@ export const authenticate = asyncWrap(
...
@@ -37,6 +37,30 @@ export const authenticate = asyncWrap(
}
}
);
);
/**
* 지정된 역할 이상으로 권한이 있는지를 판단하는 미들웨어를 반환합니다.
* @param roleName 역할 문자열
* @returns 미들웨어
*/
export
const
hasRole
=
(
roleName
:
string
)
=>
{
// roleName 이상으로 허락하는 것
return
async
(
reqExp
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
const
req
=
reqExp
as
TypedRequestAuth
<
{
userId
:
string
}
>
;
if
(
!
req
.
auth
)
{
return
res
.
status
(
401
).
send
(
"
로그인이 필요합니다
"
);
}
const
{
userId
}
=
req
.
auth
;
const
userRole
=
await
roleDb
.
findRoleByUserId
(
userId
);
const
maxRole
=
await
roleDb
.
findRoleByName
(
roleName
);
if
(
maxRole
&&
Number
(
maxRole
.
priority
)
>=
Number
(
userRole
.
priority
))
{
return
next
();
}
else
{
return
res
.
status
(
401
).
send
(
"
이용 권한이 없습니다
"
);
}
};
};
export
const
login
=
asyncWrap
(
async
(
req
,
res
)
=>
{
export
const
login
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
{
email
,
password
}
=
req
.
body
;
const
{
email
,
password
}
=
req
.
body
;
console
.
log
(
`email:
${
email
}
, password:
${
password
}
`
);
console
.
log
(
`email:
${
email
}
, password:
${
password
}
`
);
...
...
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