Custom database connection

Hey there. I was successfully able to connect to my external database using “custom database” feature. All action scripts are running successfully and the user data is returned but I don’t see user being created on Auth0 even though I have enabled “Import users to auth0” option. Also, I only see options for 2 action scripts “login” and “Get user”. There is no option for me to write scripts for “create”, “delete” and for other actions. What could be the issue here?
My “login script” code:

function login(email, password, callback) {
const crypto = require(‘crypto’);
const sql = require(‘mssql’);
const hash = crypto.createHash(‘sha256’).update(password).digest(‘hex’);
const query = select * from dbo.ufn_getAccount('${email}', '${hash}');;
console.log(query);

const con = await sql.connect({
 user:  'xxxxx_user',
 password:  'xxxxxxxx_2018',
 server:    '1xxxx114.194',
 database: 'xxxxxxxxx'
});

if (con) {
let request = new sql.Request();
let response = request.query(query);

      // when no user is returned, it means unsuccessful login,
      // respond with Wrong Username Or Password message
if (response && response.length === 0) {
	    return callback(new WrongUsernameOrPasswordError(
          email,
          "my error message"
        ));
}
console.log('user-details', response);
const user_id = response[0].accountId;

//close the DB connection
sql.close();

callback(null, { 
  user_id,
  email,
  password
});

} else {
	console.log('no connection');
 	callback(new Error("NO MSSQL CONNECTION"));
  //throw new Error('NO MSSQL CONNECTION');

}
}

1 Like

@dan.woda require your help

I figured this out. The issue was with the “user_id” field. Auth0 expects it to be as string but my DB was returning an integer. I changed the type to string and it worked and user was created and if there are any “rules” written, those will also run.

Perfect! Glad you have figured that one out!