Skip to content

Commit

Permalink
fix-krishnaacharyaa#3: fixed some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsidhar-914 committed Jun 8, 2024
1 parent 70a976a commit 4348ebe
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 33 deletions.
5 changes: 3 additions & 2 deletions backend/controllers/auth-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ApiError } from '../utils/api-error';
import { ApiResponse } from '../utils/api-response';
import { asyncHandler } from '../utils/async-handler';
import { Response, Request } from 'express';
import { RequestWithUserRole } from '../types/request-User-Type';
import { RequestWithUserRole } from '../types/request-user-type';

//REGULAR EMAIL PASSWORD STRATEGY
//1.Sign Up
Expand Down Expand Up @@ -60,7 +60,7 @@ export const signUpWithEmail = asyncHandler(async (req: Request, res: Response)
user.refreshToken = refreshToken;

await user.save();
// user.password = undefined;
user.password = undefined;

res
.status(HTTP_STATUS.OK)
Expand Down Expand Up @@ -103,6 +103,7 @@ export const signInWithEmailOrUsername = asyncHandler(async (req, res) => {
const refreshToken = await user.generateRefreshToken();

user.refreshToken = refreshToken;
user.password = undefined;
await user.save();

res
Expand Down
4 changes: 2 additions & 2 deletions backend/controllers/posts-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Post from '../models/post';
import User from '../models/user';
import { deleteDataFromCache, storeDataInCache } from '../utils/cache-posts';
import { HTTP_STATUS, REDIS_KEYS, RESPONSE_MESSAGES, validCategories } from '../utils/constants';
import { RequestWithUserRole } from '../types/request-User-Type';
import { RequestWithUserRole } from '../types/request-user-type';

export const createPostHandler = async (req: RequestWithUserRole, res: Response) => {
try {
Expand Down Expand Up @@ -196,7 +196,7 @@ export const deletePostByIdHandler = async (req: Request, res: Response) => {
export const getRelatedPostsByCategories = async (req: Request, res: Response) => {
const { categories } = req.query;
if (!categories) {
return res.status(HTTP_STATUS.NOT_FOUND).json({ message: 'Categories not Found' });
return res.status(HTTP_STATUS.NOT_FOUND).json(RESPONSE_MESSAGES.POSTS);
}

try {
Expand Down
4 changes: 2 additions & 2 deletions backend/middlewares/auth-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JWT_SECRET } from '../config/utils';
import { ApiError } from '../utils/api-error';
import { HTTP_STATUS, RESPONSE_MESSAGES } from '../utils/constants';
import jwt from 'jsonwebtoken';
import { RequestWithUserRole } from '../types/request-User-Type';
import { RequestWithUserRole } from '../types/request-user-type';

export const authMiddleware = async (
req: RequestWithUserRole,
Expand All @@ -16,7 +16,7 @@ export const authMiddleware = async (
}

if (token) {
jwt.verify(token, JWT_SECRET!, (error: any, payload: any) => {
await jwt.verify(token, JWT_SECRET!, (error: any, payload: any) => {
if (error) {
return new ApiError(HTTP_STATUS.FORBIDDEN, RESPONSE_MESSAGES.USERS.INVALID_TOKEN);
}
Expand Down
2 changes: 1 addition & 1 deletion backend/middlewares/error-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextFunction, Request, Response } from 'express';
import { HTTP_STATUS, RESPONSE_MESSAGES } from '../utils/constants';

export type ErrorMiddlewareType = {
type ErrorMiddlewareType = {
stack: string;
status: number;
message: string;
Expand Down
2 changes: 1 addition & 1 deletion backend/middlewares/post-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextFunction, Response } from 'express';
import Post from '../models/post';
import { HTTP_STATUS, RESPONSE_MESSAGES } from '../utils/constants';
import type { RequestWithUserRole } from '../types/request-User-Type';
import type { RequestWithUserRole } from '../types/request-user-type';

export const isAuthorMiddleware = async (
req: RequestWithUserRole,
Expand Down
6 changes: 3 additions & 3 deletions backend/models/post.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Model, Schema, model } from 'mongoose';
import { IPost } from '../types/post-Type';
import { PostType } from '../types/post-type';

const postSchema = new Schema({
authorfName: String,
authorName: String,
title: String,
imageLink: String,
categories: [String],
Expand All @@ -16,6 +16,6 @@ const postSchema = new Schema({
},
});

const Post: Model<IPost> = model<IPost>('Post', postSchema);
const Post: Model<PostType> = model<PostType>('Post', postSchema);

export default Post;
8 changes: 4 additions & 4 deletions backend/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import JWT from 'jsonwebtoken';
import bcrypt from 'bcryptjs';
import crypto from 'crypto';
import { ACCESS_TOKEN_EXPIRES_IN, JWT_SECRET, REFRESH_TOKEN_EXPIRES_IN } from '../config/utils';
import { IUser } from '../types/user-Type';
import { UserType } from '../types/user-type';

const userSchema = new Schema<IUser>(
const userSchema = new Schema<UserType>(
{
userName: {
type: String,
Expand Down Expand Up @@ -68,7 +68,7 @@ userSchema.pre('save', async function (next) {
if (!this.isModified('password')) {
return next();
}
this.password = await bcrypt.hash(this.password, 10);
this.password = await bcrypt.hash(this.password!, 10);
});

userSchema.methods = {
Expand Down Expand Up @@ -111,6 +111,6 @@ userSchema.methods = {
},
};

const User = model<IUser>('User', userSchema);
const User = model<UserType>('User', userSchema);

export default User;
2 changes: 1 addition & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES2016",
"target": "ES2015",
"module": "CommonJS",
"outDir": "./dist",
"esModuleInterop": true,
Expand Down
8 changes: 3 additions & 5 deletions backend/types/express/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { IUser } from '../models/user';

declare global {
namespace Express {
interface Request {
export interface Request {
user?: {
email: string;
role: string;
_id: string;
userName: string;
role: string;
email: string;
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions backend/types/post-Type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IUser } from './user-Type';
import { UserType } from './user-type';

export interface IPost extends Document {
export type PostType = {
_id?: string;
authorName: string;
title: string;
Expand All @@ -9,5 +9,5 @@ export interface IPost extends Document {
description: string;
isFeaturedPost: boolean;
timeOfPost: Date;
authorId: IUser['_id'];
}
authorId: UserType['_id'];
};
8 changes: 4 additions & 4 deletions backend/types/request-User-Type.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Request } from 'express';

interface RequestUserType {
type RequestUserType = {
_id: string;
role: string;
userName: string;
email: string;
}
};

export interface RequestWithUserRole extends Request {
export type RequestWithUserRole = Request & {
user?: RequestUserType;
}
};
6 changes: 3 additions & 3 deletions backend/types/user-Type.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import mongoose from 'mongoose';

export interface IUser extends Document {
export type UserType = {
_id: string;
userName: string;
fullName: string;
email: string;
password: string;
password: string | undefined;
avatar?: string;
role: 'USER' | 'ADMIN';
posts: mongoose.Types.ObjectId[];
Expand All @@ -15,4 +15,4 @@ export interface IUser extends Document {
generateAccessToken(): Promise<string>;
generateRefreshToken(): Promise<string>;
isPasswordCorrect(password: string): Promise<boolean>;
}
};
2 changes: 1 addition & 1 deletion backend/utils/api-error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RESPONSE_MESSAGES } from './constants';
import { ApiErrorType } from '../types/api-Error-Type';
import { ApiErrorType } from '../types/api-error-type';

class ApiError extends Error implements ApiErrorType {
status: number;
Expand Down

0 comments on commit 4348ebe

Please sign in to comment.