Home > Net >  validator isStrongPassword is not a function
validator isStrongPassword is not a function

Time:09-21

I get this error whenever I try to register a user!

isStrongPassword is not a function

I tried typing the isStrongPassword which is Min 8 characters, 1 uppercase, 1 lowercase, 1 number, 1 symbol but no luck, I don't know what i'm missing !!

Auth.js

const isEmail = require ("validator/lib/isEmail.js")
const isStrongPassword = ("validator/lib/isStrongPassword");

    //REGISTER
    router.post("/register", async (req, res, next) => {
      try {
        if (!req.body.username) {
          return next(createError(400, "Invalid username"));
        } if (!req.body.email || !isEmail(req.body.email)) {
          return next(createError(400, "Invalid email"));
        } if (!req.body.password) {
          return next(createError(400, "Invalid password"));
        } else if (!isStrongPassword(req.body.password)) {
          return next(
            createError(
              400,
              "Invalid password: Min 8 characters, 1 uppercase, 1 lowercase, 1 number, 1 symbol"
            )
          );
        } else {
          const checkUsername = await User.findOne({ username: req.body.username });
          const checkEmail = await User.findOne({ email: req.body.email });
          if (checkUsername) {
            return next(createError(400, "Username taken"));
          } else if (checkEmail) {
            return next(createError(400, "Email taken"));
          } else {
            //generate new password
            const salt = await bcrypt.genSalt(10);
            const hashedPass = await bcrypt.hash(req.body.password, salt);
            //create new user
            const newUser = new User ({
                username: req.body.username,
                firstName: req.body.firstName,
                lastName: req.body.lastName,
                email: req.body.email,
                password: hashedPass,
                repeatPassword: hashedPass,
                birthday: req.body.birthday,
          });
          //save user and respond
          const user = await newUser.save();
          res.status(200).json({
            message: "Successfully registered",
            user,
          });
          }
        }
    
      } catch(err) {
        next(err);
    
      }
    });

CodePudding user response:

You are missing a require statement from your isStrongPassword import - and also presumably the file extension. e.g.

const isStrongPassword = ("validator/lib/isStrongPassword");

Should presumably be

const isStrongPassword = require ("validator/lib/isStrongPassword.js");
  • Related