Skip to content

Commit

Permalink
feat: add routes + cookieAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
saisilinus committed Dec 17, 2021
1 parent 29793dc commit 58cecb3
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/Auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const loginController = catchAsync(async (req: Request, res: Response) =>

export const logoutController = catchAsync(async (req: Request, res: Response) => {
await logout(req.cookies.refreshToken);
res.clearCookie('accessToken');
res.clearCookie('refreshToken');
res.status(httpStatus.NO_CONTENT).send();
});

Expand All @@ -54,6 +56,7 @@ export const forgotPasswordController = catchAsync(async (req: Request, res: Res

export const resetPasswordController = catchAsync(async (req: Request, res: Response) => {
await resetPassword(req.cookies.resetPasswordToken, req.body.password);
res.clearCookie('resetPasswordToken');
res.status(httpStatus.NO_CONTENT).send();
});

Expand All @@ -68,5 +71,6 @@ export const verifyEmailController = catchAsync(async (req: Request, res: Respon
if (user) {
await sendAccountCreated(user.email, user.name);
}
res.clearCookie('verifyEmailToken');
res.status(httpStatus.NO_CONTENT).send();
});
2 changes: 1 addition & 1 deletion src/Auth/auth.route.v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export default router;
* description: An email will be sent to verify email.
* tags: [Auth]
* security:
* - bearerAuth: []
* - cookieAuth: []
* responses:
* "204":
* description: No content
Expand Down
10 changes: 5 additions & 5 deletions src/Users/user.route.v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default router;
* description: Only admins can create other users.
* tags: [Users]
* security:
* - bearerAuth: []
* - cookieAuth: []
* requestBody:
* required: true
* content:
Expand Down Expand Up @@ -97,7 +97,7 @@ export default router;
* description: Only admins can retrieve all users.
* tags: [Users]
* security:
* - bearerAuth: []
* - cookieAuth: []
* parameters:
* - in: query
* name: name
Expand Down Expand Up @@ -166,7 +166,7 @@ export default router;
* description: Logged in users can fetch only their own user information. Only admins can fetch other users.
* tags: [Users]
* security:
* - bearerAuth: []
* - cookieAuth: []
* parameters:
* - in: path
* name: id
Expand All @@ -193,7 +193,7 @@ export default router;
* description: Logged in users can only update their own information. Only admins can update other users.
* tags: [Users]
* security:
* - bearerAuth: []
* - cookieAuth: []
* parameters:
* - in: path
* name: id
Expand Down Expand Up @@ -244,7 +244,7 @@ export default router;
* description: Logged in users can delete only themselves. Only admins can delete other users.
* tags: [Users]
* security:
* - bearerAuth: []
* - cookieAuth: []
* parameters:
* - in: path
* name: id
Expand Down
4 changes: 4 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import jwtStrategy from './config/passport';
import authLimiter from './middlewares/rateLimiter';
import ApiError from './utils/ApiError';
import { errorConverter, errorHandler } from './middlewares/error';
import routes from './routes';

const app: Express = express();

Expand Down Expand Up @@ -53,6 +54,9 @@ if (config.env === 'production') {
app.use('/v1/auth', authLimiter);
}

// v1 api routes
app.use('/v1', routes);

// send back a 404 error for any unknown api request
app.use((req, res, next) => {
next(new ApiError(httpStatus.NOT_FOUND, 'Not found'));
Expand Down
44 changes: 44 additions & 0 deletions src/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import express, { Router } from 'express';
import authRoute from './Auth/auth.route.v1';
import docsRoute from './swagger/swagger.route';
import userRoute from './Users/user.route.v1';
import config from './config/config';

const router = express.Router();

interface IRoute {
path: string;
route: Router;
}

const defaultIRoute: IRoute[] = [
{
path: '/auth',
route: authRoute,
},
{
path: '/users',
route: userRoute,
},
];

const devIRoute: IRoute[] = [
// IRoute available only in development mode
{
path: '/docs',
route: docsRoute,
},
];

defaultIRoute.forEach((route) => {
router.use(route.path, route.route);
});

/* istanbul ignore next */
if (config.env === 'development') {
devIRoute.forEach((route) => {
router.use(route.path, route.route);
});
}

export default router;
6 changes: 5 additions & 1 deletion src/swagger/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ components:
bearerAuth:
type: http
scheme: bearer
bearerFormat: JWT
bearerFormat: JWT
cookieAuth:
type: ApiKey
in: cookie
name: accessToken
7 changes: 2 additions & 5 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
"typeRoots": [
"@types",
"./node_modules/@types",
], /* Specify multiple folders that act like `./node_modules/@types`. */
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
"resolveJsonModule": true, /* Enable importing .json files */
Expand Down Expand Up @@ -64,7 +61,7 @@
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
"noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
Expand Down

0 comments on commit 58cecb3

Please sign in to comment.