Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const { name, email, password } = event.data;
if (!validator.isEmail(email)) {
return { error: 'Not a valid email' };
}
// check if user exists already
const userExists: boolean = await getUser(api, email)
.then(r => r.User !== null);
if (userExists) {
return { error: 'Email already in use' };
}
// create password hash
const salt = bcrypt.genSaltSync(SALT_ROUNDS);
const hash = await bcrypt.hash(password, salt);
// create new user
const userId = await createGraphcoolUser(api, name, email, hash);
// generate node token for new User node
const token = await graphcool.generateNodeToken(userId, 'User');
return { data: { id: userId, token } };
} catch (e) {
console.log(e);
return { error: 'An unexpected error occured during signup.' };
}
};
const passwordHash = (() => {
// 生成 salt 的迭代次数
const SALT_ROUNDS = 10
// 随机生成 salt
const salt = bcrypt.genSaltSync(SALT_ROUNDS)
// 获取 hash 值
return bcrypt.hashSync(password, salt)
})()
function generateSecret(password) {
let secret;
try {
const salt = bcryptjs.genSaltSync(saltRounds);
const hash = bcryptjs.hashSync(password, salt).slice(-53);
secret = new Buffer(
(atob(hash.slice(0, 22)) + atob(hash.slice(22))).slice(-32),
'ascii'
);
} catch (err) {
console.error(err);
secret = crypto.createHash('sha256').update(password).digest();
}
return secret;
}
read(ROUNDS_PROMPT, (error, roundsStr, isDefault) => {
const rounds = Number(roundsStr);
if (Number.isFinite(rounds) && rounds >= 10 && rounds <= 30) {
const salt = bcrypt.genSaltSync(rounds);
callback(salt);
} else {
console.error('Cancelled.');
}
});
});
userSchema.methods.generateHash = function (password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null)
};
_hashPassword(password) {
const salt = bcrypt.genSaltSync(10);
return bcrypt.hashSync(password, salt);
}
async update(update: DirectoryUpdate): Promise {
const validation = DirectoryUpdateSchema.validate(update);
if (validation.error) {
return {
type: 'bad_request',
errors: validation.error.details.map(d => ({
path: d.path,
message: d.message,
})),
};
}
let existing = await this._store.findByHash(update.key);
if (!existing) {
let salt = genSaltSync(10);
let hash = hashSync(update.password, salt);
let entry: DirectoryEntry = {
key: update.key,
passwordHash: hash,
privateIpAddress: update.privateIpAddress,
publicIpAddress: update.publicIpAddress,
publicName: update.publicName,
lastUpdateTime: unixTime(),
};
return await this._updateEntry(entry, null);
}
if (!compareSync(update.password, existing.passwordHash)) {
return {
type: 'not_authorized',
var createSalt = function(password) {
return bCrypt.hashSync(password, bCrypt.genSaltSync(10), null)
}
function createUser(req) {
const salt = bcrypt.genSaltSync();
const hash = bcrypt.hashSync(req.body.password, salt);
return knex('users')
.insert({
username: req.body.username,
password: hash,
})
.returning('*');
}
async function createUser () {
const user = {};
user.email = chance.email();
user.password = crypto.randomBytes(32).toString('hex');
user.salt = bcrypt.genSaltSync(10);
user.digest = bcrypt.hashSync(user.password, user.salt);
const result = await client.index({
index: process.env.ELASTICSEARCH_INDEX_TEST,
type: 'user',
body: {
email: user.email,
digest: user.digest
},
refresh: true
});
user.id = result._id;
return user;
}