Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async run () {
const users = await api.users.list()
const posts = await api.users.postsList()
api.log('*** STATUS ***', 'info', { users: users.length, posts: posts.length })
}
}
async initialize () {
const redis = api.redis.clients.client
api.users = {}
api.users.add = async (userName, password) => {
const savedUser = await redis.hget(this.usersHash, userName)
if (savedUser) { throw new Error('userName already exists') }
const hashedPassword = await api.users.cryptPassword(password)
const data = {
userName: userName,
hashedPassword: hashedPassword,
createdAt: new Date().getTime()
}
await redis.hset(this.usersHash, userName, JSON.stringify(data))
}
api.users.list = async () => {
const userData = await redis.hgetall(this.usersHash)
return Object.keys(userData).map((k) => {
const data = JSON.parse(userData[k])
async run () {
const users = await api.users.list()
const posts = await api.users.postsList()
api.log('*** STATUS ***', 'info', { users: users.length, posts: posts.length })
}
}
async run ({ response, params }) {
response.authenticated = await api.users.authenticate(params.userName, params.password)
if (!response.authenticated) { throw new Error('unable to log in') }
}
}
async run ({ response, params }) {
const users = await api.users.list()
response.users = users.map((user) => { return user.userName })
}
}
}
}
api.users.delete = async (userName, password) => {
await redis.del(this.usersHash, userName)
const titles = await api.blog.postsList(userName)
for (const i in titles) {
await api.blog.deletePost(userName, titles[i])
}
}
api.users.cryptPassword = async (password) => {
return bcrypt.hash(password, this.saltRounds)
}
api.users.comparePassword = async (hashedPassword, userPassword) => {
return bcrypt.compare(userPassword, hashedPassword)
}
}
async run ({ params }) {
await api.users.delete(params.userName, params.password)
}
}
createdAt: new Date().getTime()
}
await redis.hset(this.usersHash, userName, JSON.stringify(data))
}
api.users.list = async () => {
const userData = await redis.hgetall(this.usersHash)
return Object.keys(userData).map((k) => {
const data = JSON.parse(userData[k])
delete data.hashedPassword
return data
})
}
api.users.authenticate = async (userName, password) => {
try {
let data = await redis.hget(this.usersHash, userName)
data = JSON.parse(data)
return api.users.comparePassword(data.hashedPassword, password)
} catch (error) {
throw new Error(`userName does not exist (${error})`)
}
}
api.users.delete = async (userName, password) => {
await redis.del(this.usersHash, userName)
const titles = await api.blog.postsList(userName)
for (const i in titles) {
await api.blog.deletePost(userName, titles[i])
}
}
data = JSON.parse(data)
return api.users.comparePassword(data.hashedPassword, password)
} catch (error) {
throw new Error(`userName does not exist (${error})`)
}
}
api.users.delete = async (userName, password) => {
await redis.del(this.usersHash, userName)
const titles = await api.blog.postsList(userName)
for (const i in titles) {
await api.blog.deletePost(userName, titles[i])
}
}
api.users.cryptPassword = async (password) => {
return bcrypt.hash(password, this.saltRounds)
}
api.users.comparePassword = async (hashedPassword, userPassword) => {
return bcrypt.compare(userPassword, hashedPassword)
}
}
api.users = {}
api.users.add = async (userName, password) => {
const savedUser = await redis.hget(this.usersHash, userName)
if (savedUser) { throw new Error('userName already exists') }
const hashedPassword = await api.users.cryptPassword(password)
const data = {
userName: userName,
hashedPassword: hashedPassword,
createdAt: new Date().getTime()
}
await redis.hset(this.usersHash, userName, JSON.stringify(data))
}
api.users.list = async () => {
const userData = await redis.hgetall(this.usersHash)
return Object.keys(userData).map((k) => {
const data = JSON.parse(userData[k])
delete data.hashedPassword
return data
})
}
api.users.authenticate = async (userName, password) => {
try {
let data = await redis.hget(this.usersHash, userName)
data = JSON.parse(data)
return api.users.comparePassword(data.hashedPassword, password)
} catch (error) {
throw new Error(`userName does not exist (${error})`)
}