function login(username, password) {
  if (!username || !password) {
    return Promise.reject({
      success: false,
      message: 'Please, provide username and password!',
    });
  }

  return User.findOne({ username })
    .exec()
    .then((user) => {
      if (!user) {
        return Promise.reject({
          message: 'Sorry, no account found with the username',
          notExists: true,
        });
      }

      return Promise.all([
        Promise.resolve(user),
        user.comparePassword(password),
      ]);
    })
    .then(([user, isPasswordMatch]) => {
      if (!isPasswordMatch) {
        return Promise.reject({
          success: false,
          message: 'The username or password you entered is incorrect.',
        });
      }
      return user;
    })
    .then((savedUser) => userController.getUserById(savedUser._id))
    .then(({ user }) => {
      const token = jwt.sign(
        { userId: user._id },
        JWT_SECRET,
        { expiresIn: 60 * 60 * 24 * 10 } // 10 days
      );

      return Promise.resolve({
        success: true,
        message: 'Success! You are logged in.',
        token,
        user,
      });
    });
}
// Login user by username and password
Login user by username and password
Login user by username and password
function login(username, password) {
  if (!username || !password) {
    return Promise.reject({
      success: false,
      message: 'Please, provide username and password!',
    });
  }
  return User.findOne({ username })
    .exec()
    .then((user) => {
      if (!user) {
        return Promise.reject({
          message: 'Sorry, no account found with the username',
          notExists: true,
        });
      }
      return Promise.all([
        Promise.resolve(user),
        user.comparePassword(password),
      ]);
    })
    .then(([user, isPasswordMatch]) => {
      if (!isPasswordMatch) {
        return Promise.reject({
          success: false,
          message: 'The username or password you entered is incorrect.',
        });
      }
      return user;
    })
    .then((savedUser) => userController.getUserById(savedUser._id))
    .then(({ user }) => {
      const token = jwt.sign(
        { userId: user._id },
        JWT_SECRET,
        { expiresIn: 60 * 60 * 24 * 10 } 
      );
      return Promise.resolve({
        success: true,
        message: 'Success! You are logged in.',
        token,
        user,
      });
    });
}
function login ( username , password ) { if ( ! username || ! password ) { return Promise . reject ( { success : false , message : 'Please, provide username and password!' , } ) ; } return User . findOne ( { username } ) . exec ( ) . then ( ( user ) => { if ( ! user ) { return Promise . reject ( { message : 'Sorry, no account found with the username' , notExists : true , } ) ; } return Promise . all ( [ Promise . resolve ( user ) , user . comparePassword ( password ) , ] ) ; } ) . then ( ( [ user , isPasswordMatch ] ) => { if ( ! isPasswordMatch ) { return Promise . reject ( { success : false , message : 'The username or password you entered is incorrect.' , } ) ; } return user ; } ) . then ( ( savedUser ) => userController . getUserById ( savedUser . _id ) ) . then ( ( { user } ) => { const token = jwt . sign ( { userId : user . _id } , JWT_SECRET , { expiresIn : 60 * 60 * 24 * 10 } ) ; return Promise . resolve ( { success : true , message : 'Success! You are logged in.' , token , user , } ) ; } ) ; }
Login user by username and password
Login user by username and password
// 10 days