Automatic User Migration when logging in not working

I am trying to log in using a custom database (my own db) and automatic import the user to auth0 dashboard users list

my login function is:

function login(email, password, callback) {
  try {
    const AWS = require('aws-sdk');
    const crypto = require('crypto');
    const getHashedPassword = (salt, pwd) => {
      const hash = crypto
        .createHash('sha256')
        .update(pwd + salt)
        .digest('base64');
      return hash;
    };
    // Configure AWS DynamoDB
    AWS.config.update({
      accessKeyId: configuration.accessKeyId,
      secretAccessKey: configuration.secretAccessKey,
      sessionToken: configuration.sessionToken,
      region: 'us-west-2',
    });

    const dynamoDB = new AWS.DynamoDB.DocumentClient();

    const params = {
      TableName: 'dev_Users',
      IndexName: 'gsi_UsersTable_email', // Use the name of your GSI
      KeyConditionExpression: 'email = :email',
      ExpressionAttributeValues: {
        ':email': email,
      },
    };

  dynamoDB.query(params, function (err, data) {
      if (err) {
        console.log(err);
        return callback(err);
      } else if (!data || data.Items.length === 0) {
        return callback(new Error('User not found'));
      }

      const userInfo = data.Items[0];
      
      // Use crypto for password hashing
      const hashedPassword = getHashedPassword(userInfo.salt, password);

      // Compare the hashed password
      if (hashedPassword !== userInfo.password) {
        console.log('Invalid email or password');
        return callback(new Error('Invalid email or password'));
      }

      // If the password is valid, return the user profile
      const userProfile = {
        user_id: userInfo.userId,
        email: userInfo.email,
        email_verified: userInfo.emailVerified,
        // Add any additional profile information that Auth0 needs here
      };

      console.log(userProfile);
      return callback(null, userProfile);
    });
  } catch (error) {
    throw new Error(error);
  }
}

but when i try to log in using http://localhost:3000/login or go to Authentication → Authentication Profile → Try, i receive a Wrong email or password, despite in my real-time log i can see that it found the user in my db, verified the password and called callback with the userProfile, and i cant find this user in Auth0 Dashboard User tab

Does somebody know how to fix it?

1 Like