Commit ed053dd4 authored by Pavel Mishakov's avatar Pavel Mishakov

add types to request in middleware

parent f9c35d8a
...@@ -5,6 +5,7 @@ import IUser from "../interfaces/IUser" ...@@ -5,6 +5,7 @@ import IUser from "../interfaces/IUser"
import { EStatuses } from "../enums/EStatuses" import { EStatuses } from "../enums/EStatuses"
import IUserGetDto from "../interfaces/IUserGetDto" import IUserGetDto from "../interfaces/IUserGetDto"
import { auth } from "../middlewares/auth" import { auth } from "../middlewares/auth"
import IRequestWithTokenData from "../interfaces/IRequestWithTokenData"
...@@ -39,10 +40,10 @@ export class UserController { ...@@ -39,10 +40,10 @@ export class UserController {
res.status(200).send(response) 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> = { const response: IResponse<IUserGetDto | undefined> = {
status: EStatuses.NOT_OK, status: EStatuses.NOT_OK,
//@ts-ignore
result: req.dataFromToken as IUserGetDto, result: req.dataFromToken as IUserGetDto,
message: 'Token is ok' message: 'Token is ok'
} }
......
import { Request } from "express";
import { JwtPayload } from "jsonwebtoken";
export default interface IRequestWithTokenData extends Request {
dataFromToken: string | JwtPayload
}
\ No newline at end of file
...@@ -2,15 +2,16 @@ import { NextFunction, Request, Response } from "express"; ...@@ -2,15 +2,16 @@ import { NextFunction, Request, Response } from "express";
import { EStatuses } from "../enums/EStatuses"; import { EStatuses } from "../enums/EStatuses";
import IResponse from "../interfaces/IResponse"; import IResponse from "../interfaces/IResponse";
import jwt from 'jsonwebtoken' 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') { if (req.method === 'OPTIONS') {
next() next()
} }
try { try {
const data = jwt.verify(req.get('Authorization') || '', process.env.SECRET_KEY || '') const data = jwt.verify(req.get('Authorization') || '', process.env.SECRET_KEY || '')
if (data) { if (data) {
//@ts-ignore
req.dataFromToken = data req.dataFromToken = data
next() next()
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment