I am using pg
and node.js
. When a user logs in using the auth0
widget I am passing the email it returns and checking my database to see if the user exist. If the user does not exist I am inserting them into the database. I have managed to get this working with a hack job of a function but I would appreciate some help ironing it out.
Problems I am facing.
When checking the database for the email it is not checking it for the whole email address.
var emailCheck = "SELECT id from public.user WHERE email=" + req.body.email;
req.body.email;
is actually[email protected]
Returns this error,
column "myemail" does not exist
Even though
[email protected]
does exist.
regardless of the error it throws it moves on to insert the email address. If it does not exist it inserts it. Since there is a Unique Key on the email it throws an error
duplicate key value violates unique constraint "uk_user_email"
So to fix this my question is why is it not checking for after the @ sign? And what logic should I follow to change this function to run the first query and only run the second if the first query does not find the email address in question?
checkRegister: function(req, res) {
pool.connect(function(err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
} connection
var emailCheck = "SELECT id from public.user WHERE email=" + req.body.email;
var emailInsert = "insert into public.user (user_auth_level,email,account_locked,contract) " +
"values ('1','" + req.body.email + "','false','false')"
client.query(emailCheck, function(err, result) {
if (err) {
return console.error(err.message);
}
});
client.query(emailInsert, function(err, result) {
if (err) {
return console.error(err.message);
}
});
done();
});
pool.on('error', function(err, client) {
console.error('idle client error', err.message, err.stack)
});
}