Hello!
I’m doing Custom Database connection to my Laravel application. The connection itself is set-up to corresponding database and data from the users table is retrieved. However, I would like to check if user can be authenticated or not using Laravel hashed password. Right now I always get response “401 - Unauthorized”.
Does anybody have ideas how to implement this using “Database Action Scripts” in Auth0? Here is my code:
function login(email, password, callback) {
const mysql = require(‘mysql’);
const bcrypt = require(‘bcrypt’);
const connection = mysql.createConnection({
host: configuration.ip,
user: configuration.user,
password: configuration.password,
database: configuration.database
});
//console.log(connection);
connection.connect();
const query = ‘SELECT id, name, email, password FROM users WHERE email = ?’;
connection.query(query, [ email ], function(err, results) {
if (err) console.log(err);
if (err) return callback(err);
if (results) console.log(results);
if (results.length === 0) return callback(new WrongUsernameOrPasswordError(email));
const user = results[0];
bcrypt.compare(password, user.password, function(err, isValid) {
if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));
callback(null, {
user_id: user.id.toString(),
//user_id: user.email,
nickname: user.name,
email: user.email
});
});
});
}
Thank you in advance!
Best regards,
Levipro