Răsfoiți Sursa

chore(core): Fix circular dependency & lazy require

Michael Bromley 4 ani în urmă
părinte
comite
c96938d4fe

+ 2 - 2
packages/core/src/config/auth/bcrypt-password-hashing-strategy.ts

@@ -12,12 +12,12 @@ const SALT_ROUNDS = 12;
 export class BcryptPasswordHashingStrategy implements PasswordHashingStrategy {
     private bcrypt: any;
 
-    async init() {
+    constructor() {
         // The bcrypt lib is lazily loaded so that if we want to run Vendure
         // in an environment that does not support native Node modules
         // (such as an online sandbox like Stackblitz) the bcrypt dependency
         // does not get loaded when linking the source files on startup.
-        this.bcrypt = await import('bcrypt');
+        this.bcrypt = require('bcrypt');
     }
 
     hash(plaintext: string): Promise<string> {

+ 4 - 4
packages/core/src/config/auth/native-authentication-strategy.ts

@@ -3,12 +3,10 @@ import { DocumentNode } from 'graphql';
 import gql from 'graphql-tag';
 
 import { RequestContext } from '../../api/common/request-context';
-import { UnauthorizedError } from '../../common/error/errors';
 import { Injector } from '../../common/injector';
 import { TransactionalConnection } from '../../connection/transactional-connection';
 import { NativeAuthenticationMethod } from '../../entity/authentication-method/native-authentication-method.entity';
 import { User } from '../../entity/user/user.entity';
-import { PasswordCipher } from '../../service/helpers/password-cipher/password-cipher';
 
 import { AuthenticationStrategy } from './authentication-strategy';
 
@@ -31,10 +29,12 @@ export class NativeAuthenticationStrategy implements AuthenticationStrategy<Nati
     readonly name = NATIVE_AUTH_STRATEGY_NAME;
 
     private connection: TransactionalConnection;
-    private passwordCipher: PasswordCipher;
+    private passwordCipher: import('../../service/helpers/password-cipher/password-cipher').PasswordCipher;
 
-    init(injector: Injector) {
+    async init(injector: Injector) {
         this.connection = injector.get(TransactionalConnection);
+        // This is lazily-loaded to avoid a circular dependency
+        const { PasswordCipher } = await import('../../service/helpers/password-cipher/password-cipher');
         this.passwordCipher = injector.get(PasswordCipher);
     }