Jelajahi Sumber

refactor(core): Lazily load bcrypt lib

Michael Bromley 4 tahun lalu
induk
melakukan
bc443ca00b

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

@@ -12,19 +12,23 @@ const SALT_ROUNDS = 12;
 export class BcryptPasswordHashingStrategy implements PasswordHashingStrategy {
     private bcrypt: any;
 
-    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 = require('bcrypt');
-    }
-
     hash(plaintext: string): Promise<string> {
+        this.getBcrypt();
         return this.bcrypt.hash(plaintext, SALT_ROUNDS);
     }
 
     check(plaintext: string, hash: string): Promise<boolean> {
+        this.getBcrypt();
         return this.bcrypt.compare(plaintext, hash);
     }
+
+    private getBcrypt() {
+        if (!this.bcrypt) {
+            // 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 = require('bcrypt');
+        }
+    }
 }