@ackplus/nest-auth 1.1.22 → 1.1.23
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/package.json +1 -1
- package/src/lib/auth/auth.module.d.ts.map +1 -1
- package/src/lib/auth/auth.module.js +2 -0
- package/src/lib/auth/guards/auth.guard.d.ts.map +1 -1
- package/src/lib/auth/guards/auth.guard.js +24 -1
- package/src/lib/auth/services/auth-session-event-listener.service.d.ts +16 -0
- package/src/lib/auth/services/auth-session-event-listener.service.d.ts.map +1 -0
- package/src/lib/auth/services/auth-session-event-listener.service.js +52 -0
- package/src/lib/auth/services/auth.service.d.ts.map +1 -1
- package/src/lib/auth/services/auth.service.js +37 -27
- package/src/lib/core/services/auth-config.service.d.ts.map +1 -1
- package/src/lib/core/services/auth-config.service.js +3 -0
- package/src/lib/user/services/user.service.d.ts.map +1 -1
- package/src/lib/user/services/user.service.js +25 -6
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.module.d.ts","sourceRoot":"","sources":["../../../../../../packages/nest-auth/src/lib/auth/auth.module.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"auth.module.d.ts","sourceRoot":"","sources":["../../../../../../packages/nest-auth/src/lib/auth/auth.module.ts"],"names":[],"mappings":"AAwBA,qBAoCa,UAAU;CACtB"}
|
|
@@ -11,6 +11,7 @@ const auth_guard_1 = require("./guards/auth.guard");
|
|
|
11
11
|
const refresh_token_interceptor_1 = require("./interceptors/refresh-token.interceptor");
|
|
12
12
|
const auth_controller_1 = require("./controllers/auth.controller");
|
|
13
13
|
const mfa_controller_1 = require("./controllers/mfa.controller");
|
|
14
|
+
const auth_session_event_listener_service_1 = require("./services/auth-session-event-listener.service");
|
|
14
15
|
const identity_entity_1 = require("../user/entities/identity.entity");
|
|
15
16
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
16
17
|
const user_entity_1 = require("../user/entities/user.entity");
|
|
@@ -52,6 +53,7 @@ exports.AuthModule = AuthModule = tslib_1.__decorate([
|
|
|
52
53
|
client_config_service_1.ClientConfigService,
|
|
53
54
|
auth_guard_1.NestAuthAuthGuard,
|
|
54
55
|
refresh_token_interceptor_1.RefreshTokenInterceptor,
|
|
56
|
+
auth_session_event_listener_service_1.AuthSessionEventListener,
|
|
55
57
|
],
|
|
56
58
|
controllers: [auth_controller_1.AuthController, mfa_controller_1.MfaController],
|
|
57
59
|
exports: [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.guard.d.ts","sourceRoot":"","sources":["../../../../../../../packages/nest-auth/src/lib/auth/guards/auth.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,WAAW,EAAE,gBAAgB,
|
|
1
|
+
{"version":3,"file":"auth.guard.d.ts","sourceRoot":"","sources":["../../../../../../../packages/nest-auth/src/lib/auth/guards/auth.guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,WAAW,EAAE,gBAAgB,EAA0D,MAAM,gBAAgB,CAAC;AAEnI,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,gDAAgD,CAAC;AACvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,wCAAwC,CAAC;AAK1E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAG5E,eAAO,MAAM,iBAAiB,kBAAkB,CAAC;AAEjD;;;;;;;;;;;;;GAaG;AACH,qBACa,iBAAkB,YAAW,WAAW;IAE7C,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,UAAU;IAClB,OAAO,CAAC,cAAc;IACtB,OAAO,CAAC,gBAAgB;IACxB,OAAO,CAAC,iBAAiB;gBAJjB,SAAS,EAAE,SAAS,EACpB,UAAU,EAAE,UAAU,EACtB,cAAc,EAAE,qBAAqB,EACrC,gBAAgB,EAAE,gBAAgB,EAClC,iBAAiB,EAAE,iBAAiB;IAG1C,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;YAyFhD,aAAa;YAmGb,gBAAgB;YAqDhB,QAAQ;IA0BtB;;OAEG;YACW,kBAAkB;IAqChC;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAc9B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAcxB;;OAEG;IACH;;OAEG;IACH;;OAEG;YACW,gBAAgB;IAoB9B;;OAEG;YACW,UAAU;IAsBxB;;OAEG;IACH;;OAEG;IACH;;OAEG;YACW,gBAAgB;IAuC9B;;OAEG;IACH,OAAO,CAAC,kBAAkB;CAiB7B"}
|
|
@@ -114,7 +114,7 @@ let NestAuthAuthGuard = class NestAuthAuthGuard {
|
|
|
114
114
|
return false;
|
|
115
115
|
}
|
|
116
116
|
// After successful authentication, check authorization (roles, permissions)
|
|
117
|
-
// Only check authorization if user is authenticated
|
|
117
|
+
// Only check authorization if user is authenticated and we have user data
|
|
118
118
|
if (isAuthenticated && request.user) {
|
|
119
119
|
await this.checkAuthorization(context, request);
|
|
120
120
|
}
|
|
@@ -164,6 +164,20 @@ let NestAuthAuthGuard = class NestAuthAuthGuard {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
request.session = session;
|
|
167
|
+
// Check if user is active
|
|
168
|
+
if (session.user && session.user.isActive === false) {
|
|
169
|
+
if (isOptional) {
|
|
170
|
+
request.user = null;
|
|
171
|
+
request.authType = null;
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
throw new common_1.UnauthorizedException({
|
|
176
|
+
message: 'User is not active',
|
|
177
|
+
code: auth_constants_1.ERROR_CODES.ACCOUNT_INACTIVE
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}
|
|
167
181
|
// Check MFA requirements
|
|
168
182
|
await this.checkMfa(context, payload, isOptional);
|
|
169
183
|
// Apply guards.afterAuth hook if configured
|
|
@@ -186,6 +200,10 @@ let NestAuthAuthGuard = class NestAuthAuthGuard {
|
|
|
186
200
|
return false;
|
|
187
201
|
}
|
|
188
202
|
else {
|
|
203
|
+
// If it's already an HttpException (like UnauthorizedException from our checks), rethrow it
|
|
204
|
+
if (error instanceof common_1.UnauthorizedException || error.status) {
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
189
207
|
throw new common_1.UnauthorizedException({
|
|
190
208
|
message: 'Invalid or expired token',
|
|
191
209
|
code: auth_constants_1.ERROR_CODES.INVALID_TOKEN
|
|
@@ -286,6 +304,11 @@ let NestAuthAuthGuard = class NestAuthAuthGuard {
|
|
|
286
304
|
code: auth_constants_1.ERROR_CODES.UNAUTHORIZED,
|
|
287
305
|
});
|
|
288
306
|
}
|
|
307
|
+
// If isFetchUser=false, we might not have roles in the user object (which is just payload)
|
|
308
|
+
// Unless roles are in payload.
|
|
309
|
+
// Assuming roles are NOT in payload by default unless configured.
|
|
310
|
+
// If we need roles check but have no roles/data, we must throw or fetch.
|
|
311
|
+
// For now, if defaults are used, user is just payload.
|
|
289
312
|
// Check roles if required
|
|
290
313
|
if (requiredRoles.length > 0) {
|
|
291
314
|
await this.checkRoles(user, requiredRoles);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { UserUpdatedEvent } from '../../user/events/user-updated.event';
|
|
2
|
+
import { UserDeletedEvent } from '../../user/events/user-deleted.event';
|
|
3
|
+
import { SessionManagerService } from '../../session/services/session-manager.service';
|
|
4
|
+
import { DebugLoggerService } from '../../core/services/debug-logger.service';
|
|
5
|
+
/**
|
|
6
|
+
* Listens to user events and manages session validity.
|
|
7
|
+
* For example, revokes sessions when a user is deactivated or deleted.
|
|
8
|
+
*/
|
|
9
|
+
export declare class AuthSessionEventListener {
|
|
10
|
+
private readonly sessionManager;
|
|
11
|
+
private readonly debugLogger;
|
|
12
|
+
constructor(sessionManager: SessionManagerService, debugLogger: DebugLoggerService);
|
|
13
|
+
handleUserUpdated(event: UserUpdatedEvent): Promise<void>;
|
|
14
|
+
handleUserDeleted(event: UserDeletedEvent): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=auth-session-event-listener.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-session-event-listener.service.d.ts","sourceRoot":"","sources":["../../../../../../../packages/nest-auth/src/lib/auth/services/auth-session-event-listener.service.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AACxE,OAAO,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AACxE,OAAO,EAAE,qBAAqB,EAAE,MAAM,gDAAgD,CAAC;AACvF,OAAO,EAAE,kBAAkB,EAAE,MAAM,0CAA0C,CAAC;AAE9E;;;GAGG;AACH,qBACa,wBAAwB;IAE7B,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,WAAW;gBADX,cAAc,EAAE,qBAAqB,EACrC,WAAW,EAAE,kBAAkB;IAI9C,iBAAiB,CAAC,KAAK,EAAE,gBAAgB;IAWzC,iBAAiB,CAAC,KAAK,EAAE,gBAAgB;CAKlD"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AuthSessionEventListener = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const common_1 = require("@nestjs/common");
|
|
6
|
+
const event_emitter_1 = require("@nestjs/event-emitter");
|
|
7
|
+
const auth_constants_1 = require("../../auth.constants");
|
|
8
|
+
const user_updated_event_1 = require("../../user/events/user-updated.event");
|
|
9
|
+
const user_deleted_event_1 = require("../../user/events/user-deleted.event");
|
|
10
|
+
const session_manager_service_1 = require("../../session/services/session-manager.service");
|
|
11
|
+
const debug_logger_service_1 = require("../../core/services/debug-logger.service");
|
|
12
|
+
/**
|
|
13
|
+
* Listens to user events and manages session validity.
|
|
14
|
+
* For example, revokes sessions when a user is deactivated or deleted.
|
|
15
|
+
*/
|
|
16
|
+
let AuthSessionEventListener = class AuthSessionEventListener {
|
|
17
|
+
constructor(sessionManager, debugLogger) {
|
|
18
|
+
this.sessionManager = sessionManager;
|
|
19
|
+
this.debugLogger = debugLogger;
|
|
20
|
+
}
|
|
21
|
+
async handleUserUpdated(event) {
|
|
22
|
+
const { user, updatedFields } = event.payload;
|
|
23
|
+
// If isActive status changed to false, revoke all sessions
|
|
24
|
+
if (updatedFields.includes('isActive') && user.isActive === false) {
|
|
25
|
+
this.debugLogger.info('User deactivated. Revoking all sessions.', 'AuthSessionEventListener', { userId: user.id });
|
|
26
|
+
await this.sessionManager.revokeAllUserSessions(user.id);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async handleUserDeleted(event) {
|
|
30
|
+
const { user } = event.payload;
|
|
31
|
+
this.debugLogger.info('User deleted. Revoking all sessions.', 'AuthSessionEventListener', { userId: user.id });
|
|
32
|
+
await this.sessionManager.revokeAllUserSessions(user.id);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
exports.AuthSessionEventListener = AuthSessionEventListener;
|
|
36
|
+
tslib_1.__decorate([
|
|
37
|
+
(0, event_emitter_1.OnEvent)(auth_constants_1.NestAuthEvents.USER_UPDATED),
|
|
38
|
+
tslib_1.__metadata("design:type", Function),
|
|
39
|
+
tslib_1.__metadata("design:paramtypes", [user_updated_event_1.UserUpdatedEvent]),
|
|
40
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
41
|
+
], AuthSessionEventListener.prototype, "handleUserUpdated", null);
|
|
42
|
+
tslib_1.__decorate([
|
|
43
|
+
(0, event_emitter_1.OnEvent)(auth_constants_1.NestAuthEvents.USER_DELETED),
|
|
44
|
+
tslib_1.__metadata("design:type", Function),
|
|
45
|
+
tslib_1.__metadata("design:paramtypes", [user_deleted_event_1.UserDeletedEvent]),
|
|
46
|
+
tslib_1.__metadata("design:returntype", Promise)
|
|
47
|
+
], AuthSessionEventListener.prototype, "handleUserDeleted", null);
|
|
48
|
+
exports.AuthSessionEventListener = AuthSessionEventListener = tslib_1.__decorate([
|
|
49
|
+
(0, common_1.Injectable)(),
|
|
50
|
+
tslib_1.__metadata("design:paramtypes", [session_manager_service_1.SessionManagerService,
|
|
51
|
+
debug_logger_service_1.DebugLoggerService])
|
|
52
|
+
], AuthSessionEventListener);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.service.d.ts","sourceRoot":"","sources":["../../../../../../../packages/nest-auth/src/lib/auth/services/auth.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAU7D,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gDAAgD,CAAC;AAEvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAC;AAC7E,OAAO,EACH,aAAa,EACb,UAAU,EACb,MAAM,6CAA6C,CAAC;AAErD,OAAO,EAAE,wBAAwB,EAAE,MAAM,6CAA6C,CAAC;AAEvF,OAAO,EAAE,uBAAuB,EAAE,MAAM,4CAA4C,CAAC;AAUrF,OAAO,EAAE,2BAA2B,EAAE,MAAM,oDAAoD,CAAC;AACjG,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AACrE,OAAO,EA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iB,EAAE,iBAAiB,EAEpC,WAAW,EAAE,WAAW;IAM7C,IAAI,SAAS,IAAI,UAAU,CAE1B;IAED,8BAA8B,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,GAAE,MAAM,EAAO,GAAG,OAAO,CAAC,YAAY,CAAC;IAUzF,OAAO;IAgBP,MAAM,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"auth.service.d.ts","sourceRoot":"","sources":["../../../../../../../packages/nest-auth/src/lib/auth/services/auth.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAU7D,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,gDAAgD,CAAC;AAEvF,OAAO,EAAE,gBAAgB,EAAE,MAAM,oCAAoC,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,oCAAoC,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,wCAAwC,CAAC;AAC7E,OAAO,EACH,aAAa,EACb,UAAU,EACb,MAAM,6CAA6C,CAAC;AAErD,OAAO,EAAE,wBAAwB,EAAE,MAAM,6CAA6C,CAAC;AAEvF,OAAO,EAAE,uBAAuB,EAAE,MAAM,4CAA4C,CAAC;AAUrF,OAAO,EAAE,2BAA2B,EAAE,MAAM,oDAAoD,CAAC;AACjG,OAAO,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AACrE,OAAO,EAAE,kBAAkB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UIRED,
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
97
|
+
const providersToLink = [];
|
|
98
|
+
if (email && config.emailAuth?.enabled !== false) {
|
|
99
|
+
const provider = this.authProviderRegistry.getProvider(auth_constants_1.EMAIL_AUTH_PROVIDER);
|
|
100
|
+
if (provider) {
|
|
101
|
+
providersToLink.push({ provider, userId: email, type: 'email' });
|
|
102
|
+
}
|
|
102
103
|
}
|
|
103
|
-
|
|
104
|
-
provider = this.authProviderRegistry.getProvider(auth_constants_1.PHONE_AUTH_PROVIDER);
|
|
105
|
-
|
|
104
|
+
if (phone && config.phoneAuth?.enabled === true) {
|
|
105
|
+
const provider = this.authProviderRegistry.getProvider(auth_constants_1.PHONE_AUTH_PROVIDER);
|
|
106
|
+
if (provider) {
|
|
107
|
+
providersToLink.push({ provider, userId: phone, type: 'phone' });
|
|
108
|
+
}
|
|
106
109
|
}
|
|
107
|
-
if (
|
|
110
|
+
if (providersToLink.length === 0) {
|
|
108
111
|
this.debugLogger.error('Provider not found for signup', 'AuthService', { email: !!email, phone: !!phone });
|
|
109
112
|
throw new common_1.InternalServerErrorException({
|
|
110
113
|
message: 'Phone or email authentication is not enabled',
|
|
111
114
|
code: auth_constants_1.ERROR_CODES.PROVIDER_NOT_FOUND,
|
|
112
115
|
});
|
|
113
116
|
}
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
117
|
+
// Check for existing identities across all providers
|
|
118
|
+
for (const item of providersToLink) {
|
|
119
|
+
this.debugLogger.debug('Checking for existing identity', 'AuthService', { providerUserId: item.userId, type: item.type });
|
|
120
|
+
const identity = await item.provider.findIdentity(item.userId);
|
|
121
|
+
if (identity) {
|
|
122
|
+
this.debugLogger.warn('Identity already exists', 'AuthService', { email: !!email, phone: !!phone, tenantId });
|
|
123
|
+
if (item.type === 'email') {
|
|
124
|
+
throw new common_1.BadRequestException({
|
|
125
|
+
message: 'Email already exists in this tenant',
|
|
126
|
+
code: auth_constants_1.ERROR_CODES.EMAIL_ALREADY_EXISTS,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
if (item.type === 'phone') {
|
|
130
|
+
throw new common_1.BadRequestException({
|
|
131
|
+
message: 'Phone number already exists in this tenant',
|
|
132
|
+
code: auth_constants_1.ERROR_CODES.PHONE_ALREADY_EXISTS,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
129
135
|
}
|
|
130
136
|
}
|
|
131
137
|
this.debugLogger.debug('Creating new user via UserService', 'AuthService', { email: !!email, phone: !!phone, tenantId });
|
|
@@ -140,9 +146,12 @@ let AuthService = class AuthService {
|
|
|
140
146
|
}, input);
|
|
141
147
|
this.debugLogger.info('User created successfully', 'AuthService', { userId: user.id, tenantId });
|
|
142
148
|
user = await this.getUserWithRolesAndPermissions(user.id);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
149
|
+
// Link user to all enabled providers
|
|
150
|
+
for (const item of providersToLink) {
|
|
151
|
+
this.debugLogger.debug('Linking user to provider', 'AuthService', { userId: user.id, providerName: item.provider.providerName });
|
|
152
|
+
// Note: UserService might have already created the identity, but we ensure it's linked here
|
|
153
|
+
await item.provider.linkToUser(user.id, item.userId);
|
|
154
|
+
}
|
|
146
155
|
this.debugLogger.debug('Creating session for new user', 'AuthService', { userId: user.id });
|
|
147
156
|
const session = await this.sessionManager.createSessionFromUser(user);
|
|
148
157
|
const tokens = await this.generateTokensFromSession(session);
|
|
@@ -150,6 +159,7 @@ let AuthService = class AuthService {
|
|
|
150
159
|
this.debugLogger.debug('Signup tokens generated', 'AuthService', { userId: user.id, isRequiresMfa });
|
|
151
160
|
// Emit registration event
|
|
152
161
|
this.debugLogger.debug('Emitting user registration event', 'AuthService', { userId: user.id });
|
|
162
|
+
const provider = providersToLink[0]?.provider;
|
|
153
163
|
await this.eventEmitter.emitAsync(auth_constants_1.NestAuthEvents.REGISTERED, new user_registered_event_1.UserRegisteredEvent({
|
|
154
164
|
user,
|
|
155
165
|
tenantId: user.tenantId,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-config.service.d.ts","sourceRoot":"","sources":["../../../../../../../packages/nest-auth/src/lib/core/services/auth-config.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AAIhF,qBACa,iBAAiB;IAC1B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAoB;IAC3C,OAAO,CAAC,MAAM,CAAC,OAAO,CAAoB;IAE1C;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,cAAc,
|
|
1
|
+
{"version":3,"file":"auth-config.service.d.ts","sourceRoot":"","sources":["../../../../../../../packages/nest-auth/src/lib/core/services/auth-config.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AAIhF,qBACa,iBAAiB;IAC1B,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAoB;IAC3C,OAAO,CAAC,MAAM,CAAC,OAAO,CAAoB;IAE1C;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,cAAc,CAoD3B;;IASF,MAAM,CAAC,WAAW,IAAI,iBAAiB;IAOvC;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,iBAAiB;IAIhC;;;;;;;;;;OAUG;IACH,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAqB/B,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI;IAyBnD;;OAEG;IACH,OAAO,CAAC,MAAM,CAAC,2BAA2B;IAiB1C,MAAM,CAAC,UAAU,IAAI,iBAAiB;IAOtC,MAAM,CAAC,iBAAiB,IAAI,iBAAiB;IAI7C,SAAS,IAAI,iBAAiB;IAI9B,SAAS,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI;CAG9C"}
|
|
@@ -132,6 +132,9 @@ AuthConfigService.defaultOptions = {
|
|
|
132
132
|
emailAuth: {
|
|
133
133
|
enabled: true,
|
|
134
134
|
},
|
|
135
|
+
phoneAuth: {
|
|
136
|
+
enabled: false,
|
|
137
|
+
},
|
|
135
138
|
mfa: {
|
|
136
139
|
enabled: false,
|
|
137
140
|
methods: [mfa_options_interface_1.MFAMethodEnum.EMAIL, mfa_options_interface_1.MFAMethodEnum.TOTP],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"user.service.d.ts","sourceRoot":"","sources":["../../../../../../../packages/nest-auth/src/lib/user/services/user.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAe,UAAU,EAAE,MAAM,SAAS,CAAC;AACnF,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAKtD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,0CAA0C,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAE5E,qBACa,WAAW;IAGhB,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,aAAa;IAC9B,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,iBAAiB;IAClC,OAAO,CAAC,QAAQ,CAAC,WAAW;gBAJX,cAAc,EAAE,UAAU,CAAC,YAAY,CAAC,EACxC,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,aAAa,EAC3B,iBAAiB,EAAE,iBAAiB,EACpC,WAAW,EAAE,kBAAkB;IAG9C,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,YAA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AE,MAAM,CAAC,CAAC;IAI5F,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;CAajF"}
|
|
@@ -67,10 +67,12 @@ let UserService = class UserService {
|
|
|
67
67
|
this.debugLogger.info('User created successfully', 'UserService', { userId: user.id });
|
|
68
68
|
// Create identities
|
|
69
69
|
const normalizedEmail = email?.toLowerCase().trim();
|
|
70
|
-
if (normalizedEmail)
|
|
70
|
+
if (normalizedEmail && config.emailAuth?.enabled !== false) {
|
|
71
71
|
await user.findOrCreateIdentity(auth_constants_1.EMAIL_AUTH_PROVIDER, normalizedEmail);
|
|
72
|
-
|
|
72
|
+
}
|
|
73
|
+
if (phone && config.phoneAuth?.enabled === true) {
|
|
73
74
|
await user.findOrCreateIdentity(auth_constants_1.PHONE_AUTH_PROVIDER, phone);
|
|
75
|
+
}
|
|
74
76
|
// Emit user created event
|
|
75
77
|
this.debugLogger.debug('Emitting user created event', 'UserService', { userId: user.id });
|
|
76
78
|
await this.eventEmitter.emitAsync(auth_constants_1.NestAuthEvents.USER_CREATED, new user_created_event_1.UserCreatedEvent({
|
|
@@ -209,11 +211,12 @@ let UserService = class UserService {
|
|
|
209
211
|
Object.assign(user, data);
|
|
210
212
|
const updatedUser = await this.userRepository.save(user);
|
|
211
213
|
this.debugLogger.info('User updated successfully', 'UserService', { userId: updatedUser.id });
|
|
212
|
-
|
|
214
|
+
const config = this.authConfigService.getConfig();
|
|
215
|
+
if (data.email && config.emailAuth?.enabled !== false) {
|
|
213
216
|
this.debugLogger.debug('Updating email identity', 'UserService', { userId: id });
|
|
214
217
|
await user.updateOrCreateIdentity(auth_constants_1.EMAIL_AUTH_PROVIDER, { providerId: data.email });
|
|
215
218
|
}
|
|
216
|
-
if (data.phone) {
|
|
219
|
+
if (data.phone && config.phoneAuth?.enabled === true) {
|
|
217
220
|
this.debugLogger.debug('Updating phone identity', 'UserService', { userId: id });
|
|
218
221
|
await user.updateOrCreateIdentity(auth_constants_1.PHONE_AUTH_PROVIDER, { providerId: data.phone });
|
|
219
222
|
}
|
|
@@ -304,7 +307,15 @@ let UserService = class UserService {
|
|
|
304
307
|
});
|
|
305
308
|
}
|
|
306
309
|
user.isActive = isActive;
|
|
307
|
-
|
|
310
|
+
const updatedUser = await this.userRepository.save(user);
|
|
311
|
+
// Emit user updated event
|
|
312
|
+
this.debugLogger.debug('Emitting user updated event (status change)', 'UserService', { userId: id, isActive });
|
|
313
|
+
await this.eventEmitter.emitAsync(auth_constants_1.NestAuthEvents.USER_UPDATED, new user_updated_event_1.UserUpdatedEvent({
|
|
314
|
+
user: updatedUser,
|
|
315
|
+
tenantId: updatedUser.tenantId,
|
|
316
|
+
updatedFields: ['isActive']
|
|
317
|
+
}));
|
|
318
|
+
return updatedUser;
|
|
308
319
|
}
|
|
309
320
|
async updateUserMetadata(id, metadata) {
|
|
310
321
|
const user = await this.getUserById(id);
|
|
@@ -318,7 +329,15 @@ let UserService = class UserService {
|
|
|
318
329
|
...user.metadata,
|
|
319
330
|
...metadata
|
|
320
331
|
};
|
|
321
|
-
|
|
332
|
+
const updatedUser = await this.userRepository.save(user);
|
|
333
|
+
// Emit user updated event
|
|
334
|
+
this.debugLogger.debug('Emitting user updated event (metadata)', 'UserService', { userId: id });
|
|
335
|
+
await this.eventEmitter.emitAsync(auth_constants_1.NestAuthEvents.USER_UPDATED, new user_updated_event_1.UserUpdatedEvent({
|
|
336
|
+
user: updatedUser,
|
|
337
|
+
tenantId: updatedUser.tenantId,
|
|
338
|
+
updatedFields: ['metadata']
|
|
339
|
+
}));
|
|
340
|
+
return updatedUser;
|
|
322
341
|
}
|
|
323
342
|
async countUsers(options) {
|
|
324
343
|
return this.userRepository.count(options);
|