Skip to content

Instantly share code, notes, and snippets.

@mford22392
Last active December 6, 2018 15:52
Show Gist options
  • Save mford22392/664ff942667144d3a70838688668dbe3 to your computer and use it in GitHub Desktop.
Save mford22392/664ff942667144d3a70838688668dbe3 to your computer and use it in GitHub Desktop.
{
"options": {
"customScripts": {
"change_email": "function changeEmail(oldEmail, newEmail, emailVerified, callback) {var conString = configuration.DATABASE_URL || 'postgres://[email protected]:14391/guild_development';postgres(conString, function (err, client, done) {if (err) {console.log('could not connect to postgres db', err);return callback(err);}const query = 'UPDATE users SET email = $1 WHERE email = $2';client.query(query, [newEmail, oldEmail], function (err, result) {done();if (err) {console.log('error executing query', err);return callback(err);}if (result.rows.length === 0) {return callback(null);} else {var user = result.rows[0];callback(null, {id: user.uuid,email: user.email,given_name: user.first_name,family_name: user.last_name,phone_number: user.phone});}});});}",
"login": "function login(email, password, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\n // If you are in guild-dev, switch 'xxx' below to your forwarding address\n var conString = configuration.DATABASE_URL || \"postgres://[email protected]:14391/guild_development\";\n postgres(conString, function (err, client, done) {\n if (err) {\n console.log('could not connect to postgres db', err);\n return callback(err);\n }\n\n var query = 'SELECT id, uuid, email, encrypted_password ' +\n 'FROM users WHERE email = $1';\n\n client.query(query, [email], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n if (err) {\n console.log('error executing query', err);\n return callback(err);\n }\n\n if (result.rows.length === 0) {\n return callback(new WrongUsernameOrPasswordError(email));\n }\n\n var user = result.rows[0];\n\n bcrypt.compare(password, user.encrypted_password, function (err, isValid) {\n if (err) {\n callback(err);\n } else if (!isValid) {\n callback(new WrongUsernameOrPasswordError(email));\n } else {\n callback(null, {\n id: user.id,\n uuid: user.uuid,\n email: user.email\n });\n }\n });\n });\n });\n}\n",
"change_password": "function changePassword (email, newPassword, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\n // If you are in guild-dev, switch 'xxx' below to your forwarding address\n var conString = configuration.DATABASE_URL || \"postgres://[email protected]:14391/guild_development\";\n postgres(conString, function (err, client, done) {\n if (err) {\n console.log('could not connect to postgres db', err);\n return callback(err);\n }\n\n bcrypt.hash(newPassword, 10, function (err, hash) {\n if (err) return callback(err);\n client.query('UPDATE users SET encrypted_password = $1 WHERE email = $2', [hash, email], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n if (err) {\n return callback(err);\n }\n\n if (result.rowCount === 0) {\n return callback();\n }\n\n callback(null, result.rowCount > 0);\n });\n });\n });\n}\n",
"create": "function create(user, callback) {\n //this example uses the \"pg\" library\n //more info here: https://github.com/brianc/node-postgres\n\nvar conString = configuration.DATABASE_URL || \"postgres://[email protected]:14391/guild_development\";\n\n postgres(conString, function (err, client, done) {\n if (err) {\n console.log('could not connect to postgres db', err);\n return callback(err);\n }\n bcrypt.hash(user.password, 10, function (err, hashedPassword) {\n var userQuery = 'INSERT INTO users(email, encrypted_password, created_at, updated_at) VALUES ($1, $2, $3, $4) RETURNING *';\n const userRoleQuery = 'INSERT INTO users_roles(user_id, role_id) VALUES ($1, $2) RETURNING *';\n client.query(userQuery, [user.email, hashedPassword, new Date(), new Date()], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n if (err) {\n console.log('error executing query', err);\n return callback(err);\n }\n client.query(userRoleQuery, [result.rows[0].id, 1], (err, res) => {\n done();\n if (err) {\n console.log(err.stack);\n } else {\n return callback();\n }\n });\n });\n });\n });\n}\n",
"get_user": "function getByEmail (email, callback) {\n var conString = configuration.DATABASE_URL || \"postgres://[email protected]:14391/guild_development\";\n postgres(conString, function (err, client, done) {\n if (err) {\n console.log('could not connect to postgres db', err);\n return callback(err);\n }\n\n const query = 'SELECT id, email, uuid, first_name, last_name, phone FROM users WHERE email = $1';\n\n client.query(query, [email], function (err, result) {\n // NOTE: always call `done()` here to close\n // the connection to the database\n done();\n\n if (err) {\n console.log('error executing query', err);\n return callback(err);\n }\n\n if (result.rows.length === 0) {\n return callback(null);\n } else {\n var user = result.rows[0];\n callback(null, {\n id: user.uuid,\n email: user.email,\n given_name: user.first_name,\n family_name: user.last_name,\n phone_number: user.phone\n });\n }\n });\n });\n}\n\n"
},
"enabledDatabaseCustomization": true,
"brute_force_protection": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment