Skip to content

Instantly share code, notes, and snippets.

@ShayMe21
Created March 27, 2019 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShayMe21/a660453e769879fd116433e35010912f to your computer and use it in GitHub Desktop.
Save ShayMe21/a660453e769879fd116433e35010912f to your computer and use it in GitHub Desktop.
Auth0 SQL Server Custom DB Get User script
function getByEmail(email, callback) {
//this example uses the "tedious" library
//more info here: http://pekim.github.io/tedious/index.html
const sqlserver = require('tedious@1.11.0');
const Connection = sqlserver.Connection;
const Request = sqlserver.Request;
const TYPES = sqlserver.TYPES;
var user_profile = [];
const connection = new Connection({
userName: '',
password: '',
server: '',
options: {
database: '',
port: 1433
}
});
const query = 'SELECT id, nickname, email FROM dbo.users WHERE email = @Email';
connection.on('debug', function (text) {
console.log(text);
}).on('errorMessage', function (text) {
console.log(JSON.stringify(text, null, 2));
}).on('infoMessage', function (text) {
//console.log(JSON.stringify(text, null, 2));
});
connection.on('connect', function (err) {
console.log("Established connection to DB");
if (err) {
console.log('Connection Failed');
throw err;
}
const request = new Request(query, function (err, rowCount) {
if (err) {
console.log('DB Query request failed');
throw err;
} else {
console.log(rowCount + ' rows found!');
}
if (rowCount === 0) {
callback("The User does not exist, no records returned from DB");
}
});
// We found a record/row for the user in DB
request.on('row', function (columns) {
console.log("Received rows event: Found a user record");
columns.forEach(function (column) {
user_profile.push(column.value);
});
// Send the user profile to Auth0
callback(null, {
user_id: user_profile[0],
nickname: user_profile[1],
email: user_profile[2]
});
});
request.addParameter('Email', TYPES.VarChar, email);
connection.execSql(request);
});
}
@pashmelkin
Copy link

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment