@nest-boot/auth 6.12.0 → 6.14.0
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/dist/auth.guard.d.ts +16 -2
- package/dist/auth.guard.js +57 -9
- package/dist/auth.guard.js.map +1 -1
- package/dist/auth.module.d.ts +1 -14
- package/dist/auth.module.js +1 -61
- package/dist/auth.module.js.map +1 -1
- package/dist/auth.service.d.ts +4 -5
- package/dist/auth.service.js +6 -9
- package/dist/auth.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +7 -6
package/dist/auth.guard.d.ts
CHANGED
|
@@ -1,24 +1,38 @@
|
|
|
1
|
+
import { EntityManager } from "@mikro-orm/core";
|
|
1
2
|
import { type CanActivate, type ExecutionContext } from "@nestjs/common";
|
|
2
3
|
import { Reflector } from "@nestjs/core";
|
|
4
|
+
import { AuthService } from "./auth.service";
|
|
3
5
|
import { AuthModuleOptions } from "./interfaces";
|
|
4
6
|
/**
|
|
5
7
|
* Authentication guard class used to protect routes and handle requests.
|
|
6
8
|
*/
|
|
7
9
|
export declare class AuthGuard implements CanActivate {
|
|
8
10
|
private readonly reflector;
|
|
11
|
+
private readonly em;
|
|
12
|
+
private readonly authService;
|
|
9
13
|
private readonly options;
|
|
10
14
|
private readonly defaultRequireAuth;
|
|
11
|
-
|
|
15
|
+
private readonly userEntityClass;
|
|
16
|
+
private readonly personalAccessTokenEntityClass;
|
|
17
|
+
private readonly personalAccessTokenAndUserDataLoader;
|
|
18
|
+
constructor(reflector: Reflector, em: EntityManager, authService: AuthService, options: AuthModuleOptions);
|
|
12
19
|
/**
|
|
13
20
|
* Get internationalized text based on the specified key.
|
|
14
21
|
* @param key - The key of the internationalized text.
|
|
15
22
|
* @returns The internationalized text.
|
|
16
23
|
*/
|
|
17
24
|
private t;
|
|
25
|
+
/**
|
|
26
|
+
* Extracts the personal access token from the request.
|
|
27
|
+
* @param req - The request object.
|
|
28
|
+
* @returns The personal access token, or null if it doesn't exist.
|
|
29
|
+
*/
|
|
30
|
+
private extractPersonalAccessToken;
|
|
31
|
+
private getPersonalAccessTokenAndUser;
|
|
18
32
|
/**
|
|
19
33
|
* Determines whether the request is allowed to be executed.
|
|
20
34
|
* @param executionContext - The execution context object.
|
|
21
35
|
* @returns True if the request is allowed to be executed, otherwise false.
|
|
22
36
|
*/
|
|
23
|
-
canActivate(executionContext: ExecutionContext): boolean
|
|
37
|
+
canActivate(executionContext: ExecutionContext): Promise<boolean>;
|
|
24
38
|
}
|
package/dist/auth.guard.js
CHANGED
|
@@ -16,21 +16,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
16
16
|
};
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.AuthGuard = void 0;
|
|
19
|
+
const core_1 = require("@mikro-orm/core");
|
|
19
20
|
const i18n_1 = require("@nest-boot/i18n");
|
|
20
21
|
const request_context_1 = require("@nest-boot/request-context");
|
|
21
22
|
const common_1 = require("@nestjs/common");
|
|
22
|
-
const
|
|
23
|
+
const core_2 = require("@nestjs/core");
|
|
24
|
+
const dataloader_1 = __importDefault(require("dataloader"));
|
|
23
25
|
const lodash_1 = __importDefault(require("lodash"));
|
|
24
26
|
const auth_constants_1 = require("./auth.constants");
|
|
25
27
|
const auth_module_definition_1 = require("./auth.module-definition");
|
|
28
|
+
const auth_service_1 = require("./auth.service");
|
|
29
|
+
const entities_1 = require("./entities");
|
|
26
30
|
/**
|
|
27
31
|
* Authentication guard class used to protect routes and handle requests.
|
|
28
32
|
*/
|
|
29
33
|
let AuthGuard = class AuthGuard {
|
|
30
|
-
constructor(reflector, options) {
|
|
34
|
+
constructor(reflector, em, authService, options) {
|
|
31
35
|
this.reflector = reflector;
|
|
36
|
+
this.em = em;
|
|
37
|
+
this.authService = authService;
|
|
32
38
|
this.options = options;
|
|
39
|
+
this.reflector = reflector;
|
|
33
40
|
this.defaultRequireAuth = this.options?.defaultRequireAuth ?? true;
|
|
41
|
+
this.userEntityClass = this.options.entities?.User ?? entities_1.User;
|
|
42
|
+
this.personalAccessTokenEntityClass =
|
|
43
|
+
this.options.entities?.PersonalAccessToken ?? entities_1.PersonalAccessToken;
|
|
44
|
+
this.personalAccessTokenAndUserDataLoader = new dataloader_1.default((tokens) => Promise.all(tokens.map((token) => this.getPersonalAccessTokenAndUser(token))));
|
|
34
45
|
}
|
|
35
46
|
/**
|
|
36
47
|
* Get internationalized text based on the specified key.
|
|
@@ -40,12 +51,43 @@ let AuthGuard = class AuthGuard {
|
|
|
40
51
|
t(key) {
|
|
41
52
|
return request_context_1.RequestContext.get(i18n_1.I18N)?.t(key, { ns: "auth" }) ?? key;
|
|
42
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Extracts the personal access token from the request.
|
|
56
|
+
* @param req - The request object.
|
|
57
|
+
* @returns The personal access token, or null if it doesn't exist.
|
|
58
|
+
*/
|
|
59
|
+
extractPersonalAccessToken(req) {
|
|
60
|
+
const authorizationHeader = req.get("authorization");
|
|
61
|
+
if (typeof authorizationHeader !== "undefined") {
|
|
62
|
+
const matched = authorizationHeader.match(/(\S+)\s+(\S+)/);
|
|
63
|
+
if (matched !== null && matched[1].toLowerCase() === "bearer") {
|
|
64
|
+
return matched[2];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (typeof req.cookies?.token === "string") {
|
|
68
|
+
return req.cookies.token;
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
async getPersonalAccessTokenAndUser(token) {
|
|
73
|
+
const personalAccessToken = await this.authService.getToken(token);
|
|
74
|
+
const user = await personalAccessToken?.user.load();
|
|
75
|
+
if (personalAccessToken === null || user == null) {
|
|
76
|
+
throw new common_1.UnauthorizedException(this.t("The personal access token is invalid."));
|
|
77
|
+
}
|
|
78
|
+
await this.authService.updateLastUsedAt(personalAccessToken);
|
|
79
|
+
request_context_1.RequestContext.set(this.userEntityClass, user);
|
|
80
|
+
request_context_1.RequestContext.set(this.personalAccessTokenEntityClass, personalAccessToken);
|
|
81
|
+
request_context_1.RequestContext.set(auth_constants_1.AUTH_USER, user);
|
|
82
|
+
request_context_1.RequestContext.set(auth_constants_1.AUTH_PERSONAL_ACCESS_TOKEN, personalAccessToken);
|
|
83
|
+
return { personalAccessToken, user };
|
|
84
|
+
}
|
|
43
85
|
/**
|
|
44
86
|
* Determines whether the request is allowed to be executed.
|
|
45
87
|
* @param executionContext - The execution context object.
|
|
46
88
|
* @returns True if the request is allowed to be executed, otherwise false.
|
|
47
89
|
*/
|
|
48
|
-
canActivate(executionContext) {
|
|
90
|
+
async canActivate(executionContext) {
|
|
49
91
|
if (!["http", "graphql"].includes(executionContext.getType())) {
|
|
50
92
|
return true;
|
|
51
93
|
}
|
|
@@ -56,11 +98,15 @@ let AuthGuard = class AuthGuard {
|
|
|
56
98
|
if (!(requireAuth ?? this.defaultRequireAuth ?? false)) {
|
|
57
99
|
return true;
|
|
58
100
|
}
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
101
|
+
// Get the Request object
|
|
102
|
+
const req = executionContext.switchToHttp().getRequest() ??
|
|
103
|
+
executionContext.getArgs()[2].req;
|
|
104
|
+
// Extract the token
|
|
105
|
+
const token = this.extractPersonalAccessToken(req);
|
|
106
|
+
if (token === null) {
|
|
62
107
|
throw new common_1.UnauthorizedException(this.t("The personal access token is invalid."));
|
|
63
108
|
}
|
|
109
|
+
const { user } = await this.personalAccessTokenAndUserDataLoader.load(token);
|
|
64
110
|
// Get the method permissions
|
|
65
111
|
const permissions = this.reflector.get(auth_constants_1.PERMISSIONS_METADATA_KEY, executionContext.getHandler());
|
|
66
112
|
// If there are no permission requirements, allow access directly
|
|
@@ -74,8 +120,10 @@ let AuthGuard = class AuthGuard {
|
|
|
74
120
|
};
|
|
75
121
|
exports.AuthGuard = AuthGuard;
|
|
76
122
|
exports.AuthGuard = AuthGuard = __decorate([
|
|
77
|
-
(0, common_1.Injectable)(),
|
|
78
|
-
__param(
|
|
79
|
-
__metadata("design:paramtypes", [
|
|
123
|
+
(0, common_1.Injectable)({ scope: common_1.Scope.REQUEST }),
|
|
124
|
+
__param(3, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
|
|
125
|
+
__metadata("design:paramtypes", [core_2.Reflector,
|
|
126
|
+
core_1.EntityManager,
|
|
127
|
+
auth_service_1.AuthService, Object])
|
|
80
128
|
], AuthGuard);
|
|
81
129
|
//# sourceMappingURL=auth.guard.js.map
|
package/dist/auth.guard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../src/auth.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,0CAAkD;AAClD,gEAA4D;AAC5D,
|
|
1
|
+
{"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../src/auth.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAAA,0CAA6D;AAC7D,0CAAkD;AAClD,gEAA4D;AAC5D,2CASwB;AACxB,uCAAyC;AACzC,4DAAoC;AAEpC,oDAAuB;AAEvB,qDAK0B;AAC1B,qEAAgE;AAChE,iDAA6C;AAC7C,yCAAuD;AAGvD;;GAEG;AAEI,IAAM,SAAS,GAAf,MAAM,SAAS;IAWpB,YACmB,SAAoB,EACpB,EAAiB,EACjB,WAAwB,EAExB,OAA0B;QAJ1B,cAAS,GAAT,SAAS,CAAW;QACpB,OAAE,GAAF,EAAE,CAAe;QACjB,gBAAW,GAAX,WAAW,CAAa;QAExB,YAAO,GAAP,OAAO,CAAmB;QAE3C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,OAAO,EAAE,kBAAkB,IAAI,IAAI,CAAC;QAEnE,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,IAAI,eAAI,CAAC;QAC3D,IAAI,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AY,EAAE,CAAC,UAAU,EAAW;YACrD,gBAAgB,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC;QAEpC,oBAAoB;QACpB,MAAM,KAAK,GAAG,IAAI,CAAC,0BAA0B,CAAC,GAAG,CAAC,CAAC;QAEnD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,MAAM,IAAI,8BAAqB,CAC7B,IAAI,CAAC,CAAC,CAAC,uCAAuC,CAAC,CAChD,CAAC;QACJ,CAAC;QAED,MAAM,EAAE,IAAI,EAAE,GACZ,MAAM,IAAI,CAAC,oCAAoC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAE9D,6BAA6B;QAC7B,MAAM,WAAW,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CACpC,yCAAwB,EACxB,gBAAgB,CAAC,UAAU,EAAE,CAC9B,CAAC;QAEF,iEAAiE;QACjE,6GAA6G;QAC7G,IACE,OAAO,WAAW,KAAK,WAAW;YAClC,gBAAC,CAAC,YAAY,CAAC,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC,MAAM,GAAG,CAAC,EACxD,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,IAAI,2BAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC,CAAC;IAC7D,CAAC;CACF,CAAA;AAvJY,8BAAS;oBAAT,SAAS;IADrB,IAAA,mBAAU,EAAC,EAAE,KAAK,EAAE,cAAK,CAAC,OAAO,EAAE,CAAC;IAgBhC,WAAA,IAAA,eAAM,EAAC,6CAAoB,CAAC,CAAA;qCAHD,gBAAS;QAChB,oBAAa;QACJ,0BAAW;GAdhC,SAAS,CAuJrB"}
|
package/dist/auth.module.d.ts
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
import { OnModuleInit } from "@nestjs/common";
|
|
2
1
|
import { ConfigurableModuleClass } from "./auth.module-definition";
|
|
3
|
-
|
|
4
|
-
import { AuthModuleOptions } from "./interfaces";
|
|
5
|
-
export declare class AuthModule extends ConfigurableModuleClass implements OnModuleInit {
|
|
6
|
-
private readonly authService;
|
|
7
|
-
private readonly options;
|
|
8
|
-
constructor(authService: AuthService, options: AuthModuleOptions);
|
|
9
|
-
onModuleInit(): void;
|
|
10
|
-
/**
|
|
11
|
-
* Extracts the personal access token from the request.
|
|
12
|
-
* @param req - The request object.
|
|
13
|
-
* @returns The personal access token, or null if it doesn't exist.
|
|
14
|
-
*/
|
|
15
|
-
private extractPersonalAccessToken;
|
|
2
|
+
export declare class AuthModule extends ConfigurableModuleClass {
|
|
16
3
|
}
|
package/dist/auth.module.js
CHANGED
|
@@ -5,73 +5,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
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
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
9
|
exports.AuthModule = void 0;
|
|
16
10
|
const request_context_1 = require("@nest-boot/request-context");
|
|
17
11
|
const common_1 = require("@nestjs/common");
|
|
18
12
|
const core_1 = require("@nestjs/core");
|
|
19
|
-
const auth_constants_1 = require("./auth.constants");
|
|
20
13
|
const auth_guard_1 = require("./auth.guard");
|
|
21
14
|
const auth_module_definition_1 = require("./auth.module-definition");
|
|
22
15
|
const auth_service_1 = require("./auth.service");
|
|
23
|
-
const entities_1 = require("./entities");
|
|
24
16
|
let AuthModule = class AuthModule extends auth_module_definition_1.ConfigurableModuleClass {
|
|
25
|
-
constructor(authService, options) {
|
|
26
|
-
super();
|
|
27
|
-
this.authService = authService;
|
|
28
|
-
this.options = options;
|
|
29
|
-
this.defaultRequireAuth = this.options?.defaultRequireAuth ?? true;
|
|
30
|
-
this.userEntityClass = this.options.entities?.User ?? entities_1.User;
|
|
31
|
-
this.personalAccessTokenEntityClass =
|
|
32
|
-
this.options.entities?.PersonalAccessToken ?? entities_1.PersonalAccessToken;
|
|
33
|
-
}
|
|
34
|
-
onModuleInit() {
|
|
35
|
-
request_context_1.RequestContext.registerMiddleware("auth", async (ctx, next) => {
|
|
36
|
-
if (ctx.type === "http") {
|
|
37
|
-
const req = ctx.get(request_context_1.REQUEST);
|
|
38
|
-
if (req) {
|
|
39
|
-
const token = this.extractPersonalAccessToken(req);
|
|
40
|
-
if (token) {
|
|
41
|
-
const personalAccessToken = await this.authService.getToken(token);
|
|
42
|
-
const user = await personalAccessToken?.user.load();
|
|
43
|
-
if (personalAccessToken && user) {
|
|
44
|
-
request_context_1.RequestContext.set(this.userEntityClass, user);
|
|
45
|
-
request_context_1.RequestContext.set(this
|
|
46
|
-
.personalAccessTokenEntityClass, personalAccessToken);
|
|
47
|
-
request_context_1.RequestContext.set(auth_constants_1.AUTH_USER, user);
|
|
48
|
-
request_context_1.RequestContext.set(auth_constants_1.AUTH_PERSONAL_ACCESS_TOKEN, personalAccessToken);
|
|
49
|
-
await this.authService.updateLastUsedAt(personalAccessToken);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return await next();
|
|
55
|
-
}, ["database"]);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Extracts the personal access token from the request.
|
|
59
|
-
* @param req - The request object.
|
|
60
|
-
* @returns The personal access token, or null if it doesn't exist.
|
|
61
|
-
*/
|
|
62
|
-
extractPersonalAccessToken(req) {
|
|
63
|
-
const authorizationHeader = req.get("authorization");
|
|
64
|
-
if (typeof authorizationHeader !== "undefined") {
|
|
65
|
-
const matched = authorizationHeader.match(/(\S+)\s+(\S+)/);
|
|
66
|
-
if (matched !== null && matched[1].toLowerCase() === "bearer") {
|
|
67
|
-
return matched[2];
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
if (typeof req.cookies?.token === "string") {
|
|
71
|
-
return req.cookies.token;
|
|
72
|
-
}
|
|
73
|
-
return null;
|
|
74
|
-
}
|
|
75
17
|
};
|
|
76
18
|
exports.AuthModule = AuthModule;
|
|
77
19
|
exports.AuthModule = AuthModule = __decorate([
|
|
@@ -86,8 +28,6 @@ exports.AuthModule = AuthModule = __decorate([
|
|
|
86
28
|
core_1.Reflector,
|
|
87
29
|
],
|
|
88
30
|
exports: [auth_service_1.AuthService],
|
|
89
|
-
})
|
|
90
|
-
__param(1, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
|
|
91
|
-
__metadata("design:paramtypes", [auth_service_1.AuthService, Object])
|
|
31
|
+
})
|
|
92
32
|
], AuthModule);
|
|
93
33
|
//# sourceMappingURL=auth.module.js.map
|
package/dist/auth.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.module.js","sourceRoot":"","sources":["../src/auth.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth.module.js","sourceRoot":"","sources":["../src/auth.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,gEAAkE;AAClE,2CAAwC;AACxC,uCAAoD;AAEpD,6CAAyC;AACzC,qEAAmE;AACnE,iDAA6C;AActC,IAAM,UAAU,GAAhB,MAAM,UAAW,SAAQ,gDAAuB;CAAG,CAAA;AAA7C,gCAAU;qBAAV,UAAU;IAZtB,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,sCAAoB,CAAC;QAC/B,SAAS,EAAE;YACT;gBACE,OAAO,EAAE,gBAAS;gBAClB,QAAQ,EAAE,sBAAS;aACpB;YACD,0BAAW;YACX,gBAAS;SACV;QACD,OAAO,EAAE,CAAC,0BAAW,CAAC;KACvB,CAAC;GACW,UAAU,CAAmC"}
|
package/dist/auth.service.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { EntityManager } from "@mikro-orm/core";
|
|
2
2
|
import { HashService } from "@nest-boot/hash";
|
|
3
3
|
import { PersonalAccessToken, User } from "./entities";
|
|
4
4
|
import { AuthModuleOptions } from "./interfaces";
|
|
@@ -6,10 +6,9 @@ import { AuthModuleOptions } from "./interfaces";
|
|
|
6
6
|
* Service responsible for handling authentication-related operations.
|
|
7
7
|
*/
|
|
8
8
|
export declare class AuthService {
|
|
9
|
-
private readonly
|
|
9
|
+
private readonly em;
|
|
10
10
|
private readonly hashService;
|
|
11
11
|
private readonly options;
|
|
12
|
-
private readonly em;
|
|
13
12
|
private readonly User;
|
|
14
13
|
private readonly PersonalAccessToken;
|
|
15
14
|
private readonly expiresIn?;
|
|
@@ -19,7 +18,7 @@ export declare class AuthService {
|
|
|
19
18
|
* @param hashService The HashService instance.
|
|
20
19
|
* @param options The options for the Auth module.
|
|
21
20
|
*/
|
|
22
|
-
constructor(
|
|
21
|
+
constructor(em: EntityManager, hashService: HashService, options: AuthModuleOptions);
|
|
23
22
|
/**
|
|
24
23
|
* Attempts to authenticate a user with the provided email and password.
|
|
25
24
|
* @param email The user's email.
|
|
@@ -60,5 +59,5 @@ export declare class AuthService {
|
|
|
60
59
|
* @returns The registered user.
|
|
61
60
|
*/
|
|
62
61
|
register(name: string, email: string, password: string, permissions?: string[]): Promise<User>;
|
|
63
|
-
updateLastUsedAt(personalAccessToken: PersonalAccessToken
|
|
62
|
+
updateLastUsedAt(personalAccessToken: PersonalAccessToken): Promise<void>;
|
|
64
63
|
}
|
package/dist/auth.service.js
CHANGED
|
@@ -29,11 +29,10 @@ let AuthService = class AuthService {
|
|
|
29
29
|
* @param hashService The HashService instance.
|
|
30
30
|
* @param options The options for the Auth module.
|
|
31
31
|
*/
|
|
32
|
-
constructor(
|
|
33
|
-
this.
|
|
32
|
+
constructor(em, hashService, options) {
|
|
33
|
+
this.em = em;
|
|
34
34
|
this.hashService = hashService;
|
|
35
35
|
this.options = options;
|
|
36
|
-
this.em = this.orm.em;
|
|
37
36
|
this.User = this.options?.entities?.User ?? entities_1.User;
|
|
38
37
|
this.PersonalAccessToken =
|
|
39
38
|
this.options?.entities?.PersonalAccessToken ?? entities_1.PersonalAccessToken;
|
|
@@ -64,7 +63,7 @@ let AuthService = class AuthService {
|
|
|
64
63
|
async getToken(token) {
|
|
65
64
|
return await this.em.findOne(this.PersonalAccessToken, {
|
|
66
65
|
token,
|
|
67
|
-
}, {
|
|
66
|
+
}, { populate: ["user"] });
|
|
68
67
|
}
|
|
69
68
|
/**
|
|
70
69
|
* Creates a new personal access token for a user.
|
|
@@ -118,18 +117,16 @@ let AuthService = class AuthService {
|
|
|
118
117
|
await this.em.persistAndFlush(user);
|
|
119
118
|
return user;
|
|
120
119
|
}
|
|
121
|
-
async updateLastUsedAt(personalAccessToken
|
|
120
|
+
async updateLastUsedAt(personalAccessToken) {
|
|
122
121
|
personalAccessToken.lastUsedAt = new Date();
|
|
123
|
-
|
|
124
|
-
await this.em.flush();
|
|
125
|
-
}
|
|
122
|
+
await this.em.flush();
|
|
126
123
|
}
|
|
127
124
|
};
|
|
128
125
|
exports.AuthService = AuthService;
|
|
129
126
|
exports.AuthService = AuthService = __decorate([
|
|
130
127
|
(0, common_1.Injectable)(),
|
|
131
128
|
__param(2, (0, common_1.Inject)(auth_module_definition_1.MODULE_OPTIONS_TOKEN)),
|
|
132
|
-
__metadata("design:paramtypes", [core_1.
|
|
129
|
+
__metadata("design:paramtypes", [core_1.EntityManager,
|
|
133
130
|
hash_1.HashService, Object])
|
|
134
131
|
], AuthService);
|
|
135
132
|
//# sourceMappingURL=auth.service.js.map
|
package/dist/auth.service.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.service.js","sourceRoot":"","sources":["../src/auth.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"auth.service.js","sourceRoot":"","sources":["../src/auth.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,0CAA6D;AAC7D,0CAA8C;AAC9C,2CAAoD;AAEpD,qEAAgE;AAChE,yCAAuD;AAEvD,mEAA0D;AAE1D;;GAEG;AAEI,IAAM,WAAW,GAAjB,MAAM,WAAW;IAMtB;;;;;OAKG;IACH,YACmB,EAAiB,EACjB,WAAwB,EAExB,OAA0B;QAH1B,OAAE,GAAF,EAAE,CAAe;QACjB,gBAAW,GAAX,WAAW,CAAa;QAExB,YAAO,GAAP,OAAO,CAAmB;QAE3C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,IAAI,IAAI,eAAI,CAAC;QACjD,IAAI,CAAC,mBAAmB;YACtB,IAAI,CAAC,OAAO,EAAE,QAAQ,EAAE,mBAAmB,IAAI,8BAAmB,CAAC;QAErE,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC;IAC3C,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,OAAO,CAAC,KAAa,EAAE,QAAgB;QAC3C,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,OAAO,CAChC,IAAI,CAAC,IAAI,EACT,EAAE,KAAK,EAAE,EACT,EAAE,OAAO,EAAE,KAAK,EAAE,CACnB,CAAC;QAEF;QACE,oEAAoE;QACpE,IAAI,KAAK,IAAI;YACb,IAAI,CAAC,QAAQ,KAAK,IAAI;YACtB,CAAC,CAAC,MAAM,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,EACzD,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;OAI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