Search code examples
javascriptnode.jsnodemailer

nodemailer verify is returning success even with bad credentials


so I am trying to create a gmail client as a learning project. I am using nodemailer module to verify credentials and send mails. My code is as follows

let data ;
    req.setEncoding('utf8') ;
    await req.on('data', (chunk) => {
        data = query.parse(chunk) ;
    });

    const mailer = nodemailer.createTransport({service: 'gmail'}) ;

    mailer.options.auth = await data ;

    mailer.verify((err, suc) => {
        if (mailer.options.auth === undefined) {
          console.log("No Credentials") ;
        }
        else if (err) {
            console.log("Error : ") ;
        } else {
            console.log("success") ;
        }
      }) ;

PS : the function wrapping it is a async arrow function

It is correctly logging "No Credentials" when the post data received from form is empty, It is logging "success" even if entered credentials are wrong. I hope for a solution soon, Thanks in advance.


Solution

  • I've tried it myself and I've came to these 2 conclusions :

    • I haven't found any way to re-set the auth property after having created the Mail object using createTransport() method. Maybe there is one, maybe not. You're gonna have to look into it.
    • The verify() method does not check if the auth object is defined but rather checks if the props that it contains are defined and valid

    But here's what I did and that works :

    // the function returns an object of type { user: string, pass: string }
    const credentials = await getMyAuthData();
    const config = {
      service: 'Gmail',
      auth: credentials
    };
    
    const mailer = nodemailer.createTransport(config);
    
    mailer.verify((error, success) => {
      if (error) throw error;
      console.log(success);
    });