Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
products_lesson
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pavel Mishakov
products_lesson
Commits
ed053dd4
Commit
ed053dd4
authored
Mar 25, 2023
by
Pavel Mishakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add types to request in middleware
parent
f9c35d8a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
4 deletions
+12
-4
user.ts
src/controllers/user.ts
+3
-2
IRequestWithTokenData.ts
src/interfaces/IRequestWithTokenData.ts
+6
-0
auth.ts
src/middlewares/auth.ts
+3
-2
No files found.
src/controllers/user.ts
View file @
ed053dd4
...
...
@@ -5,6 +5,7 @@ import IUser from "../interfaces/IUser"
import
{
EStatuses
}
from
"../enums/EStatuses"
import
IUserGetDto
from
"../interfaces/IUserGetDto"
import
{
auth
}
from
"../middlewares/auth"
import
IRequestWithTokenData
from
"../interfaces/IRequestWithTokenData"
...
...
@@ -39,10 +40,10 @@ export class UserController {
res
.
status
(
200
).
send
(
response
)
}
public
checkToken
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
public
checkToken
=
async
(
expressReq
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
req
=
expressReq
as
IRequestWithTokenData
const
response
:
IResponse
<
IUserGetDto
|
undefined
>
=
{
status
:
EStatuses
.
NOT_OK
,
//@ts-ignore
result
:
req
.
dataFromToken
as
IUserGetDto
,
message
:
'Token is ok'
}
...
...
src/interfaces/IRequestWithTokenData.ts
0 → 100644
View file @
ed053dd4
import
{
Request
}
from
"express"
;
import
{
JwtPayload
}
from
"jsonwebtoken"
;
export
default
interface
IRequestWithTokenData
extends
Request
{
dataFromToken
:
string
|
JwtPayload
}
\ No newline at end of file
src/middlewares/auth.ts
View file @
ed053dd4
...
...
@@ -2,15 +2,16 @@ import { NextFunction, Request, Response } from "express";
import
{
EStatuses
}
from
"../enums/EStatuses"
;
import
IResponse
from
"../interfaces/IResponse"
;
import
jwt
from
'jsonwebtoken'
import
IRequestWithTokenData
from
"../interfaces/IRequestWithTokenData"
;
export
const
auth
=
(
req
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
export
const
auth
=
(
expressReq
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
const
req
=
expressReq
as
IRequestWithTokenData
if
(
req
.
method
===
'OPTIONS'
)
{
next
()
}
try
{
const
data
=
jwt
.
verify
(
req
.
get
(
'Authorization'
)
||
''
,
process
.
env
.
SECRET_KEY
||
''
)
if
(
data
)
{
//@ts-ignore
req
.
dataFromToken
=
data
next
()
}
else
{
...
...
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