ecrs-auth-core 1.0.43 → 1.0.44
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/README.md +263 -0
- package/dist/auth.controller.d.ts +26 -19
- package/dist/auth.controller.js +41 -41
- package/dist/auth.module.d.ts +9 -9
- package/dist/auth.module.js +143 -143
- package/dist/auth.service.d.ts +47 -40
- package/dist/auth.service.js +185 -168
- package/dist/constants/constants.d.ts +1 -1
- package/dist/constants/constants.js +4 -4
- package/dist/decorators/current-user.decorator.d.ts +1 -1
- package/dist/decorators/current-user.decorator.js +9 -9
- package/dist/decorators/feature.decorator.d.ts +1 -1
- package/dist/decorators/feature.decorator.js +11 -11
- package/dist/decorators/has-permission.decorator.d.ts +6 -6
- package/dist/decorators/has-permission.decorator.js +8 -8
- package/dist/decorators/roles.decorator.d.ts +1 -1
- package/dist/decorators/roles.decorator.js +6 -6
- package/dist/decorators/route-permission.decorator.d.ts +1 -1
- package/dist/decorators/route-permission.decorator.js +11 -11
- package/dist/dtos/login.dto.d.ts +4 -4
- package/dist/dtos/login.dto.js +7 -7
- package/dist/entities/feature.entity.d.ts +9 -9
- package/dist/entities/feature.entity.js +48 -48
- package/dist/entities/module-route.entity.d.ts +9 -9
- package/dist/entities/module-route.entity.js +48 -48
- package/dist/entities/module-screen-permission.entity.d.ts +15 -15
- package/dist/entities/module-screen-permission.entity.js +44 -44
- package/dist/entities/module.entity.d.ts +8 -8
- package/dist/entities/module.entity.js +44 -44
- package/dist/entities/role.entity.d.ts +7 -7
- package/dist/entities/role.entity.js +44 -44
- package/dist/entities/user-feature-access.entity.d.ts +19 -19
- package/dist/entities/user-feature-access.entity.js +88 -88
- package/dist/entities/user-module-access.entity.d.ts +12 -12
- package/dist/entities/user-module-access.entity.js +60 -60
- package/dist/entities/user.entity.d.ts +25 -24
- package/dist/entities/user.entity.js +112 -108
- package/dist/guards/feature.guard.d.ts +7 -7
- package/dist/guards/feature.guard.js +34 -34
- package/dist/guards/module.guard.d.ts +8 -8
- package/dist/guards/module.guard.js +36 -36
- package/dist/guards/permission.guard.d.ts +7 -7
- package/dist/guards/permission.guard.js +41 -41
- package/dist/guards/roles.guard.d.ts +7 -7
- package/dist/guards/roles.guard.js +34 -34
- package/dist/guards/route.guard.d.ts +7 -7
- package/dist/guards/route.guard.js +34 -34
- package/dist/index.d.ts +23 -23
- package/dist/index.js +45 -45
- package/dist/interfaces/auth-core-options.interface.d.ts +30 -30
- package/dist/interfaces/auth-core-options.interface.js +2 -2
- package/dist/jwt/jwt.guard.d.ts +4 -4
- package/dist/jwt/jwt.guard.js +18 -18
- package/dist/jwt/jwt.strategy.d.ts +14 -14
- package/dist/jwt/jwt.strategy.js +42 -42
- package/package.json +47 -47
package/README.md
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
# ECRS Auth Core
|
|
2
|
+
|
|
3
|
+
A centralized authentication and authorization module for NestJS applications, providing JWT-based authentication, role-based access control, and feature-level permissions.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔐 JWT-based authentication
|
|
8
|
+
- 👥 Role-based access control (RBAC)
|
|
9
|
+
- 🎯 Feature-level permissions
|
|
10
|
+
- 🛡️ Route-level security guards
|
|
11
|
+
- 📊 Module and screen permissions
|
|
12
|
+
- 🔧 Easy integration with TypeORM
|
|
13
|
+
- 📦 Fully typed with TypeScript
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install ecrs-auth-core
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Peer Dependencies
|
|
22
|
+
|
|
23
|
+
Make sure you have the following peer dependencies installed:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @nestjs/common @nestjs/core @nestjs/passport @nestjs/typeorm bcrypt passport passport-jwt typeorm
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
### 1. Import the Auth Module
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { Module } from '@nestjs/common';
|
|
35
|
+
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
36
|
+
import { AuthCoreModule } from 'ecrs-auth-core';
|
|
37
|
+
|
|
38
|
+
@Module({
|
|
39
|
+
imports: [
|
|
40
|
+
TypeOrmModule.forRoot({
|
|
41
|
+
// your database configuration
|
|
42
|
+
}),
|
|
43
|
+
AuthCoreModule.forRoot({
|
|
44
|
+
jwtSecret: 'your-jwt-secret',
|
|
45
|
+
jwtExpiresIn: '1h',
|
|
46
|
+
}),
|
|
47
|
+
],
|
|
48
|
+
})
|
|
49
|
+
export class AppModule {}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### 2. Include Entities in TypeORM
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import {
|
|
56
|
+
User,
|
|
57
|
+
Role,
|
|
58
|
+
Module as AuthModule,
|
|
59
|
+
Feature,
|
|
60
|
+
ModuleRoute,
|
|
61
|
+
UserFeatureAccess,
|
|
62
|
+
UserModuleAccess,
|
|
63
|
+
ModuleScreenPermission,
|
|
64
|
+
} from 'ecrs-auth-core';
|
|
65
|
+
|
|
66
|
+
TypeOrmModule.forRoot({
|
|
67
|
+
// ... other config
|
|
68
|
+
entities: [
|
|
69
|
+
User,
|
|
70
|
+
Role,
|
|
71
|
+
AuthModule,
|
|
72
|
+
Feature,
|
|
73
|
+
ModuleRoute,
|
|
74
|
+
UserFeatureAccess,
|
|
75
|
+
UserModuleAccess,
|
|
76
|
+
ModuleScreenPermission,
|
|
77
|
+
],
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 3. Use Guards and Decorators
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { Controller, Get, Post, UseGuards } from '@nestjs/common';
|
|
85
|
+
import {
|
|
86
|
+
JwtAuthGuard,
|
|
87
|
+
RolesGuard,
|
|
88
|
+
FeatureGuard,
|
|
89
|
+
Roles,
|
|
90
|
+
Feature,
|
|
91
|
+
CurrentUser,
|
|
92
|
+
User
|
|
93
|
+
} from 'ecrs-auth-core';
|
|
94
|
+
|
|
95
|
+
@Controller('protected')
|
|
96
|
+
@UseGuards(JwtAuthGuard)
|
|
97
|
+
export class ProtectedController {
|
|
98
|
+
|
|
99
|
+
@Get('admin-only')
|
|
100
|
+
@UseGuards(RolesGuard)
|
|
101
|
+
@Roles('admin')
|
|
102
|
+
adminOnlyEndpoint(@CurrentUser() user: User) {
|
|
103
|
+
return { message: 'Admin access granted', user: user.username };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
@Get('feature-protected')
|
|
107
|
+
@UseGuards(FeatureGuard)
|
|
108
|
+
@Feature('user-management')
|
|
109
|
+
featureProtectedEndpoint(@CurrentUser() user: User) {
|
|
110
|
+
return { message: 'Feature access granted' };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Available Decorators
|
|
116
|
+
|
|
117
|
+
### @CurrentUser()
|
|
118
|
+
Get the current authenticated user in your controller methods.
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
@Get('profile')
|
|
122
|
+
getProfile(@CurrentUser() user: User) {
|
|
123
|
+
return user;
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
### @Roles()
|
|
128
|
+
Restrict access based on user roles.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
@Roles('admin', 'manager')
|
|
132
|
+
@UseGuards(RolesGuard)
|
|
133
|
+
adminEndpoint() {
|
|
134
|
+
// Only admin and manager roles can access
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### @Feature()
|
|
139
|
+
Restrict access based on feature permissions.
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
@Feature('user-management')
|
|
143
|
+
@UseGuards(FeatureGuard)
|
|
144
|
+
userManagementEndpoint() {
|
|
145
|
+
// Only users with user-management feature access
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### @HasPermission()
|
|
150
|
+
Check for specific permissions.
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
@HasPermission('CREATE_USER')
|
|
154
|
+
@UseGuards(PermissionGuard)
|
|
155
|
+
createUserEndpoint() {
|
|
156
|
+
// Only users with CREATE_USER permission
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### @RoutePermission()
|
|
161
|
+
Route-level permission checking.
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
@RoutePermission('users', 'create')
|
|
165
|
+
@UseGuards(RouteGuard)
|
|
166
|
+
createUser() {
|
|
167
|
+
// Route-specific permission checking
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Available Guards
|
|
172
|
+
|
|
173
|
+
- **JwtAuthGuard**: JWT token validation
|
|
174
|
+
- **RolesGuard**: Role-based access control
|
|
175
|
+
- **FeatureGuard**: Feature-based access control
|
|
176
|
+
- **PermissionGuard**: Permission-based access control
|
|
177
|
+
- **RouteGuard**: Route-level access control
|
|
178
|
+
- **ModuleGuard**: Module-based access control
|
|
179
|
+
|
|
180
|
+
## Authentication Service
|
|
181
|
+
|
|
182
|
+
The `AuthService` provides methods for user authentication and token management:
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
import { AuthService } from 'ecrs-auth-core';
|
|
186
|
+
|
|
187
|
+
@Injectable()
|
|
188
|
+
export class MyService {
|
|
189
|
+
constructor(private authService: AuthService) {}
|
|
190
|
+
|
|
191
|
+
async login(username: string, password: string) {
|
|
192
|
+
return this.authService.validateUser(username, password);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async generateToken(user: User) {
|
|
196
|
+
return this.authService.generateToken(user);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Database Entities
|
|
202
|
+
|
|
203
|
+
The package includes the following TypeORM entities:
|
|
204
|
+
|
|
205
|
+
- **User**: User account information
|
|
206
|
+
- **Role**: User roles (admin, user, etc.)
|
|
207
|
+
- **Module**: Application modules
|
|
208
|
+
- **Feature**: Feature definitions
|
|
209
|
+
- **ModuleRoute**: Module route mappings
|
|
210
|
+
- **UserFeatureAccess**: User-feature access permissions
|
|
211
|
+
- **UserModuleAccess**: User-module access permissions
|
|
212
|
+
- **ModuleScreenPermission**: Screen-level permissions
|
|
213
|
+
|
|
214
|
+
## Configuration Options
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
interface AuthCoreOptions {
|
|
218
|
+
jwtSecret: string;
|
|
219
|
+
jwtExpiresIn?: string;
|
|
220
|
+
bcryptRounds?: number;
|
|
221
|
+
// ... other options
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## Examples
|
|
226
|
+
|
|
227
|
+
### Basic Setup with Custom Configuration
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
AuthCoreModule.forRoot({
|
|
231
|
+
jwtSecret: process.env.JWT_SECRET,
|
|
232
|
+
jwtExpiresIn: '24h',
|
|
233
|
+
bcryptRounds: 12,
|
|
234
|
+
})
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Using Multiple Guards
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
@UseGuards(JwtAuthGuard, RolesGuard, FeatureGuard)
|
|
241
|
+
@Roles('admin')
|
|
242
|
+
@Feature('advanced-settings')
|
|
243
|
+
@Get('advanced-admin')
|
|
244
|
+
advancedAdminEndpoint() {
|
|
245
|
+
return { message: 'Multi-level security passed' };
|
|
246
|
+
}
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## License
|
|
250
|
+
|
|
251
|
+
MIT
|
|
252
|
+
|
|
253
|
+
## Author
|
|
254
|
+
|
|
255
|
+
Chetan Yadnik
|
|
256
|
+
|
|
257
|
+
## Contributing
|
|
258
|
+
|
|
259
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
260
|
+
|
|
261
|
+
## Support
|
|
262
|
+
|
|
263
|
+
For questions and support, please open an issue on GitHub.
|
|
@@ -1,19 +1,26 @@
|
|
|
1
|
-
import { AuthService } from './auth.service';
|
|
2
|
-
import { LoginDto } from './dtos/login.dto';
|
|
3
|
-
export declare class AuthController {
|
|
4
|
-
private readonly authService;
|
|
5
|
-
constructor(authService: AuthService);
|
|
6
|
-
login(body: LoginDto): Promise<{
|
|
7
|
-
status: boolean;
|
|
8
|
-
message: string;
|
|
9
|
-
data: {
|
|
10
|
-
user: {
|
|
11
|
-
id: number;
|
|
12
|
-
email: string;
|
|
13
|
-
roleId: number;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
import { AuthService } from './auth.service';
|
|
2
|
+
import { LoginDto } from './dtos/login.dto';
|
|
3
|
+
export declare class AuthController {
|
|
4
|
+
private readonly authService;
|
|
5
|
+
constructor(authService: AuthService);
|
|
6
|
+
login(body: LoginDto): Promise<{
|
|
7
|
+
status: boolean;
|
|
8
|
+
message: string;
|
|
9
|
+
data: {
|
|
10
|
+
user: {
|
|
11
|
+
id: number;
|
|
12
|
+
email: string;
|
|
13
|
+
roleId: number;
|
|
14
|
+
roleName: string | null;
|
|
15
|
+
moduleId: number;
|
|
16
|
+
name: string;
|
|
17
|
+
firstName: string;
|
|
18
|
+
lastName: string;
|
|
19
|
+
mobileNo: number;
|
|
20
|
+
userImage: string;
|
|
21
|
+
employeeId: number;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
access_token: string;
|
|
25
|
+
}>;
|
|
26
|
+
}
|
package/dist/auth.controller.js
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.AuthController = void 0;
|
|
16
|
-
const common_1 = require("@nestjs/common");
|
|
17
|
-
const auth_service_1 = require("./auth.service");
|
|
18
|
-
const login_dto_1 = require("./dtos/login.dto");
|
|
19
|
-
let AuthController = class AuthController {
|
|
20
|
-
constructor(authService) {
|
|
21
|
-
this.authService = authService;
|
|
22
|
-
}
|
|
23
|
-
async login(body) {
|
|
24
|
-
const user = await this.authService.validateUser(body.email, body.password);
|
|
25
|
-
if (!user)
|
|
26
|
-
throw new common_1.UnauthorizedException('Invalid credentials');
|
|
27
|
-
return this.authService.login(user);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
exports.AuthController = AuthController;
|
|
31
|
-
__decorate([
|
|
32
|
-
(0, common_1.Post)('login'),
|
|
33
|
-
__param(0, (0, common_1.Body)()),
|
|
34
|
-
__metadata("design:type", Function),
|
|
35
|
-
__metadata("design:paramtypes", [login_dto_1.LoginDto]),
|
|
36
|
-
__metadata("design:returntype", Promise)
|
|
37
|
-
], AuthController.prototype, "login", null);
|
|
38
|
-
exports.AuthController = AuthController = __decorate([
|
|
39
|
-
(0, common_1.Controller)('auth'),
|
|
40
|
-
__metadata("design:paramtypes", [auth_service_1.AuthService])
|
|
41
|
-
], AuthController);
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.AuthController = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const auth_service_1 = require("./auth.service");
|
|
18
|
+
const login_dto_1 = require("./dtos/login.dto");
|
|
19
|
+
let AuthController = class AuthController {
|
|
20
|
+
constructor(authService) {
|
|
21
|
+
this.authService = authService;
|
|
22
|
+
}
|
|
23
|
+
async login(body) {
|
|
24
|
+
const user = await this.authService.validateUser(body.email, body.password);
|
|
25
|
+
if (!user)
|
|
26
|
+
throw new common_1.UnauthorizedException('Invalid credentials');
|
|
27
|
+
return this.authService.login(user);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.AuthController = AuthController;
|
|
31
|
+
__decorate([
|
|
32
|
+
(0, common_1.Post)('login'),
|
|
33
|
+
__param(0, (0, common_1.Body)()),
|
|
34
|
+
__metadata("design:type", Function),
|
|
35
|
+
__metadata("design:paramtypes", [login_dto_1.LoginDto]),
|
|
36
|
+
__metadata("design:returntype", Promise)
|
|
37
|
+
], AuthController.prototype, "login", null);
|
|
38
|
+
exports.AuthController = AuthController = __decorate([
|
|
39
|
+
(0, common_1.Controller)('auth'),
|
|
40
|
+
__metadata("design:paramtypes", [auth_service_1.AuthService])
|
|
41
|
+
], AuthController);
|
package/dist/auth.module.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { DynamicModule } from '@nestjs/common';
|
|
2
|
-
import { AuthCoreOptions } from './interfaces/auth-core-options.interface';
|
|
3
|
-
export declare const AUTH_CORE_OPTIONS = "AUTH_CORE_OPTIONS";
|
|
4
|
-
export declare class AuthCoreModule {
|
|
5
|
-
static registerAsync(options: {
|
|
6
|
-
inject: any[];
|
|
7
|
-
useFactory: (...args: any[]) => Promise<AuthCoreOptions>;
|
|
8
|
-
}): DynamicModule;
|
|
9
|
-
}
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { AuthCoreOptions } from './interfaces/auth-core-options.interface';
|
|
3
|
+
export declare const AUTH_CORE_OPTIONS = "AUTH_CORE_OPTIONS";
|
|
4
|
+
export declare class AuthCoreModule {
|
|
5
|
+
static registerAsync(options: {
|
|
6
|
+
inject: any[];
|
|
7
|
+
useFactory: (...args: any[]) => Promise<AuthCoreOptions>;
|
|
8
|
+
}): DynamicModule;
|
|
9
|
+
}
|