@optimatech88/titomeet-shared-lib 1.0.7 → 1.0.9

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.
Files changed (79) hide show
  1. package/.github/workflows/npm-publish.yml +34 -34
  2. package/.prettierrc +4 -4
  3. package/dist/auth/admin.middleware.d.ts +11 -0
  4. package/dist/auth/admin.middleware.d.ts.map +1 -0
  5. package/dist/auth/admin.middleware.js +26 -0
  6. package/dist/auth/admin.middleware.js.map +1 -0
  7. package/dist/auth/auth.guard.d.ts +17 -0
  8. package/dist/auth/auth.guard.d.ts.map +1 -0
  9. package/dist/auth/auth.guard.js +89 -0
  10. package/dist/auth/auth.guard.js.map +1 -0
  11. package/dist/auth/auth.module.d.ts +9 -0
  12. package/dist/auth/auth.module.d.ts.map +1 -0
  13. package/dist/auth/auth.module.js +32 -0
  14. package/dist/auth/auth.module.js.map +1 -0
  15. package/dist/cache/cache.module.d.ts +5 -0
  16. package/dist/cache/cache.module.d.ts.map +1 -0
  17. package/dist/cache/cache.module.js +41 -0
  18. package/dist/cache/cache.module.js.map +1 -0
  19. package/dist/cache/cache.service.d.ts +9 -0
  20. package/dist/cache/cache.service.d.ts.map +1 -0
  21. package/dist/cache/cache.service.js +38 -0
  22. package/dist/cache/cache.service.js.map +1 -0
  23. package/dist/index.d.ts +15 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +40 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/prisma/prisma.module.d.ts +3 -0
  28. package/dist/prisma/prisma.module.d.ts.map +1 -0
  29. package/dist/prisma/prisma.module.js +22 -0
  30. package/dist/prisma/prisma.module.js.map +1 -0
  31. package/dist/prisma/prisma.service.d.ts +7 -0
  32. package/dist/prisma/prisma.service.d.ts.map +1 -0
  33. package/dist/prisma/prisma.service.js +24 -0
  34. package/dist/prisma/prisma.service.js.map +1 -0
  35. package/dist/prisma/prisma.service.spec.d.ts +2 -0
  36. package/dist/prisma/prisma.service.spec.d.ts.map +1 -0
  37. package/dist/prisma/prisma.service.spec.js +17 -0
  38. package/dist/prisma/prisma.service.spec.js.map +1 -0
  39. package/dist/prisma/seed.d.ts +2 -0
  40. package/dist/prisma/seed.d.ts.map +1 -0
  41. package/dist/prisma/seed.js +73 -0
  42. package/dist/prisma/seed.js.map +1 -0
  43. package/dist/types/index.d.ts +12 -0
  44. package/dist/types/index.d.ts.map +1 -0
  45. package/dist/types/index.js +3 -0
  46. package/dist/types/index.js.map +1 -0
  47. package/dist/utils/api/endpoints.d.ts +7 -0
  48. package/dist/utils/api/endpoints.d.ts.map +1 -0
  49. package/dist/utils/api/endpoints.js +10 -0
  50. package/dist/utils/api/endpoints.js.map +1 -0
  51. package/dist/utils/constants.d.ts +3 -0
  52. package/dist/utils/constants.d.ts.map +1 -0
  53. package/dist/utils/constants.js +15 -0
  54. package/dist/utils/constants.js.map +1 -0
  55. package/dist/utils/events.d.ts +7 -0
  56. package/dist/utils/events.d.ts.map +1 -0
  57. package/dist/utils/events.js +10 -0
  58. package/dist/utils/events.js.map +1 -0
  59. package/dist/utils/index.d.ts +8 -0
  60. package/dist/utils/index.d.ts.map +1 -0
  61. package/dist/utils/index.js +14 -0
  62. package/dist/utils/index.js.map +1 -0
  63. package/package.json +1 -1
  64. package/prisma/schema.prisma +3 -1
  65. package/src/auth/admin.middleware.ts +20 -20
  66. package/src/auth/auth.guard.ts +98 -98
  67. package/src/auth/auth.module.ts +24 -24
  68. package/src/cache/cache.module.ts +25 -25
  69. package/src/cache/cache.service.ts +19 -19
  70. package/src/index.ts +29 -29
  71. package/src/prisma/prisma.module.ts +9 -9
  72. package/src/prisma/prisma.service.ts +13 -13
  73. package/src/prisma/seed.ts +82 -0
  74. package/src/types/index.ts +12 -12
  75. package/src/utils/api/endpoints.ts +6 -6
  76. package/src/utils/constants.ts +13 -13
  77. package/src/utils/events.ts +7 -7
  78. package/src/utils/index.ts +11 -11
  79. 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,12 @@
1
+ export interface PaginatedData<T> {
2
+ items: T[];
3
+ total: number;
4
+ page: number;
5
+ limit: number;
6
+ totalPages: number;
7
+ }
8
+ export interface PaginationQuery {
9
+ page?: string;
10
+ limit?: string;
11
+ }
12
+ //# sourceMappingURL=index.d.ts.map
@@ -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,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ export declare const CHAT_API_URL: string;
2
+ export declare const PROPERTIES_API_URL: string;
3
+ export declare const API_ENDPOINTS: {
4
+ CHAT: string;
5
+ PROPERTIES_API_URL: string;
6
+ };
7
+ //# sourceMappingURL=endpoints.d.ts.map
@@ -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,3 @@
1
+ export declare const DEFAULT_AVATAR_IMAGE = "https://immob-bucket-2.s3.us-east-2.amazonaws.com/pictures/images.png";
2
+ export declare const TEMPORARY_EMAIL_DOMAINS: string[];
3
+ //# sourceMappingURL=constants.d.ts.map
@@ -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,7 @@
1
+ export declare const SOCKET_EVENTS: {
2
+ INBOX_OPENED: string;
3
+ INBOX_CHANGED: string;
4
+ NEW_MESSAGE: string;
5
+ GET_UNREAD_MESSAGES_COUNT: string;
6
+ };
7
+ //# sourceMappingURL=events.d.ts.map
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@optimatech88/titomeet-shared-lib",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
@@ -77,12 +77,14 @@ enum EventVisibility {
77
77
 
78
78
  enum EventStatus {
79
79
  DRAFT
80
+ PENDING
80
81
  PUBLISHED
82
+ CANCELLED
81
83
  }
82
84
 
83
85
  model EventCategory {
84
86
  id String @id @default(cuid())
85
- name String
87
+ name String @unique
86
88
  description String?
87
89
  events Event[]
88
90
 
@@ -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
+ }
@@ -1,98 +1,98 @@
1
- import {
2
- CanActivate,
3
- ExecutionContext,
4
- Injectable,
5
- UnauthorizedException,
6
- } from '@nestjs/common';
7
- import { JwtService } from '@nestjs/jwt';
8
- import { PrismaClient } from '@prisma/client';
9
- import { Request } from 'express';
10
- import { IncomingHttpHeaders } from 'http';
11
-
12
- @Injectable()
13
- export class AuthGuard implements CanActivate {
14
- private prisma: PrismaClient;
15
-
16
- constructor(
17
- private jwtService: JwtService,
18
- ) {
19
- this.prisma = new PrismaClient();
20
- }
21
-
22
- async canActivate(context: ExecutionContext): Promise<boolean> {
23
- const request = context.switchToHttp().getRequest();
24
- const token = this.extractTokenFromHeader(request);
25
- if (!token) {
26
- throw new UnauthorizedException();
27
- }
28
- try {
29
- const payload = (await this.jwtService.verifyAsync(token)) as {
30
- userId: string;
31
- };
32
-
33
- const user = await this.prisma.user.findUnique({
34
- where: { id: payload.userId }
35
- });
36
-
37
- if (!user) {
38
- throw new UnauthorizedException();
39
- }
40
-
41
- request['user'] = user;
42
- } catch {
43
- throw new UnauthorizedException();
44
- }
45
- return true;
46
- }
47
-
48
- private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
49
- const [type, token] = request.headers.authorization?.split(' ') ?? [];
50
- return type === 'Bearer' ? token : undefined;
51
- }
52
- }
53
-
54
- //optional auth guard
55
- @Injectable()
56
- export class OptionalAuthGuard implements CanActivate {
57
- private prisma: PrismaClient;
58
-
59
- constructor(
60
- private jwtService: JwtService,
61
- ) {
62
- this.prisma = new PrismaClient();
63
- }
64
-
65
- async canActivate(context: ExecutionContext): Promise<boolean> {
66
- const request = context.switchToHttp().getRequest();
67
- const token = this.extractTokenFromHeader(request);
68
- if (!token) {
69
- return true;
70
- }
71
- try {
72
- const payload = (await this.jwtService.verifyAsync(token)) as {
73
- userId: string;
74
- };
75
-
76
- const user = await this.prisma.user.findUnique({
77
- where: { id: payload.userId }
78
- });
79
-
80
- if (!user) {
81
- throw new UnauthorizedException();
82
- }
83
-
84
- request['user'] = user;
85
-
86
- // Continue without setting user
87
- } catch {
88
- // Continue without setting user
89
- }
90
- return true;
91
- }
92
-
93
- private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
94
- const [type, token] = request.headers.authorization?.split(' ') ?? [];
95
- return type === 'Bearer' ? token : undefined;
96
- }
97
- }
98
-
1
+ import {
2
+ CanActivate,
3
+ ExecutionContext,
4
+ Injectable,
5
+ UnauthorizedException,
6
+ } from '@nestjs/common';
7
+ import { JwtService } from '@nestjs/jwt';
8
+ import { PrismaClient } from '@prisma/client';
9
+ import { Request } from 'express';
10
+ import { IncomingHttpHeaders } from 'http';
11
+
12
+ @Injectable()
13
+ export class AuthGuard implements CanActivate {
14
+ private prisma: PrismaClient;
15
+
16
+ constructor(
17
+ private jwtService: JwtService,
18
+ ) {
19
+ this.prisma = new PrismaClient();
20
+ }
21
+
22
+ async canActivate(context: ExecutionContext): Promise<boolean> {
23
+ const request = context.switchToHttp().getRequest();
24
+ const token = this.extractTokenFromHeader(request);
25
+ if (!token) {
26
+ throw new UnauthorizedException();
27
+ }
28
+ try {
29
+ const payload = (await this.jwtService.verifyAsync(token)) as {
30
+ userId: string;
31
+ };
32
+
33
+ const user = await this.prisma.user.findUnique({
34
+ where: { id: payload.userId }
35
+ });
36
+
37
+ if (!user) {
38
+ throw new UnauthorizedException();
39
+ }
40
+
41
+ request['user'] = user;
42
+ } catch {
43
+ throw new UnauthorizedException();
44
+ }
45
+ return true;
46
+ }
47
+
48
+ private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
49
+ const [type, token] = request.headers.authorization?.split(' ') ?? [];
50
+ return type === 'Bearer' ? token : undefined;
51
+ }
52
+ }
53
+
54
+ //optional auth guard
55
+ @Injectable()
56
+ export class OptionalAuthGuard implements CanActivate {
57
+ private prisma: PrismaClient;
58
+
59
+ constructor(
60
+ private jwtService: JwtService,
61
+ ) {
62
+ this.prisma = new PrismaClient();
63
+ }
64
+
65
+ async canActivate(context: ExecutionContext): Promise<boolean> {
66
+ const request = context.switchToHttp().getRequest();
67
+ const token = this.extractTokenFromHeader(request);
68
+ if (!token) {
69
+ return true;
70
+ }
71
+ try {
72
+ const payload = (await this.jwtService.verifyAsync(token)) as {
73
+ userId: string;
74
+ };
75
+
76
+ const user = await this.prisma.user.findUnique({
77
+ where: { id: payload.userId }
78
+ });
79
+
80
+ if (!user) {
81
+ throw new UnauthorizedException();
82
+ }
83
+
84
+ request['user'] = user;
85
+
86
+ // Continue without setting user
87
+ } catch {
88
+ // Continue without setting user
89
+ }
90
+ return true;
91
+ }
92
+
93
+ private extractTokenFromHeader(request: Request & { headers: IncomingHttpHeaders }): string | undefined {
94
+ const [type, token] = request.headers.authorization?.split(' ') ?? [];
95
+ return type === 'Bearer' ? token : undefined;
96
+ }
97
+ }
98
+
@@ -1,24 +1,24 @@
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
- return {
14
- module: AuthModule,
15
- imports: [
16
- JwtModule.register({
17
- secret: options.jwtSecret,
18
- signOptions: { expiresIn: options.jwtExpiresIn || '7d' },
19
- }),
20
- ],
21
- exports: [JwtModule],
22
- };
23
- }
24
- }
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
+ return {
14
+ module: AuthModule,
15
+ imports: [
16
+ JwtModule.register({
17
+ secret: options.jwtSecret,
18
+ signOptions: { expiresIn: options.jwtExpiresIn || '7d' },
19
+ }),
20
+ ],
21
+ exports: [JwtModule],
22
+ };
23
+ }
24
+ }
@@ -1,25 +1,25 @@
1
- import { Module, DynamicModule, Global } from '@nestjs/common';
2
- import { Cacheable } from 'cacheable';
3
- import { CacheService } from './cache.service';
4
- import KeyvRedis from '@keyv/redis';
5
-
6
- @Global()
7
- @Module({})
8
- export class CacheModule {
9
- static forRoot(redisUrl: string): DynamicModule {
10
- return {
11
- module: CacheModule,
12
- providers: [
13
- {
14
- provide: 'CACHE_INSTANCE',
15
- useFactory: () => {
16
- const secondary = new KeyvRedis(redisUrl);
17
- return new Cacheable({ secondary, ttl: '1h' });
18
- },
19
- },
20
- CacheService,
21
- ],
22
- exports: ['CACHE_INSTANCE', CacheService],
23
- };
24
- }
25
- }
1
+ import { Module, DynamicModule, Global } from '@nestjs/common';
2
+ import { Cacheable } from 'cacheable';
3
+ import { CacheService } from './cache.service';
4
+ import KeyvRedis from '@keyv/redis';
5
+
6
+ @Global()
7
+ @Module({})
8
+ export class CacheModule {
9
+ static forRoot(redisUrl: string): DynamicModule {
10
+ return {
11
+ module: CacheModule,
12
+ providers: [
13
+ {
14
+ provide: 'CACHE_INSTANCE',
15
+ useFactory: () => {
16
+ const secondary = new KeyvRedis(redisUrl);
17
+ return new Cacheable({ secondary, ttl: '1h' });
18
+ },
19
+ },
20
+ CacheService,
21
+ ],
22
+ exports: ['CACHE_INSTANCE', CacheService],
23
+ };
24
+ }
25
+ }
@@ -1,19 +1,19 @@
1
- import { Inject, Injectable } from '@nestjs/common';
2
- import { Cacheable } from 'cacheable';
3
-
4
- @Injectable()
5
- export class CacheService<T> {
6
- constructor(@Inject('CACHE_INSTANCE') private readonly cache: Cacheable) {}
7
-
8
- async get<T>(key: string): Promise<T> {
9
- return await this.cache.get(key);
10
- }
11
-
12
- async set(key: string, value: T, ttl?: number | string): Promise<void> {
13
- await this.cache.set(key, value, ttl);
14
- }
15
-
16
- async delete(key: string): Promise<void> {
17
- await this.cache.delete(key);
18
- }
19
- }
1
+ import { Inject, Injectable } from '@nestjs/common';
2
+ import { Cacheable } from 'cacheable';
3
+
4
+ @Injectable()
5
+ export class CacheService<T> {
6
+ constructor(@Inject('CACHE_INSTANCE') private readonly cache: Cacheable) {}
7
+
8
+ async get<T>(key: string): Promise<T> {
9
+ return await this.cache.get(key);
10
+ }
11
+
12
+ async set(key: string, value: T, ttl?: number | string): Promise<void> {
13
+ await this.cache.set(key, value, ttl);
14
+ }
15
+
16
+ async delete(key: string): Promise<void> {
17
+ await this.cache.delete(key);
18
+ }
19
+ }