Custom database login - Resend verification if not_verified

Greetings,
I’ve been wondering,
Is it possible to resend the verification from the custom database’s login script?

I’ll add an example,
Ofc it fails because I did not add the Authentication to the request ( and Unable to add it. )

function login(email, password, callback) {
  const mysql = require('mysql');
  const bcrypt = require('bcrypt');

  const connection = mysql.createConnection({
    host: configuration.DB_HOST,
    user: configuration.DB_USER,
    password: configuration.DB_PASSWORD,
    database: configuration.DATABASE_NAME
  });

  const resendVerificationAndThrowError = (user, callback) => {
  const request = require('request');
  const requestParams = {
    user_id: user.id.toString(),
    client_id: configuration.CLIENT_ID
  };

    request.post("<DOMAIN>/api/v2/jobs/verification-email", requestParams,  function(err,httpResponse,body) {
      console.error('error:', err);
      console.log('statusCode:', httpResponse && httpResponse.statusCode); 
      console.log('body:', body);
  });

    return callback(new UnauthorizedError('Please verify your email before loggin in.'));
  };

  connection.connect();

  const query = 'SELECT id, nickname, email, password FROM users WHERE email = ?';

  connection.query(query, [ email ], function(err, results) {
  if (err) return callback(err);
  if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));
  const user = results[0];

  bcrypt.compare(password, user.password, function(err, isValid) {
  if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));
  if (!user.email_Verified) return resendVerificationAndThrowError(user, callback);

    callback(null, {
    user_id: user.id.toString(),
    nickname: user.nickname,
    email: user.email
  });
  });
  });
  }

Does anyone have a good answer for this?

Thanks in advance,
Daniel

Hi @blokh,

It may make more sense to do this from a rule, have you considered that?

Here is a thread that goes through how to set it up:

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.