Skip to content

Commit 6a39925

Browse files
committed
Updates to common package
1 parent bd0fe25 commit 6a39925

9 files changed

Lines changed: 65 additions & 2 deletions

File tree

examples_new/microservices/common/.gitignore

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export declare enum Events {
2+
USER_CREATED = "USER_CREATED",
3+
ITEM_CREATED = "ITEM_CREATED",
4+
ORDER_CREATED = "ORDER_CREATED"
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Events = void 0;
4+
var Events;
5+
(function (Events) {
6+
Events["USER_CREATED"] = "USER_CREATED";
7+
Events["ITEM_CREATED"] = "ITEM_CREATED";
8+
Events["ORDER_CREATED"] = "ORDER_CREATED";
9+
})(Events = exports.Events || (exports.Events = {}));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { NextFunction, Request, Response } from 'express';
2+
export interface CurrentUserRequest extends Request {
3+
currentUser?: string;
4+
}
5+
export declare const currentUser: (req: CurrentUserRequest, res: Response, next: NextFunction) => void;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.currentUser = void 0;
7+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8+
//middleware to check if there is a cookie
9+
// if there is a cookie, we decode jwt and attach the current user id
10+
const currentUser = (req, res, next) => {
11+
const { token } = req.cookies;
12+
if (!token)
13+
return next();
14+
try {
15+
const payload = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY);
16+
req.currentUser = payload.userId;
17+
return next();
18+
}
19+
catch (err) {
20+
throw new Error('🎃Internal Error');
21+
}
22+
};
23+
exports.currentUser = currentUser;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { CurrentUserRequest } from './currentUser';
2+
import { NextFunction, Response } from 'express';
3+
export declare const requireAuth: (req: CurrentUserRequest, res: Response, next: NextFunction) => void;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.requireAuth = void 0;
4+
const not_authorized_error_1 = require("../errors/not-authorized-error");
5+
// take in req, res and next as arguments - include their types from express package
6+
const requireAuth = (req, res, next) => {
7+
// check if req.currentUser exists
8+
// if it does, move onto to the next middleware
9+
if (req.currentUser)
10+
return next();
11+
// if not throw a not authorized error
12+
throw new not_authorized_error_1.NotAuthorizedError();
13+
};
14+
exports.requireAuth = requireAuth;

examples_new/microservices/common/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
export * from './events/events';
2+
13
export * from './errors/custom-error';
24
export * from './errors/bad-request-error';
35
export * from './errors/db-connection-error';
46
export * from './errors/not-authorized-error';
57
export * from './errors/not-found-error';
68

79
export * from './middlewares/errorHandler';
10+
export * from './middlewares/currentUser';
11+
export * from './middlewares/requireAuth';

0 commit comments

Comments
 (0)