@nest-omni/core 1.0.29 → 1.0.30
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.
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
export interface IOmniAuth {
|
|
2
|
-
uid:
|
|
2
|
+
uid: string;
|
|
3
3
|
username: string;
|
|
4
4
|
tenantId: string;
|
|
5
5
|
appId: string;
|
|
6
|
+
admin: boolean;
|
|
7
|
+
role: string;
|
|
6
8
|
}
|
|
7
|
-
export declare const OmniAuth: (...dataOrPipes: (
|
|
8
|
-
export declare function getOmniAuthData
|
|
9
|
+
export declare const OmniAuth: (...dataOrPipes: (import("@nestjs/common").PipeTransform<any, any> | import("@nestjs/common").Type<import("@nestjs/common").PipeTransform<any, any>> | keyof IOmniAuth)[]) => ParameterDecorator;
|
|
10
|
+
export declare function getOmniAuthData<T extends keyof IOmniAuth>(request: {
|
|
11
|
+
headers: Record<string, string>;
|
|
12
|
+
}, key?: T): IOmniAuth[T] | IOmniAuth;
|
|
@@ -2,34 +2,40 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getOmniAuthData = exports.OmniAuth = void 0;
|
|
4
4
|
const common_1 = require("@nestjs/common");
|
|
5
|
-
const
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
6
|
const config_1 = require("@nestjs/config");
|
|
7
|
-
exports.OmniAuth = (0, common_1.createParamDecorator)((
|
|
8
|
-
return getOmniAuthData(ctx.switchToHttp().getRequest(),
|
|
7
|
+
exports.OmniAuth = (0, common_1.createParamDecorator)((key, ctx) => {
|
|
8
|
+
return getOmniAuthData(ctx.switchToHttp().getRequest(), key);
|
|
9
9
|
});
|
|
10
|
-
function getOmniAuthData(
|
|
11
|
-
const headers =
|
|
10
|
+
function getOmniAuthData(request, key) {
|
|
11
|
+
const headers = request.headers;
|
|
12
12
|
let user;
|
|
13
|
-
const configService = new config_1.ConfigService(
|
|
14
|
-
if (
|
|
15
|
-
|
|
16
|
-
if (process.env.NODE_ENV === 'dev' || process.env.NODE_ENV === 'uat') {
|
|
13
|
+
const configService = new config_1.ConfigService(request);
|
|
14
|
+
if ((0, lodash_1.isEmpty)(headers['x-userid']) || (0, lodash_1.isEmpty)(headers['x-username'])) {
|
|
15
|
+
if (process.env.NODE_ENV === 'dev') {
|
|
17
16
|
user = {
|
|
18
17
|
uid: configService.get('MOCK_UID'),
|
|
19
18
|
username: configService.get('MOCK_USERNAME'),
|
|
20
19
|
appId: configService.get('MOCK_APP_ID'),
|
|
21
20
|
tenantId: configService.get('MOCK_TENANT_ID'),
|
|
21
|
+
admin: configService.get('MOCK_ADMIN', false),
|
|
22
|
+
role: configService.get('MOCK_ROLE', ''),
|
|
22
23
|
};
|
|
23
24
|
}
|
|
25
|
+
else {
|
|
26
|
+
throw new Error('User information not found in headers.');
|
|
27
|
+
}
|
|
24
28
|
}
|
|
25
29
|
else {
|
|
26
30
|
user = {
|
|
27
|
-
uid: headers['x-userid'],
|
|
28
|
-
username: headers['x-username'],
|
|
29
|
-
appId: headers['x-appid'],
|
|
30
|
-
tenantId: headers['x-tenantid'],
|
|
31
|
+
uid: headers['x-userid'] || '',
|
|
32
|
+
username: headers['x-username'] || '',
|
|
33
|
+
appId: headers['x-appid'] || '',
|
|
34
|
+
tenantId: headers['x-tenantid'] || '',
|
|
35
|
+
admin: headers['x-admin'] === '1',
|
|
36
|
+
role: headers['x-role'] || '',
|
|
31
37
|
};
|
|
32
38
|
}
|
|
33
|
-
return
|
|
39
|
+
return key ? user[key] : user;
|
|
34
40
|
}
|
|
35
41
|
exports.getOmniAuthData = getOmniAuthData;
|
package/package.json
CHANGED
|
@@ -28,5 +28,7 @@ export declare class ContextProvider {
|
|
|
28
28
|
static getAuthUserField<T extends keyof IOmniAuth>(field: T): IOmniAuth[T];
|
|
29
29
|
static getAuthUser(): IOmniAuth;
|
|
30
30
|
static getAppId(): string;
|
|
31
|
+
static getRole(): string;
|
|
32
|
+
static getAdmin(): boolean;
|
|
31
33
|
static getTenantId(): string;
|
|
32
34
|
}
|
|
@@ -59,6 +59,12 @@ class ContextProvider {
|
|
|
59
59
|
static getAppId() {
|
|
60
60
|
return ContextProvider.getAuthUserField('appId');
|
|
61
61
|
}
|
|
62
|
+
static getRole() {
|
|
63
|
+
return ContextProvider.getAuthUserField('role');
|
|
64
|
+
}
|
|
65
|
+
static getAdmin() {
|
|
66
|
+
return ContextProvider.getAuthUserField('admin');
|
|
67
|
+
}
|
|
62
68
|
static getTenantId() {
|
|
63
69
|
return ContextProvider.getAuthUserField('tenantId');
|
|
64
70
|
}
|