@optimatech88/titomeet-shared-lib 1.0.25 → 1.0.26
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/package.json +1 -1
- package/prisma/schema.prisma +15 -0
- 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
- package/dist/auth/admin.middleware.d.ts +0 -11
- package/dist/auth/admin.middleware.d.ts.map +0 -1
- package/dist/auth/admin.middleware.js +0 -26
- package/dist/auth/admin.middleware.js.map +0 -1
- package/dist/auth/auth.guard.d.ts +0 -25
- package/dist/auth/auth.guard.d.ts.map +0 -1
- package/dist/auth/auth.guard.js +0 -136
- package/dist/auth/auth.guard.js.map +0 -1
- package/dist/auth/auth.module.d.ts +0 -9
- package/dist/auth/auth.module.d.ts.map +0 -1
- package/dist/auth/auth.module.js +0 -33
- package/dist/auth/auth.module.js.map +0 -1
- package/dist/cache/cache.module.d.ts +0 -5
- package/dist/cache/cache.module.d.ts.map +0 -1
- package/dist/cache/cache.module.js +0 -41
- package/dist/cache/cache.module.js.map +0 -1
- package/dist/cache/cache.service.d.ts +0 -9
- package/dist/cache/cache.service.d.ts.map +0 -1
- package/dist/cache/cache.service.js +0 -38
- package/dist/cache/cache.service.js.map +0 -1
- package/dist/index.d.ts +0 -16
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -43
- package/dist/index.js.map +0 -1
- package/dist/prisma/prisma.module.d.ts +0 -3
- package/dist/prisma/prisma.module.d.ts.map +0 -1
- package/dist/prisma/prisma.module.js +0 -22
- package/dist/prisma/prisma.module.js.map +0 -1
- package/dist/prisma/prisma.service.d.ts +0 -7
- package/dist/prisma/prisma.service.d.ts.map +0 -1
- package/dist/prisma/prisma.service.js +0 -24
- package/dist/prisma/prisma.service.js.map +0 -1
- package/dist/prisma/seed.d.ts +0 -2
- package/dist/prisma/seed.d.ts.map +0 -1
- package/dist/prisma/seed.js +0 -73
- package/dist/prisma/seed.js.map +0 -1
- package/dist/types/index.d.ts +0 -12
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -3
- package/dist/types/index.js.map +0 -1
- package/dist/utils/api/endpoints.d.ts +0 -7
- package/dist/utils/api/endpoints.d.ts.map +0 -1
- package/dist/utils/api/endpoints.js +0 -10
- package/dist/utils/api/endpoints.js.map +0 -1
- package/dist/utils/constants.d.ts +0 -3
- package/dist/utils/constants.d.ts.map +0 -1
- package/dist/utils/constants.js +0 -15
- package/dist/utils/constants.js.map +0 -1
- package/dist/utils/events.d.ts +0 -7
- package/dist/utils/events.d.ts.map +0 -1
- package/dist/utils/events.js +0 -10
- package/dist/utils/events.js.map +0 -1
- package/dist/utils/index.d.ts +0 -8
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -14
- package/dist/utils/index.js.map +0 -1
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
-
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
-
|
|
4
|
-
name: Node.js Package
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
push:
|
|
8
|
-
branches:
|
|
9
|
-
- main # Or your main branch
|
|
10
|
-
|
|
11
|
-
jobs:
|
|
12
|
-
build:
|
|
13
|
-
runs-on: ubuntu-latest
|
|
14
|
-
steps:
|
|
15
|
-
- uses: actions/checkout@v4
|
|
16
|
-
- uses: actions/setup-node@v4
|
|
17
|
-
with:
|
|
18
|
-
node-version: 20
|
|
19
|
-
- run: npm ci
|
|
20
|
-
- run: npm run build
|
|
21
|
-
|
|
22
|
-
publish-npm:
|
|
23
|
-
needs: build
|
|
24
|
-
runs-on: ubuntu-latest
|
|
25
|
-
steps:
|
|
26
|
-
- uses: actions/checkout@v4
|
|
27
|
-
- uses: actions/setup-node@v4
|
|
28
|
-
with:
|
|
29
|
-
node-version: 20
|
|
30
|
-
registry-url: https://registry.npmjs.org/
|
|
31
|
-
- run: npm ci
|
|
32
|
-
- run: npm publish --access public
|
|
33
|
-
env:
|
|
34
|
-
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
|
1
|
+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
|
|
3
|
+
|
|
4
|
+
name: Node.js Package
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches:
|
|
9
|
+
- main # Or your main branch
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: actions/setup-node@v4
|
|
17
|
+
with:
|
|
18
|
+
node-version: 20
|
|
19
|
+
- run: npm ci
|
|
20
|
+
- run: npm run build
|
|
21
|
+
|
|
22
|
+
publish-npm:
|
|
23
|
+
needs: build
|
|
24
|
+
runs-on: ubuntu-latest
|
|
25
|
+
steps:
|
|
26
|
+
- uses: actions/checkout@v4
|
|
27
|
+
- uses: actions/setup-node@v4
|
|
28
|
+
with:
|
|
29
|
+
node-version: 20
|
|
30
|
+
registry-url: https://registry.npmjs.org/
|
|
31
|
+
- run: npm ci
|
|
32
|
+
- run: npm publish --access public
|
|
33
|
+
env:
|
|
34
|
+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
package/.prettierrc
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
{
|
|
2
|
-
"singleQuote": true,
|
|
3
|
-
"trailingComma": "all",
|
|
4
|
-
"endOfLine":"auto"
|
|
1
|
+
{
|
|
2
|
+
"singleQuote": true,
|
|
3
|
+
"trailingComma": "all",
|
|
4
|
+
"endOfLine":"auto"
|
|
5
5
|
}
|
package/package.json
CHANGED
package/prisma/schema.prisma
CHANGED
|
@@ -20,6 +20,12 @@ enum UserRole {
|
|
|
20
20
|
USER
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
enum UserStatus {
|
|
24
|
+
ACTIVE
|
|
25
|
+
INACTIVE
|
|
26
|
+
DELETED
|
|
27
|
+
}
|
|
28
|
+
|
|
23
29
|
model User {
|
|
24
30
|
id String @id @default(cuid())
|
|
25
31
|
username String @unique
|
|
@@ -31,6 +37,7 @@ model User {
|
|
|
31
37
|
|
|
32
38
|
profilePicture String?
|
|
33
39
|
emailVerified Boolean @default(false)
|
|
40
|
+
status UserStatus @default(ACTIVE)
|
|
34
41
|
|
|
35
42
|
createdAt DateTime @default(now())
|
|
36
43
|
updatedAt DateTime @updatedAt
|
|
@@ -181,6 +188,13 @@ model ChatUser {
|
|
|
181
188
|
updatedAt DateTime @updatedAt
|
|
182
189
|
}
|
|
183
190
|
|
|
191
|
+
enum MediaType {
|
|
192
|
+
image
|
|
193
|
+
video
|
|
194
|
+
audio
|
|
195
|
+
pdf
|
|
196
|
+
}
|
|
197
|
+
|
|
184
198
|
model Message {
|
|
185
199
|
id String @id @default(cuid())
|
|
186
200
|
text String
|
|
@@ -190,6 +204,7 @@ model Message {
|
|
|
190
204
|
senderId String
|
|
191
205
|
|
|
192
206
|
mediaUrl String?
|
|
207
|
+
mediaType MediaType?
|
|
193
208
|
|
|
194
209
|
createdAt DateTime @default(now())
|
|
195
210
|
updatedAt DateTime @updatedAt
|
|
@@ -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
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
+
}
|
|
@@ -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
|
+
}
|