Skip to content

Instantly share code, notes, and snippets.

@mdamaceno
Last active August 23, 2019 19:41
Show Gist options
  • Save mdamaceno/a8d390abfc1369b158368bfd407fafed to your computer and use it in GitHub Desktop.
Save mdamaceno/a8d390abfc1369b158368bfd407fafed to your computer and use it in GitHub Desktop.
Auth0 custom scripts for connections
async function loginByEmail(email, callback) {
//this example uses the "pg" library
//more info here: https://github.com/brianc/node-postgres
const bcrypt = require('bcrypt');
const postgres = require('pg');
const conString = 'postgres://username:password@localhost:5432/database_name';
const client = await new pg.Client(conString);
client.connect();
const query = 'SELECT id, email FROM users WHERE email = $1';
const res = await client.query(query, [email], (err, res) => {
if (err) {
return callback(err);
} else {
const user = res.rows[0];
return callback(null, {
user_id: user.id,
email: user.email
});
}
});
}
async function login(email, password, callback) {
const mongo = require('mongodb');
const bcrypt = require('bcrypt');
const db = await mongo('mongodb://localhost:27017/database_name');
const User = await db.collection('users');
await User.findOne({ email: email }, function (err, user) {
if (err) return callback(err);
if (!user) return callback(new WrongUsernameOrPasswordError(email));
bcrypt.compare(password, user.password, function (err, isValid) {
if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));
return callback(null, {
user_id: user._id.toString(),
username: user.username,
email: user.email
});
});
});
}
async function login(email, password, callback) {
const bcrypt = require('bcrypt');
const postgres = require('pg');
const conString = 'postgres://username:password@localhost:5432/database_name';
const client = await new pg.Client(conString);
client.connect();
const query = 'SELECT id, email, password FROM users WHERE email = $1';
await client.query(query, [email], function (err, res) {
if (err || res.rows.length === 0) return callback(err || new WrongUsernameOrPasswordError(email));
const user = res.rows[0];
bcrypt.compare(password, user.password, function (err, isValid) {
if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));
return callback(null, {
user_id: user.id,
email: user.email
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment