@optimatech88/titomeet-shared-lib 1.0.26 → 1.0.27
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/.github/workflows/npm-publish.yml +34 -34
- package/.prettierrc +4 -4
- package/dist/auth/admin.middleware.d.ts +11 -0
- package/dist/auth/admin.middleware.d.ts.map +1 -0
- package/dist/auth/admin.middleware.js +26 -0
- package/dist/auth/admin.middleware.js.map +1 -0
- package/dist/auth/auth.guard.d.ts +25 -0
- package/dist/auth/auth.guard.d.ts.map +1 -0
- package/dist/auth/auth.guard.js +133 -0
- package/dist/auth/auth.guard.js.map +1 -0
- package/dist/auth/auth.module.d.ts +9 -0
- package/dist/auth/auth.module.d.ts.map +1 -0
- package/dist/auth/auth.module.js +33 -0
- package/dist/auth/auth.module.js.map +1 -0
- package/dist/cache/cache.module.d.ts +5 -0
- package/dist/cache/cache.module.d.ts.map +1 -0
- package/dist/cache/cache.module.js +41 -0
- package/dist/cache/cache.module.js.map +1 -0
- package/dist/cache/cache.service.d.ts +9 -0
- package/dist/cache/cache.service.d.ts.map +1 -0
- package/dist/cache/cache.service.js +38 -0
- package/dist/cache/cache.service.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/index.js.map +1 -0
- package/dist/prisma/prisma.module.d.ts +3 -0
- package/dist/prisma/prisma.module.d.ts.map +1 -0
- package/dist/prisma/prisma.module.js +22 -0
- package/dist/prisma/prisma.module.js.map +1 -0
- package/dist/prisma/prisma.service.d.ts +7 -0
- package/dist/prisma/prisma.service.d.ts.map +1 -0
- package/dist/prisma/prisma.service.js +24 -0
- package/dist/prisma/prisma.service.js.map +1 -0
- package/dist/prisma/seed.d.ts +2 -0
- package/dist/prisma/seed.d.ts.map +1 -0
- package/dist/prisma/seed.js +73 -0
- package/dist/prisma/seed.js.map +1 -0
- package/dist/types/index.d.ts +12 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/api/endpoints.d.ts +7 -0
- package/dist/utils/api/endpoints.d.ts.map +1 -0
- package/dist/utils/api/endpoints.js +10 -0
- package/dist/utils/api/endpoints.js.map +1 -0
- package/dist/utils/constants.d.ts +3 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +15 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/events.d.ts +7 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/utils/events.js +10 -0
- package/dist/utils/events.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +14 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +1 -1
- package/src/auth/admin.middleware.ts +20 -20
- package/src/auth/auth.guard.ts +154 -154
- package/src/auth/auth.module.ts +25 -25
- package/src/cache/cache.module.ts +25 -25
- package/src/cache/cache.service.ts +19 -19
- package/src/index.ts +40 -40
- package/src/prisma/prisma.module.ts +9 -9
- package/src/prisma/prisma.service.ts +13 -13
- package/src/prisma/seed.ts +82 -82
- package/src/types/index.ts +12 -12
- package/src/utils/api/endpoints.ts +6 -6
- package/src/utils/constants.ts +13 -13
- package/src/utils/events.ts +7 -7
- package/src/utils/index.ts +11 -11
- package/tsconfig.json +24 -24
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const client_1 = require("@prisma/client");
|
|
4
|
+
const prisma = new client_1.PrismaClient();
|
|
5
|
+
async function main() {
|
|
6
|
+
try {
|
|
7
|
+
const categories = [
|
|
8
|
+
{
|
|
9
|
+
name: 'Music',
|
|
10
|
+
description: 'Music events',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
name: 'Food',
|
|
14
|
+
description: 'Food events',
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
name: 'Art',
|
|
18
|
+
description: 'Art events',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: 'Sports',
|
|
22
|
+
description: 'Sports events',
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: 'Games',
|
|
26
|
+
description: 'Games events',
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
name: 'Dance',
|
|
30
|
+
description: 'Dance events',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
name: 'Fashion',
|
|
34
|
+
description: 'Fashion events',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: 'Tech',
|
|
38
|
+
description: 'Technology events',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: 'Science',
|
|
42
|
+
description: 'Science events',
|
|
43
|
+
},
|
|
44
|
+
];
|
|
45
|
+
const categoriesCreated = await prisma.eventCategory.findMany({
|
|
46
|
+
where: {
|
|
47
|
+
name: {
|
|
48
|
+
in: categories.map((category) => category.name),
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
const categoriesNotCreated = categories.filter((category) => !categoriesCreated.some((c) => c.name === category.name));
|
|
53
|
+
if (categoriesNotCreated.length > 0) {
|
|
54
|
+
console.log(`Creating ${categoriesNotCreated.length} categories`);
|
|
55
|
+
await prisma.eventCategory.createMany({
|
|
56
|
+
data: categoriesNotCreated,
|
|
57
|
+
});
|
|
58
|
+
console.log(`Created ${categoriesNotCreated.length} categories`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
console.error(error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
main()
|
|
66
|
+
.catch((error) => {
|
|
67
|
+
console.error(error);
|
|
68
|
+
process.exit(1);
|
|
69
|
+
})
|
|
70
|
+
.finally(async () => {
|
|
71
|
+
await prisma.$disconnect();
|
|
72
|
+
});
|
|
73
|
+
//# sourceMappingURL=seed.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed.js","sourceRoot":"","sources":["../../src/prisma/seed.ts"],"names":[],"mappings":";;AAEA,2CAA8C;AAE9C,MAAM,MAAM,GAAG,IAAI,qBAAY,EAAE,CAAC;AAElC,KAAK,UAAU,IAAI;IACf,IAAI,CAAC;QACD,MAAM,UAAU,GAAG;YACnB;gBACI,IAAI,EAAE,OAAO;gBACb,WAAW,EAAE,cAAc;aAC9B;YACD;gBACI,IAAI,EAAE,MAAM;gBACZ,WAAW,EAAE,aAAa;aAC7B;YACD;gBACI,IAAI,EAAE,KAAK;gBACX,WAAW,EAAE,YAAY;aAC5B;YACD;gBACI,IAAI,EAAE,QAAQ;gBACd,WAAW,EAAE,eAAe;aAC/B;YACD;gBACI,IAAI,EAAE,OAAO;gBACb,WAAW,EAAE,cAAc;aAC9B;YACD;gBACI,IAAI,EAAE,OAAO;gBACb,WAAW,EAAE,cAAc;aAC9B;YACD;gBACI,IAAI,EAAE,SAAS;gBACf,WAAW,EAAE,gBAAgB;aAChC;YACD;gBACI,IAAI,EAAE,MAAM;gBACZ,WAAW,EAAE,mBAAmB;aACnC;YACD;gBACI,IAAI,EAAE,SAAS;gBACf,WAAW,EAAE,gBAAgB;aAChC;SAEA,CAAC;QAEF,MAAM,iBAAiB,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,QAAQ,CAAC;YAC1D,KAAK,EAAE;gBACH,IAAI,EAAE;oBACF,EAAE,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC;iBAClD;aACJ;SACJ,CAAC,CAAC;QAEH,MAAM,oBAAoB,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QAEvH,IAAG,oBAAoB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACjC,OAAO,CAAC,GAAG,CAAC,YAAY,oBAAoB,CAAC,MAAM,aAAa,CAAC,CAAC;YAClE,MAAM,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC;gBAClC,IAAI,EAAE,oBAAoB;aAC7B,CAAC,CAAC;YACH,OAAO,CAAC,GAAG,CAAC,WAAW,oBAAoB,CAAC,MAAM,aAAa,CAAC,CAAC;QACrE,CAAC;IAGL,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACzB,CAAC;AAGL,CAAC;AAED,IAAI,EAAE;KACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACb,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACrB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACpB,CAAC,CAAC;KACD,OAAO,CAAC,KAAK,IAAI,EAAE;IAChB,MAAM,MAAM,CAAC,WAAW,EAAE,CAAC;AAC/B,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,KAAK,EAAE,CAAC,EAAE,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"endpoints.d.ts","sourceRoot":"","sources":["../../../src/utils/api/endpoints.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,EAAE,MAAoB,CAAA;AAC/C,eAAO,MAAM,kBAAkB,EAAE,MAAyB,CAAA;AAE1D,eAAO,MAAM,aAAa;;;CAGzB,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.API_ENDPOINTS = exports.PROPERTIES_API_URL = exports.CHAT_API_URL = void 0;
|
|
4
|
+
exports.CHAT_API_URL = "api/chats";
|
|
5
|
+
exports.PROPERTIES_API_URL = "api/properties";
|
|
6
|
+
exports.API_ENDPOINTS = {
|
|
7
|
+
CHAT: 'api/chats',
|
|
8
|
+
PROPERTIES_API_URL: 'api/properties',
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=endpoints.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"endpoints.js","sourceRoot":"","sources":["../../../src/utils/api/endpoints.ts"],"names":[],"mappings":";;;AAAa,QAAA,YAAY,GAAW,WAAW,CAAA;AAClC,QAAA,kBAAkB,GAAW,gBAAgB,CAAA;AAE7C,QAAA,aAAa,GAAG;IAC3B,IAAI,EAAE,WAAW;IACjB,kBAAkB,EAAE,gBAAgB;CACrC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,0EACwC,CAAC;AAE1E,eAAO,MAAM,uBAAuB,UASnC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TEMPORARY_EMAIL_DOMAINS = exports.DEFAULT_AVATAR_IMAGE = void 0;
|
|
4
|
+
exports.DEFAULT_AVATAR_IMAGE = 'https://immob-bucket-2.s3.us-east-2.amazonaws.com/pictures/images.png';
|
|
5
|
+
exports.TEMPORARY_EMAIL_DOMAINS = [
|
|
6
|
+
'10minutemail.com',
|
|
7
|
+
'guerrillamail.com',
|
|
8
|
+
'mailinator.com',
|
|
9
|
+
'dispostable.com',
|
|
10
|
+
'yopmail.com',
|
|
11
|
+
'trashmail.com',
|
|
12
|
+
'tempmail.com',
|
|
13
|
+
'temp-mail.org',
|
|
14
|
+
];
|
|
15
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,oBAAoB,GAC/B,uEAAuE,CAAC;AAE7D,QAAA,uBAAuB,GAAG;IACrC,kBAAkB;IAClB,mBAAmB;IACnB,gBAAgB;IAChB,iBAAiB;IACjB,aAAa;IACb,eAAe;IACf,cAAc;IACd,eAAe;CAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/utils/events.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,aAAa;;;;;CAKzB,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SOCKET_EVENTS = void 0;
|
|
4
|
+
exports.SOCKET_EVENTS = {
|
|
5
|
+
INBOX_OPENED: 'events.sockets.inboxOpened',
|
|
6
|
+
INBOX_CHANGED: 'events.sockets.inboxChanged',
|
|
7
|
+
NEW_MESSAGE: 'events.sockets.newMessage',
|
|
8
|
+
GET_UNREAD_MESSAGES_COUNT: 'events.sockets.getUnreadMessagesCount',
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../../src/utils/events.ts"],"names":[],"mappings":";;;AAAa,QAAA,aAAa,GAAG;IACzB,YAAY,EAAE,4BAA4B;IAC1C,aAAa,EAAE,6BAA6B;IAC5C,WAAW,EAAE,2BAA2B;IACxC,yBAAyB,EAAE,uCAAuC;CACrE,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PaginationQuery } from "@/types";
|
|
2
|
+
export declare const getPaginationData: (query: PaginationQuery) => {
|
|
3
|
+
page: number;
|
|
4
|
+
skip: number;
|
|
5
|
+
limit: number;
|
|
6
|
+
};
|
|
7
|
+
export declare const getUserChannel: (userId: string) => string;
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE1C,eAAO,MAAM,iBAAiB,UAAW,eAAe;;;;CAOvD,CAAC;AAEF,eAAO,MAAM,cAAc,WAAY,MAAM,WAA8B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getUserChannel = exports.getPaginationData = void 0;
|
|
4
|
+
const getPaginationData = (query) => {
|
|
5
|
+
const page = query.page;
|
|
6
|
+
const PAGE = parseInt(page, 10) || 1;
|
|
7
|
+
const LIMIT = Number(query.limit) || 10;
|
|
8
|
+
const SKIP = (PAGE - 1) * LIMIT;
|
|
9
|
+
return { page: PAGE, skip: SKIP, limit: LIMIT };
|
|
10
|
+
};
|
|
11
|
+
exports.getPaginationData = getPaginationData;
|
|
12
|
+
const getUserChannel = (userId) => `channels-user-${userId}`;
|
|
13
|
+
exports.getUserChannel = getUserChannel;
|
|
14
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":";;;AAEO,MAAM,iBAAiB,GAAG,CAAC,KAAsB,EAAE,EAAE;IAC1D,MAAM,IAAI,GAAG,KAAK,CAAC,IAAc,CAAC;IAClC,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC;IACrC,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IACxC,MAAM,IAAI,GAAG,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;IAEhC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAClD,CAAC,CAAC;AAPW,QAAA,iBAAiB,qBAO5B;AAEK,MAAM,cAAc,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,iBAAiB,MAAM,EAAE,CAAC;AAA/D,QAAA,cAAc,kBAAiD"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/common';
|
|
2
|
-
import { Request, Response, NextFunction } from 'express';
|
|
3
|
-
import { UserRole, User } from '@prisma/client';
|
|
4
|
-
|
|
5
|
-
interface RequestWithUser extends Request {
|
|
6
|
-
user?: User;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
@Injectable()
|
|
10
|
-
export class AdminMiddleware implements NestMiddleware {
|
|
11
|
-
use(req: RequestWithUser, res: Response, next: NextFunction) {
|
|
12
|
-
const user = req.user;
|
|
13
|
-
|
|
14
|
-
if (!user || !user.role?.includes(UserRole.ADMIN)) {
|
|
15
|
-
throw new UnauthorizedException('Access denied. Admins only.');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
next();
|
|
19
|
-
}
|
|
20
|
-
}
|
|
1
|
+
import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/common';
|
|
2
|
+
import { Request, Response, NextFunction } from 'express';
|
|
3
|
+
import { UserRole, User } from '@prisma/client';
|
|
4
|
+
|
|
5
|
+
interface RequestWithUser extends Request {
|
|
6
|
+
user?: User;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
@Injectable()
|
|
10
|
+
export class AdminMiddleware implements NestMiddleware {
|
|
11
|
+
use(req: RequestWithUser, res: Response, next: NextFunction) {
|
|
12
|
+
const user = req.user;
|
|
13
|
+
|
|
14
|
+
if (!user || !user.role?.includes(UserRole.ADMIN)) {
|
|
15
|
+
throw new UnauthorizedException('Access denied. Admins only.');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
next();
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/auth/auth.guard.ts
CHANGED
|
@@ -1,154 +1,154 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CanActivate,
|
|
3
|
-
ExecutionContext,
|
|
4
|
-
Injectable,
|
|
5
|
-
UnauthorizedException,
|
|
6
|
-
} from '@nestjs/common';
|
|
7
|
-
import { JwtService } from '@nestjs/jwt';
|
|
8
|
-
import { PrismaClient, UserRole } from '@prisma/client';
|
|
9
|
-
import { Request } from 'express';
|
|
10
|
-
import { IncomingHttpHeaders } from 'http';
|
|
11
|
-
import { Logger } from '@nestjs/common';
|
|
12
|
-
|
|
13
|
-
@Injectable()
|
|
14
|
-
export class AuthGuard implements CanActivate {
|
|
15
|
-
private prisma: PrismaClient;
|
|
16
|
-
private logger = new Logger(AuthGuard.name);
|
|
17
|
-
|
|
18
|
-
constructor(
|
|
19
|
-
private jwtService: JwtService,
|
|
20
|
-
) {
|
|
21
|
-
this.prisma = new PrismaClient();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
25
|
-
const request = context.switchToHttp().getRequest();
|
|
26
|
-
const token = this.extractTokenFromHeader(request);
|
|
27
|
-
if (!token) {
|
|
28
|
-
throw new UnauthorizedException();
|
|
29
|
-
}
|
|
30
|
-
try {
|
|
31
|
-
const payload = (await this.jwtService.verifyAsync(token)) as {
|
|
32
|
-
userId: string;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
//this.logger.log('payload', payload);
|
|
36
|
-
|
|
37
|
-
const user = await this.prisma.user.findUnique({
|
|
38
|
-
where: { id: payload.userId }
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
//this.logger.log('user', user);
|
|
42
|
-
|
|
43
|
-
if (!user) {
|
|
44
|
-
throw new UnauthorizedException();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
request['user'] = user;
|
|
48
|
-
} catch {
|
|
49
|
-
this.logger.log('UnauthorizedException');
|
|
50
|
-
throw new UnauthorizedException();
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
|
|
56
|
-
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
|
57
|
-
//this.logger.log('extractTokenFromHeader', {type, token});
|
|
58
|
-
return type === 'Bearer' ? token : undefined;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
//optional auth guard
|
|
63
|
-
@Injectable()
|
|
64
|
-
export class OptionalAuthGuard implements CanActivate {
|
|
65
|
-
private prisma: PrismaClient;
|
|
66
|
-
|
|
67
|
-
constructor(
|
|
68
|
-
private jwtService: JwtService,
|
|
69
|
-
) {
|
|
70
|
-
this.prisma = new PrismaClient();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
74
|
-
const request = context.switchToHttp().getRequest();
|
|
75
|
-
const token = this.extractTokenFromHeader(request);
|
|
76
|
-
if (!token) {
|
|
77
|
-
return true;
|
|
78
|
-
}
|
|
79
|
-
try {
|
|
80
|
-
const payload = (await this.jwtService.verifyAsync(token)) as {
|
|
81
|
-
userId: string;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const user = await this.prisma.user.findUnique({
|
|
85
|
-
where: { id: payload.userId }
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
if (!user) {
|
|
89
|
-
throw new UnauthorizedException();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
request['user'] = user;
|
|
93
|
-
|
|
94
|
-
// Continue without setting user
|
|
95
|
-
} catch {
|
|
96
|
-
// Continue without setting user
|
|
97
|
-
}
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
|
|
102
|
-
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
|
103
|
-
return type === 'Bearer' ? token : undefined;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
//admin auth guard
|
|
108
|
-
@Injectable()
|
|
109
|
-
export class AdminAuthGuard implements CanActivate {
|
|
110
|
-
private prisma: PrismaClient;
|
|
111
|
-
|
|
112
|
-
constructor(
|
|
113
|
-
private jwtService: JwtService,
|
|
114
|
-
) {
|
|
115
|
-
this.prisma = new PrismaClient();
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
119
|
-
const request = context.switchToHttp().getRequest();
|
|
120
|
-
const token = this.extractTokenFromHeader(request);
|
|
121
|
-
if (!token) {
|
|
122
|
-
throw new UnauthorizedException();
|
|
123
|
-
}
|
|
124
|
-
try {
|
|
125
|
-
const payload = (await this.jwtService.verifyAsync(token)) as {
|
|
126
|
-
userId: string;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
const user = await this.prisma.user.findUnique({
|
|
130
|
-
where: { id: payload.userId }
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
if (!user) {
|
|
134
|
-
throw new UnauthorizedException();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if(user.role !== UserRole.ADMIN && user.role !== UserRole.SUPER_ADMIN) {
|
|
138
|
-
throw new UnauthorizedException();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
request['user'] = user;
|
|
142
|
-
} catch {
|
|
143
|
-
throw new UnauthorizedException();
|
|
144
|
-
}
|
|
145
|
-
return true;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
|
|
149
|
-
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
|
150
|
-
return type === 'Bearer' ? token : undefined;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
1
|
+
import {
|
|
2
|
+
CanActivate,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
Injectable,
|
|
5
|
+
UnauthorizedException,
|
|
6
|
+
} from '@nestjs/common';
|
|
7
|
+
import { JwtService } from '@nestjs/jwt';
|
|
8
|
+
import { PrismaClient, UserRole } from '@prisma/client';
|
|
9
|
+
import { Request } from 'express';
|
|
10
|
+
import { IncomingHttpHeaders } from 'http';
|
|
11
|
+
import { Logger } from '@nestjs/common';
|
|
12
|
+
|
|
13
|
+
@Injectable()
|
|
14
|
+
export class AuthGuard implements CanActivate {
|
|
15
|
+
private prisma: PrismaClient;
|
|
16
|
+
private logger = new Logger(AuthGuard.name);
|
|
17
|
+
|
|
18
|
+
constructor(
|
|
19
|
+
private jwtService: JwtService,
|
|
20
|
+
) {
|
|
21
|
+
this.prisma = new PrismaClient();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
25
|
+
const request = context.switchToHttp().getRequest();
|
|
26
|
+
const token = this.extractTokenFromHeader(request);
|
|
27
|
+
if (!token) {
|
|
28
|
+
throw new UnauthorizedException();
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const payload = (await this.jwtService.verifyAsync(token)) as {
|
|
32
|
+
userId: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
//this.logger.log('payload', payload);
|
|
36
|
+
|
|
37
|
+
const user = await this.prisma.user.findUnique({
|
|
38
|
+
where: { id: payload.userId }
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
//this.logger.log('user', user);
|
|
42
|
+
|
|
43
|
+
if (!user) {
|
|
44
|
+
throw new UnauthorizedException();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
request['user'] = user;
|
|
48
|
+
} catch {
|
|
49
|
+
this.logger.log('UnauthorizedException');
|
|
50
|
+
throw new UnauthorizedException();
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
|
|
56
|
+
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
|
57
|
+
//this.logger.log('extractTokenFromHeader', {type, token});
|
|
58
|
+
return type === 'Bearer' ? token : undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//optional auth guard
|
|
63
|
+
@Injectable()
|
|
64
|
+
export class OptionalAuthGuard implements CanActivate {
|
|
65
|
+
private prisma: PrismaClient;
|
|
66
|
+
|
|
67
|
+
constructor(
|
|
68
|
+
private jwtService: JwtService,
|
|
69
|
+
) {
|
|
70
|
+
this.prisma = new PrismaClient();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
74
|
+
const request = context.switchToHttp().getRequest();
|
|
75
|
+
const token = this.extractTokenFromHeader(request);
|
|
76
|
+
if (!token) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const payload = (await this.jwtService.verifyAsync(token)) as {
|
|
81
|
+
userId: string;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const user = await this.prisma.user.findUnique({
|
|
85
|
+
where: { id: payload.userId }
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (!user) {
|
|
89
|
+
throw new UnauthorizedException();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
request['user'] = user;
|
|
93
|
+
|
|
94
|
+
// Continue without setting user
|
|
95
|
+
} catch {
|
|
96
|
+
// Continue without setting user
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
|
|
102
|
+
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
|
103
|
+
return type === 'Bearer' ? token : undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//admin auth guard
|
|
108
|
+
@Injectable()
|
|
109
|
+
export class AdminAuthGuard implements CanActivate {
|
|
110
|
+
private prisma: PrismaClient;
|
|
111
|
+
|
|
112
|
+
constructor(
|
|
113
|
+
private jwtService: JwtService,
|
|
114
|
+
) {
|
|
115
|
+
this.prisma = new PrismaClient();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
119
|
+
const request = context.switchToHttp().getRequest();
|
|
120
|
+
const token = this.extractTokenFromHeader(request);
|
|
121
|
+
if (!token) {
|
|
122
|
+
throw new UnauthorizedException();
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
const payload = (await this.jwtService.verifyAsync(token)) as {
|
|
126
|
+
userId: string;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const user = await this.prisma.user.findUnique({
|
|
130
|
+
where: { id: payload.userId }
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
if (!user) {
|
|
134
|
+
throw new UnauthorizedException();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if(user.role !== UserRole.ADMIN && user.role !== UserRole.SUPER_ADMIN) {
|
|
138
|
+
throw new UnauthorizedException();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
request['user'] = user;
|
|
142
|
+
} catch {
|
|
143
|
+
throw new UnauthorizedException();
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
|
|
149
|
+
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
|
150
|
+
return type === 'Bearer' ? token : undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
package/src/auth/auth.module.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { Module, DynamicModule, Global } from '@nestjs/common';
|
|
2
|
-
import { JwtModule } from '@nestjs/jwt';
|
|
3
|
-
|
|
4
|
-
export interface AuthModuleOptions {
|
|
5
|
-
jwtSecret: string;
|
|
6
|
-
jwtExpiresIn?: string | number;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
@Global()
|
|
10
|
-
@Module({})
|
|
11
|
-
export class AuthModule {
|
|
12
|
-
static forRoot(options: AuthModuleOptions): DynamicModule {
|
|
13
|
-
console.log('AuthModule.forRoot', options);
|
|
14
|
-
return {
|
|
15
|
-
module: AuthModule,
|
|
16
|
-
imports: [
|
|
17
|
-
JwtModule.register({
|
|
18
|
-
secret: options.jwtSecret,
|
|
19
|
-
signOptions: { expiresIn: options.jwtExpiresIn || '7d' },
|
|
20
|
-
}),
|
|
21
|
-
],
|
|
22
|
-
exports: [JwtModule],
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
}
|
|
1
|
+
import { Module, DynamicModule, Global } from '@nestjs/common';
|
|
2
|
+
import { JwtModule } from '@nestjs/jwt';
|
|
3
|
+
|
|
4
|
+
export interface AuthModuleOptions {
|
|
5
|
+
jwtSecret: string;
|
|
6
|
+
jwtExpiresIn?: string | number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
@Global()
|
|
10
|
+
@Module({})
|
|
11
|
+
export class AuthModule {
|
|
12
|
+
static forRoot(options: AuthModuleOptions): DynamicModule {
|
|
13
|
+
console.log('AuthModule.forRoot', options);
|
|
14
|
+
return {
|
|
15
|
+
module: AuthModule,
|
|
16
|
+
imports: [
|
|
17
|
+
JwtModule.register({
|
|
18
|
+
secret: options.jwtSecret,
|
|
19
|
+
signOptions: { expiresIn: options.jwtExpiresIn || '7d' },
|
|
20
|
+
}),
|
|
21
|
+
],
|
|
22
|
+
exports: [JwtModule],
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
}
|