Console.log not showing for create script in Database Action Scripts

I am unable to debug why my create script is not working. The only error message I get is “[Error] User not found” even if I set the callback to just return an error. No console logs appear when using the Real-time Webtask Logs extension. Also, the template code is completely outdated and not usable (for postgresql template).

Here is my create script:

function create(user, callback) {
  console.log('test!');
  const bcrypt = require('bcrypt');
  const { Pool } = require('pg');

  const pool = new Pool({
    connectionString:
      configuration.POSTGRES_URL,
  });

  bcrypt.hash(user.password, 10, (err, hashedPassword) => {
    if (err) {
      console.error(err);
      throw new Error("Something went badly wrong!");
    }

    const query =
      'INSERT INTO public."User"(email, password, "photoId") VALUES ($1, $2, 0) RETURNING email, password';

    pool.query(query, [user.email, hashedPassword], (err, res) => {
      if (err) {
        console.error(err);
        throw new Error("Something went badly wrong!");
      }

      console.log('User created:', res.rows[0]);
    });
  });
}

My login and get user scripts work without any issue:

async function login(email, password, callback) {
  const { Pool } = require('pg');
  const bcrypt = require('bcrypt');

  const pool = new Pool({
    connectionString: configuration.POSTGRES_URL,
  });

  try {
    const query = 'SELECT id, email, password FROM public."User" WHERE email = $1';
    const { rows } = await pool.query(query, [email]);

    if (rows.length === 0) {
      return callback(new Error('Wrong username or password'));
    }

    const user = rows[0];
    const isValid = await bcrypt.compare(password, user.password);

    if (!isValid) {
      return callback(new Error('Wrong username or password'));
    }

    return callback(null, {
      user_id: user.id,
      email: user.email,
    });
  } catch (err) {
    return callback(err);
  }
}
async function loginByEmail(email, callback) {
  const { Pool } = require('pg');

  const pool = new Pool({
    connectionString: configuration.POSTGRES_URL, // Make sure this is correctly set in your environment
  });
  try {
    const query = 'SELECT id, email, password FROM public."User" WHERE email = $1';
    const { rows } = await pool.query(query, [email]);

    if (rows.length === 0) {
      return callback(new Error('User not found'));
    }

    const user = rows[0];
    return callback(null, {
      user_id: user.id,
      email: user.email,
      // You might want to return more fields depending on your requirements
    });
  } catch (err) {
    return callback(err);
  }
}

Hi Jack,
I am experiencing the same issue with console.log(). have you found a solution? it seems nobody has replied to your questions.

Hi Simon,

I was able to get this solved by going on a Zoom call with a dev from Auth0. Here are my working scripts, hopefully they will be useful. I can’t remember what actually fixed the issue but it was something simple that we missed.

Login Script:

async function login(email, password, callback) {
  console.log("using login script");
  const { Pool } = require('pg');
  const bcrypt = require('bcrypt');

  const pool = new Pool({
    connectionString: configuration.POSTGRES_URL,
  });

  try {
    const query = 'SELECT id, email, password FROM public."User" WHERE email = $1';
    const { rows } = await pool.query(query, [email]);

    if (rows.length === 0) {
      return callback(new Error('Wrong username or password'));
    }

    const user = rows[0];
    const isValid = await bcrypt.compare(password, user.password);

    if (!isValid) {
      return callback(new Error('Wrong username or password'));
    }

    return callback(null, {
      user_id: user.id,
      email: user.email,
    });
  } catch (err) {
    return callback(err);
  }
}

Create script:

function create(user, callback) {
  console.log("Using create script");
  const bcrypt = require('bcrypt');
  const { Pool } = require('pg');
  const pool = new Pool({
    connectionString:configuration.POSTGRES_URL
  });

  bcrypt.hash(user.password, 10, (err, hashedPassword) => {
    if (err) {
      return callback(new Error('Error hashing password'));
    }

    const query =
      'INSERT INTO public."User"(email, password, "photoId") VALUES ($1, $2, 0) RETURNING email, password';

    pool.query(query, [user.email, hashedPassword], (err, res) => {
      if (err) {
        if (err.code === '23505') {
          // Unique constraint violation
          return callback(new Error('User already exists'));
        }
        return callback(new Error('Error accessing database'));
      }

      if (res.rows.length > 0) {
        console.log('User created:', res.rows[0]);
        callback(null);
      } else {
        callback(new Error('User not created'));
      }
    });
  });
}

Get User Script:

async function loginByEmail(email, callback) {
  console.log("using get user script");
  const { Pool } = require('pg');

  const pool = new Pool({
    connectionString: configuration.POSTGRES_URL, // Make sure this is correctly set in your environment
  });
  try {
    const query = 'SELECT id, email, password FROM public."User" WHERE email = $1';
    const { rows } = await pool.query(query, [email]);

    if (rows.length === 0) {
      return callback(new Error('User not found'));
    }

    const user = rows[0];
    return callback(null, {
      user_id: user.id,
      email: user.email,
      // You might want to return more fields depending on your requirements
    });
  } catch (err) {
    return callback(err);
  }
}

If you need more help I do recommend contacting Auth0 and hopping on a zoom call, they were very helpful.

Jack

1 Like

After quickly looking at my create script, the difference was that you need to use the callback for a new error rather than just throwing an error.

1 Like

Thanks for sharing your solution @jackthomas818 ! Hopefully this helps you @smolee :pray:

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