@hichchi/nest-auth 0.0.2 → 0.0.3
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/CHANGELOG.md +16 -8
- package/README.md +782 -1567
- package/auth.module.js +50 -47
- package/auth.module.js.map +1 -1
- package/constants.js +7 -4
- package/constants.js.map +1 -1
- package/controllers/auth.controller.js +148 -145
- package/controllers/auth.controller.js.map +1 -1
- package/controllers/index.js +4 -1
- package/controllers/index.js.map +1 -1
- package/decorators/auth-info.decorator.js +6 -3
- package/decorators/auth-info.decorator.js.map +1 -1
- package/decorators/current-user.decorator.js +6 -3
- package/decorators/current-user.decorator.js.map +1 -1
- package/decorators/index.js +9 -6
- package/decorators/index.js.map +1 -1
- package/decorators/permission.decorator.js +8 -4
- package/decorators/permission.decorator.js.map +1 -1
- package/decorators/roles.decorator.js +8 -4
- package/decorators/roles.decorator.js.map +1 -1
- package/decorators/socket-id.decorator.js +6 -3
- package/decorators/socket-id.decorator.js.map +1 -1
- package/decorators/subdomain.decorator.js +11 -8
- package/decorators/subdomain.decorator.js.map +1 -1
- package/dtos/email-verify.dto.js +17 -14
- package/dtos/email-verify.dto.js.map +1 -1
- package/dtos/get-auth-response.dto.js +13 -10
- package/dtos/get-auth-response.dto.js.map +1 -1
- package/dtos/index.js +14 -11
- package/dtos/index.js.map +1 -1
- package/dtos/refresh-token.dto.js +13 -10
- package/dtos/refresh-token.dto.js.map +1 -1
- package/dtos/request-reset.dto.js +12 -9
- package/dtos/request-reset.dto.js.map +1 -1
- package/dtos/resend-email-verify.dto.js +12 -9
- package/dtos/resend-email-verify.dto.js.map +1 -1
- package/dtos/reset-password-token-verify.dto.js +13 -10
- package/dtos/reset-password-token-verify.dto.js.map +1 -1
- package/dtos/reset-password.dto.js +16 -13
- package/dtos/reset-password.dto.js.map +1 -1
- package/dtos/sign-in.dto.js +21 -18
- package/dtos/sign-in.dto.js.map +1 -1
- package/dtos/sign-up.dto.js +26 -23
- package/dtos/sign-up.dto.js.map +1 -1
- package/dtos/update-password.dto.js +15 -12
- package/dtos/update-password.dto.js.map +1 -1
- package/dtos/view-user.dto.js +5 -1
- package/dtos/view-user.dto.js.map +1 -1
- package/extractors/cookie-extractor.js +6 -3
- package/extractors/cookie-extractor.js.map +1 -1
- package/extractors/index.js +4 -1
- package/extractors/index.js.map +1 -1
- package/guards/google-auth.guard.js +24 -21
- package/guards/google-auth.guard.js.map +1 -1
- package/guards/index.js +8 -5
- package/guards/index.js.map +1 -1
- package/guards/jwt-auth.guard.js +43 -40
- package/guards/jwt-auth.guard.js.map +1 -1
- package/guards/local-auth.guard.js +15 -12
- package/guards/local-auth.guard.js.map +1 -1
- package/guards/permission.guard.js +15 -12
- package/guards/permission.guard.js.map +1 -1
- package/guards/role.guard.js +15 -12
- package/guards/role.guard.js.map +1 -1
- package/index.js +18 -13
- package/index.js.map +1 -1
- package/interfaces/auth-options.interface.js +2 -1
- package/interfaces/auth-user.type.js +2 -1
- package/interfaces/cache-user.interfaces.js +2 -1
- package/interfaces/index.js +9 -6
- package/interfaces/index.js.map +1 -1
- package/interfaces/token-data.interface.js +2 -1
- package/interfaces/user-extra.interfaces.js +2 -1
- package/interfaces/user-service.interface.d.ts +1 -1
- package/interfaces/user-service.interface.js +2 -1
- package/package.json +6 -5
- package/pipes/index.js +4 -1
- package/pipes/index.js.map +1 -1
- package/pipes/override-sign-up-dto.pipe.js +16 -13
- package/pipes/override-sign-up-dto.pipe.js.map +1 -1
- package/providers/index.js +4 -1
- package/providers/index.js.map +1 -1
- package/providers/user-service.provider.js +2 -1
- package/readme-top.md +24 -24
- package/services/auth.service.js +142 -136
- package/services/auth.service.js.map +1 -1
- package/services/encryption.service.js +18 -15
- package/services/encryption.service.js.map +1 -1
- package/services/index.js +6 -3
- package/services/index.js.map +1 -1
- package/services/jwt-token.service.js +14 -11
- package/services/jwt-token.service.js.map +1 -1
- package/services/token-verify.service.js +10 -7
- package/services/token-verify.service.js.map +1 -1
- package/services/user-cache.service.js +16 -13
- package/services/user-cache.service.js.map +1 -1
- package/strategies/google.strategy.js +16 -13
- package/strategies/google.strategy.js.map +1 -1
- package/strategies/index.js +6 -3
- package/strategies/index.js.map +1 -1
- package/strategies/jwt.strategy.js +29 -26
- package/strategies/jwt.strategy.js.map +1 -1
- package/strategies/local.strategy.js +20 -17
- package/strategies/local.strategy.js.map +1 -1
- package/tokens.js +5 -2
- package/tokens.js.map +1 -1
- package/utils/config-validation.js +22 -17
- package/utils/config-validation.js.map +1 -1
- package/utils/generate-auth-user.js +7 -4
- package/utils/generate-auth-user.js.map +1 -1
- package/utils/index.js +5 -2
- package/utils/index.js.map +1 -1
package/services/auth.service.js
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
// noinspection JSUnusedGlobalSymbols,ExceptionCaughtLocallyJS
|
|
2
3
|
var AuthService_1;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.AuthService = void 0;
|
|
6
|
+
const tslib_1 = require("tslib");
|
|
7
|
+
const common_1 = require("@nestjs/common");
|
|
8
|
+
const bcrypt_1 = require("bcrypt");
|
|
9
|
+
const jwt_1 = require("@nestjs/jwt");
|
|
10
|
+
const nest_core_1 = require("@hichchi/nest-core");
|
|
11
|
+
const tokens_1 = require("../tokens");
|
|
12
|
+
const user_cache_service_1 = require("./user-cache.service");
|
|
13
|
+
const jwt_token_service_1 = require("./jwt-token.service");
|
|
14
|
+
const uuid_1 = require("uuid");
|
|
15
|
+
const token_verify_service_1 = require("./token-verify.service");
|
|
16
|
+
const utils_1 = require("../utils");
|
|
17
|
+
const crypto_1 = require("crypto");
|
|
18
|
+
const auth_1 = require("@hichchi/nest-connector/auth");
|
|
19
|
+
const nest_connector_1 = require("@hichchi/nest-connector");
|
|
20
|
+
const constants_1 = require("../constants");
|
|
18
21
|
/**
|
|
19
22
|
* Core authentication service that provides comprehensive identity management functionality.
|
|
20
23
|
*
|
|
@@ -115,8 +118,8 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
115
118
|
* @see {@link DEFAULT_VERIFY_TOKEN_LENGTH} Default length constant for verification tokens
|
|
116
119
|
* @see {@link VerifyToken} Type representing verification tokens
|
|
117
120
|
*/
|
|
118
|
-
static generateVerifyToken(length = DEFAULT_VERIFY_TOKEN_LENGTH) {
|
|
119
|
-
return randomBytes(length).toString("hex");
|
|
121
|
+
static generateVerifyToken(length = nest_connector_1.DEFAULT_VERIFY_TOKEN_LENGTH) {
|
|
122
|
+
return (0, crypto_1.randomBytes)(length).toString("hex");
|
|
120
123
|
}
|
|
121
124
|
/**
|
|
122
125
|
* Generates a cryptographically secure random password with strong complexity requirements.
|
|
@@ -159,7 +162,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
159
162
|
const symbols = "!@#$%&*";
|
|
160
163
|
const allCharacters = uppercase + lowercase + numbers + symbols;
|
|
161
164
|
const getRandomSecureIndex = (max) => {
|
|
162
|
-
return randomInt(0, max);
|
|
165
|
+
return (0, crypto_1.randomInt)(0, max);
|
|
163
166
|
};
|
|
164
167
|
let password = "";
|
|
165
168
|
password += uppercase[getRandomSecureIndex(uppercase.length)];
|
|
@@ -211,7 +214,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
211
214
|
* @see {@link verifyHash} Companion method for verifying passwords against hashes
|
|
212
215
|
*/
|
|
213
216
|
static generateHash(password) {
|
|
214
|
-
return hashSync(password, DEFAULT_SALT_ROUNDS);
|
|
217
|
+
return (0, bcrypt_1.hashSync)(password, nest_connector_1.DEFAULT_SALT_ROUNDS);
|
|
215
218
|
}
|
|
216
219
|
/**
|
|
217
220
|
* Securely verifies a plain text password against a bcrypt hash.
|
|
@@ -248,7 +251,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
248
251
|
* @see {@link generateHash} Companion method for generating password hashes
|
|
249
252
|
*/
|
|
250
253
|
static verifyHash(password, hash) {
|
|
251
|
-
return compareSync(password, hash);
|
|
254
|
+
return (0, bcrypt_1.compareSync)(password, hash);
|
|
252
255
|
}
|
|
253
256
|
/**
|
|
254
257
|
* Authenticate a user with username/email and password
|
|
@@ -285,30 +288,30 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
285
288
|
* @see {@link LocalStrategy} Strategy that uses this method to authenticate users
|
|
286
289
|
*/
|
|
287
290
|
async authenticate(request, username, password, subdomain) {
|
|
288
|
-
const INVALID_CREDS = this.options.authField === AuthField.EMAIL
|
|
289
|
-
? AuthErrors.AUTH_401_INVALID_EMAIL_PASSWORD
|
|
290
|
-
: AuthErrors.AUTH_401_INVALID_USERNAME_PASSWORD;
|
|
291
|
+
const INVALID_CREDS = this.options.authField === auth_1.AuthField.EMAIL
|
|
292
|
+
? auth_1.AuthErrors.AUTH_401_INVALID_EMAIL_PASSWORD
|
|
293
|
+
: auth_1.AuthErrors.AUTH_401_INVALID_USERNAME_PASSWORD;
|
|
291
294
|
try {
|
|
292
|
-
const user = this.options.authField === AuthField.USERNAME && this.userService.getUserByUsername
|
|
295
|
+
const user = this.options.authField === auth_1.AuthField.USERNAME && this.userService.getUserByUsername
|
|
293
296
|
? await this.userService.getUserByUsername(username, subdomain)
|
|
294
|
-
: this.options.authField === AuthField.EMAIL
|
|
297
|
+
: this.options.authField === auth_1.AuthField.EMAIL
|
|
295
298
|
? await this.userService.getUserByEmail(username, subdomain)
|
|
296
|
-
: this.options.authField === AuthField.BOTH && this.userService.getUserByUsernameOrEmail
|
|
299
|
+
: this.options.authField === auth_1.AuthField.BOTH && this.userService.getUserByUsernameOrEmail
|
|
297
300
|
? await this.userService.getUserByUsernameOrEmail(username, subdomain)
|
|
298
301
|
: Boolean(this.options.authField) && this.userService.getUserByAuthField
|
|
299
302
|
? await this.userService.getUserByAuthField(username, subdomain)
|
|
300
303
|
: null;
|
|
301
304
|
if (!user) {
|
|
302
|
-
throw new UnauthorizedException(INVALID_CREDS);
|
|
305
|
+
throw new common_1.UnauthorizedException(INVALID_CREDS);
|
|
303
306
|
}
|
|
304
307
|
if (!user.password) {
|
|
305
|
-
throw new ForbiddenException(AuthErrors.AUTH_401_NOT_LOCAL);
|
|
308
|
+
throw new common_1.ForbiddenException(auth_1.AuthErrors.AUTH_401_NOT_LOCAL);
|
|
306
309
|
}
|
|
307
310
|
if (!AuthService_1.verifyHash(password, user.password)) {
|
|
308
|
-
throw new UnauthorizedException(INVALID_CREDS);
|
|
311
|
+
throw new common_1.UnauthorizedException(INVALID_CREDS);
|
|
309
312
|
}
|
|
310
313
|
if (this.options.checkEmailVerified && !user.emailVerified) {
|
|
311
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_EMAIL_NOT_VERIFIED);
|
|
314
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_EMAIL_NOT_VERIFIED);
|
|
312
315
|
}
|
|
313
316
|
// if (user.status === Status.PENDING) {
|
|
314
317
|
// return await Promise.reject(new ForbiddenException(AuthErrors.AUTH_403_PENDING));
|
|
@@ -318,20 +321,20 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
318
321
|
// }
|
|
319
322
|
const tokenResponse = this.generateTokens(user);
|
|
320
323
|
const cacheUser = await this.updateCacheUser(user, tokenResponse);
|
|
321
|
-
const authUser = generateAuthUser(cacheUser, tokenResponse.accessToken);
|
|
324
|
+
const authUser = (0, utils_1.generateAuthUser)(cacheUser, tokenResponse.accessToken);
|
|
322
325
|
await this.userService
|
|
323
326
|
.onAuthenticate?.(request, authUser)
|
|
324
|
-
.catch(callbackError => LoggerService.error("Error in onAuthenticate success callback:", callbackError));
|
|
327
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onAuthenticate success callback:", callbackError));
|
|
325
328
|
return authUser;
|
|
326
329
|
}
|
|
327
330
|
catch (error) {
|
|
328
331
|
await this.userService
|
|
329
332
|
.onAuthenticate?.(request, undefined, error)
|
|
330
|
-
.catch(callbackError => LoggerService.error("Error in onAuthenticate error callback:", callbackError));
|
|
331
|
-
if (!(error instanceof HttpException)) {
|
|
332
|
-
LoggerService.error(error);
|
|
333
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onAuthenticate error callback:", callbackError));
|
|
334
|
+
if (!(error instanceof common_1.HttpException)) {
|
|
335
|
+
nest_core_1.LoggerService.error(error);
|
|
333
336
|
}
|
|
334
|
-
throw new UnauthorizedException(INVALID_CREDS);
|
|
337
|
+
throw new common_1.UnauthorizedException(INVALID_CREDS);
|
|
335
338
|
}
|
|
336
339
|
}
|
|
337
340
|
/**
|
|
@@ -383,28 +386,28 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
383
386
|
if (!cacheUser ||
|
|
384
387
|
!cacheUser.sessions?.length ||
|
|
385
388
|
!cacheUser.sessions?.find(session => session.accessToken === accessToken)) {
|
|
386
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_INVALID_TOKEN);
|
|
389
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_INVALID_TOKEN);
|
|
387
390
|
}
|
|
388
|
-
const authUser = generateAuthUser(cacheUser, accessToken);
|
|
391
|
+
const authUser = (0, utils_1.generateAuthUser)(cacheUser, accessToken);
|
|
389
392
|
await this.userService
|
|
390
393
|
.onAuthenticateJWT?.(request, authUser)
|
|
391
|
-
.catch(callbackError => LoggerService.error("Error in onAuthenticateJWT success callback:", callbackError));
|
|
394
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onAuthenticateJWT success callback:", callbackError));
|
|
392
395
|
return authUser;
|
|
393
396
|
}
|
|
394
397
|
catch (error) {
|
|
395
398
|
await this.userService
|
|
396
399
|
.onAuthenticateJWT?.(request, undefined, error)
|
|
397
|
-
.catch(callbackError => LoggerService.error("Error in onAuthenticateJWT error callback:", callbackError));
|
|
398
|
-
if (error instanceof TokenExpiredError) {
|
|
399
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_EXPIRED_TOKEN);
|
|
400
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onAuthenticateJWT error callback:", callbackError));
|
|
401
|
+
if (error instanceof jwt_1.TokenExpiredError) {
|
|
402
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_EXPIRED_TOKEN);
|
|
400
403
|
}
|
|
401
|
-
else if (error instanceof JsonWebTokenError) {
|
|
402
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_INVALID_TOKEN);
|
|
404
|
+
else if (error instanceof jwt_1.JsonWebTokenError) {
|
|
405
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_INVALID_TOKEN);
|
|
403
406
|
}
|
|
404
|
-
else if (error instanceof HttpException) {
|
|
407
|
+
else if (error instanceof common_1.HttpException) {
|
|
405
408
|
throw error;
|
|
406
409
|
}
|
|
407
|
-
throw new UnauthorizedException();
|
|
410
|
+
throw new common_1.UnauthorizedException();
|
|
408
411
|
}
|
|
409
412
|
}
|
|
410
413
|
/**
|
|
@@ -452,7 +455,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
452
455
|
*/
|
|
453
456
|
async authenticateGoogle(request, profile, redirectUrl) {
|
|
454
457
|
if (!("getUserByEmail" in this.userService)) {
|
|
455
|
-
throw new InternalServerErrorException(AuthErrors.AUTH_501_NOT_IMPLEMENTED);
|
|
458
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_501_NOT_IMPLEMENTED);
|
|
456
459
|
}
|
|
457
460
|
try {
|
|
458
461
|
let user = await this.userService.getUserByEmail(profile.email);
|
|
@@ -462,25 +465,25 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
462
465
|
firstName: profile.given_name,
|
|
463
466
|
lastName: profile.family_name,
|
|
464
467
|
email: profile.email,
|
|
465
|
-
}, AuthProvider.GOOGLE, profile));
|
|
468
|
+
}, auth_1.AuthProvider.GOOGLE, profile));
|
|
466
469
|
}
|
|
467
470
|
catch (error) {
|
|
468
|
-
LoggerService.error(error);
|
|
469
|
-
throw new UnauthorizedException(AuthErrors.AUTH_500_SIGN_UP);
|
|
471
|
+
nest_core_1.LoggerService.error(error);
|
|
472
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_500_SIGN_UP);
|
|
470
473
|
}
|
|
471
474
|
}
|
|
472
475
|
const tokenResponse = this.generateTokens(user);
|
|
473
476
|
const cacheUser = await this.updateCacheUser(user, tokenResponse, undefined, redirectUrl);
|
|
474
|
-
const authUser = generateAuthUser(cacheUser, tokenResponse.accessToken);
|
|
477
|
+
const authUser = (0, utils_1.generateAuthUser)(cacheUser, tokenResponse.accessToken);
|
|
475
478
|
await this.userService
|
|
476
479
|
.onAuthenticateGoogle?.(request, authUser)
|
|
477
|
-
.catch(callbackError => LoggerService.error("Error in onAuthenticateGoogle success callback:", callbackError));
|
|
480
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onAuthenticateGoogle success callback:", callbackError));
|
|
478
481
|
return authUser;
|
|
479
482
|
}
|
|
480
483
|
catch (error) {
|
|
481
484
|
await this.userService
|
|
482
485
|
.onAuthenticateGoogle?.(request, undefined, error)
|
|
483
|
-
.catch(callbackError => LoggerService.error("Error in onAuthenticateGoogle error callback:", callbackError));
|
|
486
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onAuthenticateGoogle error callback:", callbackError));
|
|
484
487
|
throw error;
|
|
485
488
|
}
|
|
486
489
|
}
|
|
@@ -523,11 +526,11 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
523
526
|
const jwtPayload = this.jwtTokenService.verifyAccessToken(accessToken);
|
|
524
527
|
const user = await this.userService.getUserById(jwtPayload.sub);
|
|
525
528
|
if (!user) {
|
|
526
|
-
return Promise.reject(new UnauthorizedException(AuthErrors.AUTH_401_SOCIAL_SIGN_IN));
|
|
529
|
+
return Promise.reject(new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_SOCIAL_SIGN_IN));
|
|
527
530
|
}
|
|
528
531
|
const tokenResponse = this.generateTokens(user);
|
|
529
532
|
const cacheUser = await this.updateCacheUser(user, tokenResponse);
|
|
530
|
-
const authUser = generateAuthUser(cacheUser, accessToken);
|
|
533
|
+
const authUser = (0, utils_1.generateAuthUser)(cacheUser, accessToken);
|
|
531
534
|
return this.signIn(request, authUser, res);
|
|
532
535
|
}
|
|
533
536
|
/**
|
|
@@ -561,14 +564,14 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
561
564
|
}
|
|
562
565
|
await this.userService
|
|
563
566
|
.onGetUserByToken?.(request, user.id)
|
|
564
|
-
.catch(callbackError => LoggerService.error("Error in onGetUserByToken success callback:", callbackError));
|
|
567
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onGetUserByToken success callback:", callbackError));
|
|
565
568
|
return user;
|
|
566
569
|
}
|
|
567
570
|
catch (error) {
|
|
568
571
|
await this.userService
|
|
569
572
|
.onGetUserByToken?.(request, undefined, error)
|
|
570
|
-
.catch(callbackError => LoggerService.error("Error in onGetUserByToken error callback:", callbackError));
|
|
571
|
-
LoggerService.error(error);
|
|
573
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onGetUserByToken error callback:", callbackError));
|
|
574
|
+
nest_core_1.LoggerService.error(error);
|
|
572
575
|
return null;
|
|
573
576
|
}
|
|
574
577
|
}
|
|
@@ -669,7 +672,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
669
672
|
cacheUser.sessions = cacheUser.sessions.filter(session => session.refreshToken !== oldRefreshToken);
|
|
670
673
|
}
|
|
671
674
|
cacheUser.sessions.push({
|
|
672
|
-
sessionId:
|
|
675
|
+
sessionId: (0, uuid_1.v4)(),
|
|
673
676
|
accessToken: tokenResponse.accessToken,
|
|
674
677
|
refreshToken: tokenResponse.refreshToken,
|
|
675
678
|
frontendUrl,
|
|
@@ -678,7 +681,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
678
681
|
else {
|
|
679
682
|
cacheUser.sessions = [
|
|
680
683
|
{
|
|
681
|
-
sessionId:
|
|
684
|
+
sessionId: (0, uuid_1.v4)(),
|
|
682
685
|
accessToken: tokenResponse.accessToken,
|
|
683
686
|
refreshToken: tokenResponse.refreshToken,
|
|
684
687
|
frontendUrl,
|
|
@@ -729,16 +732,16 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
729
732
|
* @see {@link AuthOptions.cookies} Configuration for cookie settings
|
|
730
733
|
*/
|
|
731
734
|
setAuthCookies(response, tokenResponse) {
|
|
732
|
-
if (this.options.authMethod === AuthMethod.COOKIE && this.options.cookies) {
|
|
733
|
-
response.cookie(ACCESS_TOKEN_COOKIE_NAME, tokenResponse.accessToken, {
|
|
734
|
-
maxAge: this.options.jwt.expiresIn * SECOND_IN_MS,
|
|
735
|
+
if (this.options.authMethod === auth_1.AuthMethod.COOKIE && this.options.cookies) {
|
|
736
|
+
response.cookie(constants_1.ACCESS_TOKEN_COOKIE_NAME, tokenResponse.accessToken, {
|
|
737
|
+
maxAge: this.options.jwt.expiresIn * nest_connector_1.SECOND_IN_MS,
|
|
735
738
|
httpOnly: true,
|
|
736
739
|
sameSite: this.options.cookies.sameSite,
|
|
737
740
|
secure: this.options.cookies.secure,
|
|
738
741
|
signed: true,
|
|
739
742
|
});
|
|
740
|
-
response.cookie(REFRESH_TOKEN_COOKIE_NAME, tokenResponse.refreshToken, {
|
|
741
|
-
maxAge: this.options.jwt.refreshExpiresIn * SECOND_IN_MS,
|
|
743
|
+
response.cookie(constants_1.REFRESH_TOKEN_COOKIE_NAME, tokenResponse.refreshToken, {
|
|
744
|
+
maxAge: this.options.jwt.refreshExpiresIn * nest_connector_1.SECOND_IN_MS,
|
|
742
745
|
httpOnly: true,
|
|
743
746
|
sameSite: this.options.cookies.sameSite,
|
|
744
747
|
secure: this.options.cookies.secure,
|
|
@@ -778,21 +781,21 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
778
781
|
try {
|
|
779
782
|
const { password: rawPass, ...rest } = signUpDto;
|
|
780
783
|
const password = AuthService_1.generateHash(rawPass);
|
|
781
|
-
const user = await this.userService.signUpUser({ ...rest, password }, AuthProvider.LOCAL);
|
|
784
|
+
const user = await this.userService.signUpUser({ ...rest, password }, auth_1.AuthProvider.LOCAL);
|
|
782
785
|
if (!user) {
|
|
783
|
-
throw new InternalServerErrorException(AuthErrors.AUTH_500_SIGN_UP);
|
|
786
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_500_SIGN_UP);
|
|
784
787
|
}
|
|
785
788
|
await this.sendVerificationEmail(user);
|
|
786
789
|
user.password = null;
|
|
787
790
|
await this.userService
|
|
788
791
|
.onSignUp?.(request, user.id)
|
|
789
|
-
.catch(callbackError => LoggerService.error("Error in onSignUp success callback:", callbackError));
|
|
792
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onSignUp success callback:", callbackError));
|
|
790
793
|
return user;
|
|
791
794
|
}
|
|
792
795
|
catch (error) {
|
|
793
796
|
await this.userService
|
|
794
797
|
.onSignUp?.(request, undefined, error)
|
|
795
|
-
.catch(callbackError => LoggerService.error("Error in onSignUp error callback:", callbackError));
|
|
798
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onSignUp error callback:", callbackError));
|
|
796
799
|
throw error;
|
|
797
800
|
}
|
|
798
801
|
}
|
|
@@ -830,7 +833,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
830
833
|
try {
|
|
831
834
|
const user = await this.userService.getUserById(authUser.id);
|
|
832
835
|
if (!user) {
|
|
833
|
-
throw new NotFoundException(AuthErrors.AUTH_401_UNKNOWN);
|
|
836
|
+
throw new common_1.NotFoundException(auth_1.AuthErrors.AUTH_401_UNKNOWN);
|
|
834
837
|
}
|
|
835
838
|
const { sessionId, accessToken, refreshToken } = authUser;
|
|
836
839
|
const tokenResponse = {
|
|
@@ -842,7 +845,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
842
845
|
this.setAuthCookies(res, tokenResponse);
|
|
843
846
|
await this.userService
|
|
844
847
|
.onSignIn?.(req, authUser)
|
|
845
|
-
.catch(callbackError => LoggerService.error("Error in onSignIn success callback:", callbackError));
|
|
848
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onSignIn success callback:", callbackError));
|
|
846
849
|
return {
|
|
847
850
|
...tokenResponse,
|
|
848
851
|
sessionId,
|
|
@@ -852,7 +855,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
852
855
|
catch (error) {
|
|
853
856
|
await this.userService
|
|
854
857
|
.onSignIn?.(req, authUser, error)
|
|
855
|
-
.catch(callbackError => LoggerService.error("Error in onSignIn error callback:", callbackError));
|
|
858
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onSignIn error callback:", callbackError));
|
|
856
859
|
throw error;
|
|
857
860
|
}
|
|
858
861
|
}
|
|
@@ -885,17 +888,17 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
885
888
|
try {
|
|
886
889
|
const user = await this.userService.getUserById(authUser.id);
|
|
887
890
|
if (!user) {
|
|
888
|
-
throw new NotFoundException(AuthErrors.AUTH_401_UNKNOWN);
|
|
891
|
+
throw new common_1.NotFoundException(auth_1.AuthErrors.AUTH_401_UNKNOWN);
|
|
889
892
|
}
|
|
890
893
|
await this.userService
|
|
891
894
|
.onGetCurrentUser?.(request, authUser)
|
|
892
|
-
.catch(callbackError => LoggerService.error("Error in onGetCurrentUser success callback:", callbackError));
|
|
895
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onGetCurrentUser success callback:", callbackError));
|
|
893
896
|
return { ...user, password: null };
|
|
894
897
|
}
|
|
895
898
|
catch (error) {
|
|
896
899
|
await this.userService
|
|
897
900
|
.onGetCurrentUser?.(request, authUser, error)
|
|
898
|
-
.catch(callbackError => LoggerService.error("Error in onGetCurrentUser error callback:", callbackError));
|
|
901
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onGetCurrentUser error callback:", callbackError));
|
|
899
902
|
throw error;
|
|
900
903
|
}
|
|
901
904
|
}
|
|
@@ -941,28 +944,28 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
941
944
|
const { sub } = this.jwtTokenService.verifyRefreshToken(token);
|
|
942
945
|
const user = await this.userService.getUserById(sub);
|
|
943
946
|
if (!user) {
|
|
944
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_INVALID_REFRESH_TOKEN);
|
|
947
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_INVALID_REFRESH_TOKEN);
|
|
945
948
|
}
|
|
946
949
|
const tokenResponse = this.generateTokens(user);
|
|
947
950
|
const cacheUser = await this.updateCacheUser(user, tokenResponse, token);
|
|
948
|
-
const authUser = generateAuthUser(cacheUser, tokenResponse.accessToken);
|
|
951
|
+
const authUser = (0, utils_1.generateAuthUser)(cacheUser, tokenResponse.accessToken);
|
|
949
952
|
this.setAuthCookies(response, tokenResponse);
|
|
950
953
|
await this.userService
|
|
951
954
|
.onRefreshTokens?.(request, authUser)
|
|
952
|
-
.catch(callbackError => LoggerService.error("Error in onRefreshTokens callback:", callbackError));
|
|
955
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onRefreshTokens callback:", callbackError));
|
|
953
956
|
return tokenResponse;
|
|
954
957
|
}
|
|
955
958
|
catch (error) {
|
|
956
|
-
if (error instanceof TokenExpiredError) {
|
|
957
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_EXPIRED_REFRESH_TOKEN);
|
|
959
|
+
if (error instanceof jwt_1.TokenExpiredError) {
|
|
960
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_EXPIRED_REFRESH_TOKEN);
|
|
958
961
|
}
|
|
959
|
-
else if (error instanceof JsonWebTokenError) {
|
|
960
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_INVALID_REFRESH_TOKEN);
|
|
962
|
+
else if (error instanceof jwt_1.JsonWebTokenError) {
|
|
963
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_INVALID_REFRESH_TOKEN);
|
|
961
964
|
}
|
|
962
|
-
else if (error instanceof HttpException) {
|
|
965
|
+
else if (error instanceof common_1.HttpException) {
|
|
963
966
|
throw error;
|
|
964
967
|
}
|
|
965
|
-
throw new UnauthorizedException();
|
|
968
|
+
throw new common_1.UnauthorizedException();
|
|
966
969
|
}
|
|
967
970
|
}
|
|
968
971
|
/**
|
|
@@ -1007,7 +1010,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1007
1010
|
try {
|
|
1008
1011
|
const user = await this.userService.getUserById(authUser.id);
|
|
1009
1012
|
if (!user) {
|
|
1010
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_UNKNOWN);
|
|
1013
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_UNKNOWN);
|
|
1011
1014
|
}
|
|
1012
1015
|
if (user?.password) {
|
|
1013
1016
|
const { oldPassword, newPassword } = updatePasswordDto;
|
|
@@ -1016,21 +1019,24 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1016
1019
|
const user = await this.userService.updateUserById(authUser.id, { password }, {
|
|
1017
1020
|
id: authUser.id,
|
|
1018
1021
|
});
|
|
1022
|
+
if (!user) {
|
|
1023
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_500_CHANGE_PASSWORD);
|
|
1024
|
+
}
|
|
1019
1025
|
user.password = null;
|
|
1020
1026
|
// Success callback with error logging
|
|
1021
1027
|
await this.userService
|
|
1022
1028
|
.onChangePassword?.(request, authUser)
|
|
1023
|
-
.catch(callbackError => LoggerService.error("Error in onChangePassword success callback:", callbackError));
|
|
1029
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onChangePassword success callback:", callbackError));
|
|
1024
1030
|
return user;
|
|
1025
1031
|
}
|
|
1026
|
-
throw new NotFoundException(AuthErrors.AUTH_401_INVALID_PASSWORD);
|
|
1032
|
+
throw new common_1.NotFoundException(auth_1.AuthErrors.AUTH_401_INVALID_PASSWORD);
|
|
1027
1033
|
}
|
|
1028
|
-
throw new ForbiddenException(AuthErrors.AUTH_401_NOT_LOCAL);
|
|
1034
|
+
throw new common_1.ForbiddenException(auth_1.AuthErrors.AUTH_401_NOT_LOCAL);
|
|
1029
1035
|
}
|
|
1030
1036
|
catch (error) {
|
|
1031
1037
|
await this.userService
|
|
1032
1038
|
.onChangePassword?.(request, authUser, error)
|
|
1033
|
-
.catch(callbackError => LoggerService.error("Error in onChangePassword error callback:", callbackError));
|
|
1039
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onChangePassword error callback:", callbackError));
|
|
1034
1040
|
throw error;
|
|
1035
1041
|
}
|
|
1036
1042
|
}
|
|
@@ -1066,7 +1072,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1066
1072
|
*/
|
|
1067
1073
|
async sendVerificationEmail(user) {
|
|
1068
1074
|
if (!this.userService.sendVerificationEmail) {
|
|
1069
|
-
throw new NotFoundException(Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1075
|
+
throw new common_1.NotFoundException(nest_connector_1.Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1070
1076
|
}
|
|
1071
1077
|
try {
|
|
1072
1078
|
const token = AuthService_1.generateVerifyToken();
|
|
@@ -1074,10 +1080,10 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1074
1080
|
await this.userService.sendVerificationEmail(user.id, token);
|
|
1075
1081
|
}
|
|
1076
1082
|
catch (error) {
|
|
1077
|
-
if (error instanceof HttpException) {
|
|
1083
|
+
if (error instanceof common_1.HttpException) {
|
|
1078
1084
|
throw error;
|
|
1079
1085
|
}
|
|
1080
|
-
throw new InternalServerErrorException(AuthErrors.AUTH_500_SEND_EMAIL_VERIFICATION);
|
|
1086
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_500_SEND_EMAIL_VERIFICATION);
|
|
1081
1087
|
}
|
|
1082
1088
|
}
|
|
1083
1089
|
/**
|
|
@@ -1119,20 +1125,20 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1119
1125
|
*/
|
|
1120
1126
|
async resendEmailVerification(request, resendEmailVerifyDto) {
|
|
1121
1127
|
if (!("getUserByEmail" in this.userService) || !this.userService.sendVerificationEmail) {
|
|
1122
|
-
throw new NotFoundException(Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1128
|
+
throw new common_1.NotFoundException(nest_connector_1.Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1123
1129
|
}
|
|
1124
1130
|
const user = await this.userService.getUserByEmail(resendEmailVerifyDto.email);
|
|
1125
1131
|
if (user) {
|
|
1126
1132
|
if (user.emailVerified) {
|
|
1127
|
-
throw new BadRequestException(AuthErrors.AUTH_400_EMAIL_ALREADY_VERIFIED);
|
|
1133
|
+
throw new common_1.BadRequestException(auth_1.AuthErrors.AUTH_400_EMAIL_ALREADY_VERIFIED);
|
|
1128
1134
|
}
|
|
1129
1135
|
await this.sendVerificationEmail(user);
|
|
1130
1136
|
await this.userService
|
|
1131
1137
|
.onResendVerificationEmail?.(request, user.id)
|
|
1132
|
-
.catch(callbackError => LoggerService.error("Error in onResendVerificationEmail callback:", callbackError));
|
|
1133
|
-
return new SuccessResponseDto(AuthSuccessResponses.AUTH_200_EMAIL_VERIFICATION_SENT);
|
|
1138
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onResendVerificationEmail callback:", callbackError));
|
|
1139
|
+
return new nest_connector_1.SuccessResponseDto(auth_1.AuthSuccessResponses.AUTH_200_EMAIL_VERIFICATION_SENT);
|
|
1134
1140
|
}
|
|
1135
|
-
throw new NotFoundException(AuthErrors.AUTH_404_EMAIL);
|
|
1141
|
+
throw new common_1.NotFoundException(auth_1.AuthErrors.AUTH_404_EMAIL);
|
|
1136
1142
|
}
|
|
1137
1143
|
/**
|
|
1138
1144
|
* Verify a user's email address using a verification token
|
|
@@ -1172,7 +1178,7 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1172
1178
|
*/
|
|
1173
1179
|
async verifyEmail(request, emailVerifyDto) {
|
|
1174
1180
|
if (!this.userService.sendVerificationEmail) {
|
|
1175
|
-
throw new NotFoundException(Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1181
|
+
throw new common_1.NotFoundException(nest_connector_1.Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1176
1182
|
}
|
|
1177
1183
|
try {
|
|
1178
1184
|
const userId = await this.tokenVerifyService.getUserIdByEmailVerifyToken(emailVerifyDto.token);
|
|
@@ -1184,16 +1190,16 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1184
1190
|
// Success callback with error logging
|
|
1185
1191
|
await this.userService
|
|
1186
1192
|
.onVerifyEmail?.(request, userId, true)
|
|
1187
|
-
.catch(callbackError => LoggerService.error("Error in onVerifyEmail success callback:", callbackError));
|
|
1193
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onVerifyEmail success callback:", callbackError));
|
|
1188
1194
|
return true;
|
|
1189
1195
|
}
|
|
1190
1196
|
return false;
|
|
1191
1197
|
}
|
|
1192
1198
|
catch (error) {
|
|
1193
|
-
if (error instanceof HttpException) {
|
|
1199
|
+
if (error instanceof common_1.HttpException) {
|
|
1194
1200
|
throw error;
|
|
1195
1201
|
}
|
|
1196
|
-
throw new InternalServerErrorException(AuthErrors.AUTH_500_VERIFY_EMAIL);
|
|
1202
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_500_VERIFY_EMAIL);
|
|
1197
1203
|
}
|
|
1198
1204
|
}
|
|
1199
1205
|
/**
|
|
@@ -1236,15 +1242,15 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1236
1242
|
*/
|
|
1237
1243
|
async requestPasswordReset(request, requestResetDto) {
|
|
1238
1244
|
if (!("getUserByEmail" in this.userService) || !this.userService.sendPasswordResetEmail) {
|
|
1239
|
-
throw new NotFoundException(Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1245
|
+
throw new common_1.NotFoundException(nest_connector_1.Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1240
1246
|
}
|
|
1241
1247
|
try {
|
|
1242
1248
|
const user = await this.userService.getUserByEmail(requestResetDto.email);
|
|
1243
1249
|
if (!user) {
|
|
1244
|
-
throw new NotFoundException(AuthErrors.AUTH_404_EMAIL);
|
|
1250
|
+
throw new common_1.NotFoundException(auth_1.AuthErrors.AUTH_404_EMAIL);
|
|
1245
1251
|
}
|
|
1246
1252
|
if (!user.email) {
|
|
1247
|
-
throw new InternalServerErrorException(AuthErrors.AUTH_500);
|
|
1253
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_500);
|
|
1248
1254
|
}
|
|
1249
1255
|
const token = AuthService_1.generateVerifyToken();
|
|
1250
1256
|
const setToken = await this.tokenVerifyService.savePasswordResetToken(user.id, token);
|
|
@@ -1253,16 +1259,16 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1253
1259
|
// Success callback with error logging
|
|
1254
1260
|
await this.userService
|
|
1255
1261
|
.onRequestPasswordReset?.(request, user.id)
|
|
1256
|
-
.catch(callbackError => LoggerService.error("Error in onRequestPasswordReset callback:", callbackError));
|
|
1257
|
-
return new SuccessResponseDto(AuthSuccessResponses.AUTH_200_PASSWORD_RESET_EMAIL_SENT);
|
|
1262
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onRequestPasswordReset callback:", callbackError));
|
|
1263
|
+
return new nest_connector_1.SuccessResponseDto(auth_1.AuthSuccessResponses.AUTH_200_PASSWORD_RESET_EMAIL_SENT);
|
|
1258
1264
|
}
|
|
1259
|
-
throw new InternalServerErrorException(AuthErrors.AUTH_500_REQUEST_PASSWORD_RESET);
|
|
1265
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_500_REQUEST_PASSWORD_RESET);
|
|
1260
1266
|
}
|
|
1261
1267
|
catch (error) {
|
|
1262
|
-
if (error instanceof HttpException) {
|
|
1268
|
+
if (error instanceof common_1.HttpException) {
|
|
1263
1269
|
throw error;
|
|
1264
1270
|
}
|
|
1265
|
-
throw new InternalServerErrorException(AuthErrors.AUTH_500_REQUEST_PASSWORD_RESET);
|
|
1271
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_500_REQUEST_PASSWORD_RESET);
|
|
1266
1272
|
}
|
|
1267
1273
|
}
|
|
1268
1274
|
/**
|
|
@@ -1309,16 +1315,16 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1309
1315
|
*/
|
|
1310
1316
|
async verifyResetPasswordToken(request, verifyDto) {
|
|
1311
1317
|
if (!("getUserByEmail" in this.userService) || !this.userService.sendPasswordResetEmail) {
|
|
1312
|
-
throw new NotFoundException(Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1318
|
+
throw new common_1.NotFoundException(nest_connector_1.Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1313
1319
|
}
|
|
1314
1320
|
const userId = await this.tokenVerifyService.getUserIdByPasswordResetToken(verifyDto.token);
|
|
1315
1321
|
if (userId) {
|
|
1316
1322
|
await this.userService
|
|
1317
1323
|
.onVerifyResetPasswordToken?.(request, userId)
|
|
1318
|
-
.catch(callbackError => LoggerService.error("Error in onVerifyResetPasswordToken callback:", callbackError));
|
|
1319
|
-
return new SuccessResponseDto(AuthSuccessResponses.AUTH_200_PASSWORD_RESET_TOKEN_VALID);
|
|
1324
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onVerifyResetPasswordToken callback:", callbackError));
|
|
1325
|
+
return new nest_connector_1.SuccessResponseDto(auth_1.AuthSuccessResponses.AUTH_200_PASSWORD_RESET_TOKEN_VALID);
|
|
1320
1326
|
}
|
|
1321
|
-
throw new UnauthorizedException(AuthErrors.AUTH_401_EXPIRED_OR_INVALID_PASSWORD_RESET_TOKEN);
|
|
1327
|
+
throw new common_1.UnauthorizedException(auth_1.AuthErrors.AUTH_401_EXPIRED_OR_INVALID_PASSWORD_RESET_TOKEN);
|
|
1322
1328
|
}
|
|
1323
1329
|
/**
|
|
1324
1330
|
* Reset a user's password using a reset token
|
|
@@ -1370,32 +1376,32 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1370
1376
|
*/
|
|
1371
1377
|
async resetPassword(request, resetPasswordDto) {
|
|
1372
1378
|
if (!("getUserByEmail" in this.userService) || !this.userService.sendPasswordResetEmail) {
|
|
1373
|
-
throw new NotFoundException(Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1379
|
+
throw new common_1.NotFoundException(nest_connector_1.Errors.ERROR_404_NOT_IMPLEMENTED);
|
|
1374
1380
|
}
|
|
1375
1381
|
try {
|
|
1376
1382
|
const { token, password } = resetPasswordDto;
|
|
1377
1383
|
const userId = await this.tokenVerifyService.getUserIdByPasswordResetToken(token);
|
|
1378
1384
|
if (!userId) {
|
|
1379
|
-
throw new NotFoundException(AuthErrors.AUTH_401_EXPIRED_OR_INVALID_PASSWORD_RESET_TOKEN);
|
|
1385
|
+
throw new common_1.NotFoundException(auth_1.AuthErrors.AUTH_401_EXPIRED_OR_INVALID_PASSWORD_RESET_TOKEN);
|
|
1380
1386
|
}
|
|
1381
1387
|
const hash = AuthService_1.generateHash(password);
|
|
1382
1388
|
const user = await this.userService.updateUserById(userId, { password: hash }, {
|
|
1383
1389
|
id: userId,
|
|
1384
1390
|
});
|
|
1385
1391
|
if (!user) {
|
|
1386
|
-
throw new NotFoundException(AuthErrors.AUTH_500_PASSWORD_RESET);
|
|
1392
|
+
throw new common_1.NotFoundException(auth_1.AuthErrors.AUTH_500_PASSWORD_RESET);
|
|
1387
1393
|
}
|
|
1388
1394
|
await this.tokenVerifyService.clearPasswordResetTokenByUserId(userId);
|
|
1389
1395
|
await this.userService
|
|
1390
1396
|
.onResetPassword?.(request, userId)
|
|
1391
|
-
.catch(callbackError => LoggerService.error("Error in onResetPassword callback:", callbackError));
|
|
1392
|
-
return new SuccessResponseDto(AuthSuccessResponses.AUTH_200_PASSWORD_RESET_SUCCESS);
|
|
1397
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onResetPassword callback:", callbackError));
|
|
1398
|
+
return new nest_connector_1.SuccessResponseDto(auth_1.AuthSuccessResponses.AUTH_200_PASSWORD_RESET_SUCCESS);
|
|
1393
1399
|
}
|
|
1394
1400
|
catch (error) {
|
|
1395
|
-
if (error instanceof HttpException) {
|
|
1401
|
+
if (error instanceof common_1.HttpException) {
|
|
1396
1402
|
throw error;
|
|
1397
1403
|
}
|
|
1398
|
-
throw new NotFoundException(AuthErrors.AUTH_500_PASSWORD_RESET);
|
|
1404
|
+
throw new common_1.NotFoundException(auth_1.AuthErrors.AUTH_500_PASSWORD_RESET);
|
|
1399
1405
|
}
|
|
1400
1406
|
}
|
|
1401
1407
|
/**
|
|
@@ -1436,15 +1442,15 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1436
1442
|
*/
|
|
1437
1443
|
async signOut(request, authUser, response) {
|
|
1438
1444
|
try {
|
|
1439
|
-
if (this.options.authMethod === AuthMethod.COOKIE && this.options.cookies) {
|
|
1440
|
-
response.cookie(ACCESS_TOKEN_COOKIE_NAME, "", {
|
|
1445
|
+
if (this.options.authMethod === auth_1.AuthMethod.COOKIE && this.options.cookies) {
|
|
1446
|
+
response.cookie(constants_1.ACCESS_TOKEN_COOKIE_NAME, "", {
|
|
1441
1447
|
maxAge: 0,
|
|
1442
1448
|
httpOnly: true,
|
|
1443
1449
|
sameSite: this.options.cookies.sameSite,
|
|
1444
1450
|
secure: this.options.cookies.secure,
|
|
1445
1451
|
signed: true,
|
|
1446
1452
|
});
|
|
1447
|
-
response.cookie(REFRESH_TOKEN_COOKIE_NAME, "", {
|
|
1453
|
+
response.cookie(constants_1.REFRESH_TOKEN_COOKIE_NAME, "", {
|
|
1448
1454
|
maxAge: 0,
|
|
1449
1455
|
httpOnly: true,
|
|
1450
1456
|
sameSite: this.options.cookies.sameSite,
|
|
@@ -1473,27 +1479,27 @@ let AuthService = AuthService_1 = class AuthService {
|
|
|
1473
1479
|
}
|
|
1474
1480
|
await this.userService
|
|
1475
1481
|
.onSignOut?.(request, authUser)
|
|
1476
|
-
.catch(callbackError => LoggerService.error("Error in onSignOut success callback:", callbackError));
|
|
1477
|
-
return new SuccessResponseDto(AuthSuccessResponses.AUTH_200_SIGNED_OUT);
|
|
1482
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onSignOut success callback:", callbackError));
|
|
1483
|
+
return new nest_connector_1.SuccessResponseDto(auth_1.AuthSuccessResponses.AUTH_200_SIGNED_OUT);
|
|
1478
1484
|
}
|
|
1479
1485
|
catch (error) {
|
|
1480
1486
|
await this.userService
|
|
1481
1487
|
.onSignOut?.(request, authUser, error)
|
|
1482
|
-
.catch(callbackError => LoggerService.error("Error in onSignOut error callback:", callbackError));
|
|
1483
|
-
if (error instanceof HttpException) {
|
|
1488
|
+
.catch(callbackError => nest_core_1.LoggerService.error("Error in onSignOut error callback:", callbackError));
|
|
1489
|
+
if (error instanceof common_1.HttpException) {
|
|
1484
1490
|
throw error;
|
|
1485
1491
|
}
|
|
1486
|
-
throw new InternalServerErrorException(AuthErrors.AUTH_500_SIGN_UP);
|
|
1492
|
+
throw new common_1.InternalServerErrorException(auth_1.AuthErrors.AUTH_500_SIGN_UP);
|
|
1487
1493
|
}
|
|
1488
1494
|
}
|
|
1489
1495
|
};
|
|
1490
|
-
AuthService =
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
__param(
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1496
|
+
exports.AuthService = AuthService;
|
|
1497
|
+
exports.AuthService = AuthService = AuthService_1 = tslib_1.__decorate([
|
|
1498
|
+
(0, common_1.Injectable)(),
|
|
1499
|
+
tslib_1.__param(0, (0, common_1.Inject)(tokens_1.AUTH_OPTIONS)),
|
|
1500
|
+
tslib_1.__param(1, (0, common_1.Inject)(tokens_1.USER_SERVICE)),
|
|
1501
|
+
tslib_1.__metadata("design:paramtypes", [Object, Object, jwt_token_service_1.JwtTokenService,
|
|
1502
|
+
user_cache_service_1.UserCacheService,
|
|
1503
|
+
token_verify_service_1.TokenVerifyService])
|
|
1497
1504
|
], AuthService);
|
|
1498
|
-
export { AuthService };
|
|
1499
1505
|
//# sourceMappingURL=auth.service.js.map
|