Skip to content

Commit

Permalink
fix: error handler text/html output
Browse files Browse the repository at this point in the history
  • Loading branch information
saisilinus committed Apr 5, 2022
1 parent 58597b4 commit 1c16df1
Show file tree
Hide file tree
Showing 17 changed files with 765 additions and 59 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"prettier"
],
"devDependencies": {
"@faker-js/faker": "^6.0.0-alpha.7",
"@jest/globals": "^27.5.1",
"@types/bcryptjs": "^2.4.2",
"@types/compression": "^1.7.2",
Expand All @@ -60,6 +59,7 @@
"@types/node": "^16.11.12",
"@types/nodemailer": "^6.4.4",
"@types/passport-jwt": "^3.0.6",
"@types/supertest": "^2.0.11",
"@types/swagger-jsdoc": "^6.0.1",
"@types/swagger-ui-express": "^4.1.3",
"@types/validator": "^13.7.0",
Expand Down Expand Up @@ -110,6 +110,7 @@
"swagger-ui-express": "^4.2.0",
"validator": "^13.7.0",
"winston": "^3.3.3",
"xss-clean": "^0.1.1"
"xss-clean": "^0.1.1",
"@faker-js/faker": "^6.0.0-alpha.7"
}
}
13 changes: 3 additions & 10 deletions src/components/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import httpStatus from 'http-status';
import { Request, Response } from 'express';
import catchAsync from '../../utils/catchAsync';
import { createUser } from '../user/user.service';
import { registerUser } from '../user/user.service';
import { generateAuthTokens, generateResetPasswordToken, generateVerifyEmailToken } from '../token/token.service';
import { loginUserWithEmailAndPassword, logout, refreshAuth, resetPassword, verifyEmail } from './auth.service';
import {
sendAccountCreated,
sendResetPasswordEmail,
sendSuccessfulRegistration,
sendVerificationEmail,
} from '../email/email.service';
import { sendAccountCreated, sendResetPasswordEmail, sendVerificationEmail } from '../email/email.service';
import config from '../../config/config';
import { AccessAndRefreshTokens } from '../token/token.interfaces';

Expand All @@ -19,10 +14,8 @@ export const sendTokens = (res: Response, tokens: AccessAndRefreshTokens) => {
};

export const registerController = catchAsync(async (req: Request, res: Response) => {
const user = await createUser(req.body);
const user = await registerUser(req.body);
const tokens = await generateAuthTokens(user);
const verifyEmailToken = await generateVerifyEmailToken(user);
await sendSuccessfulRegistration(user.email, verifyEmailToken, user.name);
res.status(httpStatus.CREATED).send({ user, tokens });
});

Expand Down
4 changes: 2 additions & 2 deletions src/components/auth/auth.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const verifyCallback =
resolve();
};

const auth =
const authMiddleware =
(...requiredRights: string[]) =>
async (req: Request, res: Response, next: NextFunction) =>
new Promise<void>((resolve, reject) => {
Expand All @@ -33,4 +33,4 @@ const auth =
.then(() => next())
.catch((err) => next(err));

export default auth;
export default authMiddleware;
7 changes: 4 additions & 3 deletions src/components/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import httpStatus from 'http-status';
import mongoose from 'mongoose';
import Token from '../token/token.model';
import ApiError from '../errors/ApiError';
import tokenTypes from '../token/token.types';
Expand Down Expand Up @@ -42,7 +43,7 @@ export const logout = async (refreshToken: string): Promise<void> => {
export const refreshAuth = async (refreshToken: string): Promise<AccessAndRefreshTokens> => {
try {
const refreshTokenDoc = await verifyToken(refreshToken, tokenTypes.REFRESH);
const user = await getUserById(refreshTokenDoc.user);
const user = await getUserById(new mongoose.Types.ObjectId(refreshTokenDoc.user));
if (!user) {
throw new Error();
}
Expand All @@ -62,7 +63,7 @@ export const refreshAuth = async (refreshToken: string): Promise<AccessAndRefres
export const resetPassword = async (resetPasswordToken: any, newPassword: string): Promise<void> => {
try {
const resetPasswordTokenDoc = await verifyToken(resetPasswordToken, tokenTypes.RESET_PASSWORD);
const user = await getUserById(resetPasswordTokenDoc.user);
const user = await getUserById(new mongoose.Types.ObjectId(resetPasswordTokenDoc.user));
if (!user) {
throw new Error();
}
Expand All @@ -81,7 +82,7 @@ export const resetPassword = async (resetPasswordToken: any, newPassword: string
export const verifyEmail = async (verifyEmailToken: any): Promise<IUserDoc | null> => {
try {
const verifyEmailTokenDoc = await verifyToken(verifyEmailToken, tokenTypes.VERIFY_EMAIL);
const user = await getUserById(verifyEmailTokenDoc.user);
const user = await getUserById(new mongoose.Types.ObjectId(verifyEmailTokenDoc.user));
if (!user) {
throw new Error();
}
Expand Down

0 comments on commit 1c16df1

Please sign in to comment.