@wrcb/cb-common 1.0.56 → 1.0.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/middlewares/currentUser.d.ts +2 -4
- package/build/middlewares/requireAdmin.js +1 -1
- package/build/middlewares/requireAuth.js +1 -1
- package/build/test/authHelper.d.ts +6 -0
- package/build/test/authHelper.js +58 -0
- package/build/types/index.d.ts +0 -6
- package/package.json +6 -3
package/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -15,6 +15,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./test/authHelper"), exports);
|
|
18
19
|
__exportStar(require("./errors/badRequestError"), exports);
|
|
19
20
|
__exportStar(require("./errors/customError"), exports);
|
|
20
21
|
__exportStar(require("./errors/databaseConnectionError"), exports);
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
interface UserPayload {
|
|
2
|
+
export interface UserPayload {
|
|
3
3
|
id: string;
|
|
4
4
|
email: string;
|
|
5
|
-
|
|
6
|
-
role: string;
|
|
5
|
+
role?: string;
|
|
7
6
|
}
|
|
8
7
|
declare global {
|
|
9
8
|
namespace Express {
|
|
@@ -13,4 +12,3 @@ declare global {
|
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
14
|
export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
|
|
16
|
-
export {};
|
|
@@ -6,7 +6,7 @@ const userRole_1 = require("../events/types/userRole");
|
|
|
6
6
|
const requireAdmin = (req, res, next) => {
|
|
7
7
|
var _a;
|
|
8
8
|
if (((_a = req.currentUser) === null || _a === void 0 ? void 0 : _a.role) !== userRole_1.UserRole.Admin) {
|
|
9
|
-
throw new notAuthorizedError_1.NotAuthorizedError('
|
|
9
|
+
throw new notAuthorizedError_1.NotAuthorizedError('Não autorizado - apenas administrador');
|
|
10
10
|
}
|
|
11
11
|
next();
|
|
12
12
|
};
|
|
@@ -4,7 +4,7 @@ exports.requireAuth = void 0;
|
|
|
4
4
|
const notAuthorizedError_1 = require("../errors/notAuthorizedError");
|
|
5
5
|
const requireAuth = (req, res, next) => {
|
|
6
6
|
if (!req.currentUser) {
|
|
7
|
-
throw new notAuthorizedError_1.NotAuthorizedError('
|
|
7
|
+
throw new notAuthorizedError_1.NotAuthorizedError('Não autorizado');
|
|
8
8
|
}
|
|
9
9
|
next();
|
|
10
10
|
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.signUp = signUp;
|
|
16
|
+
exports.signUpAdmin = signUpAdmin;
|
|
17
|
+
const supertest_1 = __importDefault(require("supertest"));
|
|
18
|
+
const mongoose_1 = __importDefault(require("mongoose"));
|
|
19
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
20
|
+
const country_1 = require("../events/types/country");
|
|
21
|
+
const userRole_1 = require("../events/types/userRole");
|
|
22
|
+
function signUp(app) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
const response = yield (0, supertest_1.default)(app)
|
|
25
|
+
.post('/api/auth/signup')
|
|
26
|
+
.send({
|
|
27
|
+
email: 'test@test.com.br',
|
|
28
|
+
nickName: 'ap',
|
|
29
|
+
country: country_1.Country.Brazil,
|
|
30
|
+
cpf: '05854744600',
|
|
31
|
+
phoneNumber: '(37)988083717',
|
|
32
|
+
password: 'password',
|
|
33
|
+
passwordConfirmation: 'password',
|
|
34
|
+
})
|
|
35
|
+
.expect(201);
|
|
36
|
+
const cookie = response.get('Set-Cookie');
|
|
37
|
+
const userData = response.body;
|
|
38
|
+
return { cookie, userData };
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
function signUpAdmin(id) {
|
|
42
|
+
// Build a JWT payload
|
|
43
|
+
const payload = {
|
|
44
|
+
id: id !== null && id !== void 0 ? id : new mongoose_1.default.Types.ObjectId().toHexString(),
|
|
45
|
+
email: 'test@test.com',
|
|
46
|
+
role: userRole_1.UserRole.Admin,
|
|
47
|
+
};
|
|
48
|
+
// Create the JWT
|
|
49
|
+
const token = jsonwebtoken_1.default.sign(payload, process.env.JWT_KEY);
|
|
50
|
+
// Build session object {jwt: MY_JWT}
|
|
51
|
+
const session = { jwt: token };
|
|
52
|
+
// Turn that session into JSON
|
|
53
|
+
const sessionJSON = JSON.stringify(session);
|
|
54
|
+
// Take JSON and encode it as base64
|
|
55
|
+
const base64 = Buffer.from(sessionJSON).toString('base64');
|
|
56
|
+
// return a string that the cookie wieht the encoded data
|
|
57
|
+
return [`session=${base64}`];
|
|
58
|
+
}
|
package/build/types/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wrcb/cb-common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.58",
|
|
4
4
|
"description": "Common resources between services",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -22,10 +22,13 @@
|
|
|
22
22
|
"@types/cookie-session": "^2.0.49",
|
|
23
23
|
"@types/express": "^4.17.21",
|
|
24
24
|
"@types/jsonwebtoken": "^9.0.6",
|
|
25
|
+
"@types/supertest": "^6.0.2",
|
|
25
26
|
"cookie-session": "^2.1.0",
|
|
26
|
-
"express": "^4.
|
|
27
|
+
"express": "^4.21.0",
|
|
27
28
|
"express-validator": "^7.1.0",
|
|
28
29
|
"jsonwebtoken": "^9.0.2",
|
|
29
|
-
"
|
|
30
|
+
"mongoose": "^8.6.3",
|
|
31
|
+
"node-nats-streaming": "^0.3.2",
|
|
32
|
+
"supertest": "^7.0.0"
|
|
30
33
|
}
|
|
31
34
|
}
|