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
travel
Commits
9d3f66f3
Commit
9d3f66f3
authored
Jul 27, 2022
by
Yoon, Daeki
😅
Browse files
서버 authenticate 추가
parent
c016fafd
Changes
1
Show whitespace changes
Inline
Side-by-side
src/controllers/auth.controller.ts
View file @
9d3f66f3
...
@@ -6,11 +6,37 @@ import isEmail from "validator/lib/isEmail";
...
@@ -6,11 +6,37 @@ import isEmail from "validator/lib/isEmail";
import
{
asyncWrap
}
from
"
../helpers
"
;
import
{
asyncWrap
}
from
"
../helpers
"
;
import
{
roleDb
,
userDb
}
from
"
../db
"
;
import
{
roleDb
,
userDb
}
from
"
../db
"
;
import
{
jwtCofig
,
envConfig
,
cookieConfig
}
from
"
../config
"
;
import
{
jwtCofig
,
envConfig
,
cookieConfig
}
from
"
../config
"
;
import
{
TypedRequest
}
from
"
../types
"
;
export
interface
TypedRequestAuth
<
T
>
extends
Request
{
export
interface
TypedRequestAuth
<
T
>
extends
Request
{
auth
:
T
;
auth
:
T
;
}
}
/**
* 함수를 호출하기 전에 req에 user 정보를 지정해야 합니다.
*/
export
const
authenticate
=
asyncWrap
(
async
(
reqExp
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
try
{
const
req
=
reqExp
as
TypedRequest
;
if
(
req
.
auth
)
{
const
{
userId
}
=
req
.
auth
;
const
user
=
req
.
user
;
if
(
user
&&
user
.
id
===
userId
)
{
return
next
();
}
else
{
throw
new
Error
(
"
권한이 필요합니다
"
);
}
}
else
{
throw
new
Error
(
"
로그인이 필요합니다
"
);
}
}
catch
(
error
:
any
)
{
console
.
log
(
error
);
return
res
.
status
(
401
).
send
(
error
.
message
||
"
권한 없음
"
);
}
}
);
/**
/**
* 지정된 역할 이상으로 권한이 있는지를 판단하는 미들웨어를 반환합니다.
* 지정된 역할 이상으로 권한이 있는지를 판단하는 미들웨어를 반환합니다.
* @param roleName 역할 문자열
* @param roleName 역할 문자열
...
...
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