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
960b511c
Commit
960b511c
authored
Jul 07, 2022
by
Yoon, Daeki
😅
Browse files
백엔드 auth/authenticate 함수 추가
parent
5d3b9c6f
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/controllers/auth.controller.ts
View file @
960b511c
...
@@ -9,7 +9,34 @@ import { jwtCofig, envConfig, cookieConfig } from "../config";
...
@@ -9,7 +9,34 @@ import { jwtCofig, envConfig, cookieConfig } from "../config";
export
interface
TypedRequestAuth
<
T
>
extends
Request
{
export
interface
TypedRequestAuth
<
T
>
extends
Request
{
auth
:
T
;
auth
:
T
;
user
:
any
;
}
}
/**
* 함수를 호출하기 전에 req에 user 정보를 지정해야 합니다.
*/
export
const
authenticate
=
asyncWrap
(
async
(
reqExp
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
try
{
const
req
=
reqExp
as
TypedRequestAuth
<
{
userId
:
string
}
>
;
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
||
"
권한 없음
"
);
}
}
);
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
}
`
);
...
@@ -84,12 +111,12 @@ export const signup = asyncWrap(async (req, res) => {
...
@@ -84,12 +111,12 @@ export const signup = asyncWrap(async (req, res) => {
if
(
userExist
)
{
if
(
userExist
)
{
return
res
.
status
(
422
).
send
(
`
${
email
}
사용자가 이미 존재합니다`
);
return
res
.
status
(
422
).
send
(
`
${
email
}
사용자가 이미 존재합니다`
);
}
}
// 3) 비밀번호 암호화
// 3) 비밀번호 암호화
는 useDb.createUser에서 처리
const
hash
=
await
bcrypt
.
hash
(
password
,
10
);
//
const hash = await bcrypt.hash(password, 10);
// 4) 새로운 사용자 만들기
// 4) 새로운 사용자 만들기
const
newUser
=
await
userDb
.
createUser
({
const
newUser
=
await
userDb
.
createUser
({
email
,
email
,
password
:
hash
,
password
,
});
});
// 5) 사용자 반환
// 5) 사용자 반환
res
.
json
(
newUser
);
res
.
json
(
newUser
);
...
...
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