Commit ccee784a authored by Цой Данил's avatar Цой Данил 💬

Started working with all preporations: added interfaces, filled them, added…

Started working with all preporations: added interfaces, filled them, added empty folders for controllers, services and etc
parent 5b504690
APP_PORT=8000
PG_HOST=localhost
MONGO_CLIENT_URL=mongodb://localhost/TsoyDanilExam12
ENV_SALT=10
SECRET_KEY=key
\ No newline at end of file
export const enum EStatuses {
SUCCESS = 'SUCCESS',
FAILURE = 'FAILURE'
}
\ No newline at end of file
import path from 'path'
export const config = {
port: 8000,
filePath: path.join(__dirname, '../public/uploads')
}
import { Request } from "express";
import { JwtPayload } from "jsonwebtoken";
export default interface IModifiedRequest extends Request {
verifiedData: string | JwtPayload
}
\ No newline at end of file
import { Types } from "mongoose"
import IUser from "./IUser"
export default interface IPhoto {
_id: Types.ObjectId
user: IUser
title: string
photo: string
}
\ No newline at end of file
import IUser from "./IUser"
export default interface IPhotoDto {
user: IUser['_id']
title: string
photo: string
}
\ No newline at end of file
import { EStatuses } from "../enum/EStatuses";
export default interface IResponse<T = null> {
status: EStatuses
result: T
message: string
}
\ No newline at end of file
import { Types } from "mongoose";
export default interface IUser {
_id: Types.ObjectId
username: string
password: string
checkPassword: (password: string) => boolean
}
\ No newline at end of file
import IUser from "./IUser";
export default interface IUserCreateDto {
username: IUser['username']
password: IUser['password']
}
import { Types } from "mongoose";
import IUser from "./IUser";
export default interface IUserGetDto {
_id: Types.ObjectId
username: IUser['username']
token: string
}
\ No newline at end of file
import { NextFunction, Request, Response } from "express";
import { EStatuses } from "../enum/EStatuses";
import IResponse from "../interfaces/IResponse";
import jwt from 'jsonwebtoken'
import IRequestWithTokenData from "../interfaces/IModifiedRequest";
import IModifiedRequest from "../interfaces/IModifiedRequest";
export const auth = (expressReq:Request, res: Response, next: NextFunction) => {
try {
const req = expressReq as IModifiedRequest
if (req.method === 'OPTIONS'){
next()
}
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token){
throw new Error('Token not provided')
}
const verifiedData = jwt.verify(token, process.env.SECRET_KEY as string);
req.verifiedData = verifiedData
next()
} catch(err: unknown) {
const error = err as Error
const response: IResponse<null> = {
status: EStatuses.FAILURE,
result: null,
message: error.message
}
res.status(418).send(response)
}
}
\ No newline at end of file
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