Skip to content

Instantly share code, notes, and snippets.

@tonyspiro
Created July 27, 2020 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyspiro/5a102e418f371c2d771430b0c90acee0 to your computer and use it in GitHub Desktop.
Save tonyspiro/5a102e418f371c2d771430b0c90acee0 to your computer and use it in GitHub Desktop.
app.post('/users', (req, res) => {
const data = req.body
async.series([
callback => {
let user_found = false
Cosmic.getObjectType({ bucket: { slug: config.COSMIC_BUCKET } }, { type_slug: 'users' }, (err, response) => {
_.forEach(response.objects.all, user => {
if (_.find(user.metafields, { key: 'email', value: data.email.trim() }))
user_found = true
})
if (!user_found)
return callback()
// User found
return res.status(409).json({ status: 'error', message: 'Email already in use' })
})
},
callback => {
bcrypt.hash(data.password, saltRounds, function(err, hash) {
res.locals.hash = hash
callback()
})
},
callback => {
// Send to Cosmic
const object = {
type_slug: 'users',
title: data.full_name,
metafields: [
{
title: 'First name',
key: 'first_name',
type: 'text',
value: data.first_name
},
{
title: 'Last name',
key: 'last_name',
type: 'text',
value: data.last_name
},
{
title: 'Password',
key: 'password',
type: 'text',
value: res.locals.hash
},
{
title: 'Email',
key: 'email',
type: 'text',
value: data.email.trim().toLowerCase()
}
]
}
if (config.COSMIC_WRITE_KEY)
object.write_key = config.COSMIC_WRITE_KEY
Cosmic.addObject({ bucket: { slug: config.COSMIC_BUCKET } }, object, (err, response) => {
if (err)
res.status(500).json({ status: 'error', data: response })
else
res.json({ status: 'success', data: response })
res.end()
})
}
])
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment