Skip to content

Instantly share code, notes, and snippets.

Avatar
🚀
Building and shipping

Tony Spiro tonyspiro

🚀
Building and shipping
View GitHub Profile
View webhook-without-props.json
{
"created_at": 1632927978821,
"data": {
"_id": "61547f5e16bbc1ecc42e2bf6",
"order": 0,
"slug": "new-blog-post",
"title": "New Blog Post UPDATE",
"content": "<p>This is my blog post content...</p>",
"metafields": [
{
View webhook-with-props.json
{
"created_at": 1632927654815,
"data": {
"slug": "new-blog-post",
"title": "New Blog Post UPDATE",
"metadata": {
"headline": "Amazing Beach!",
"image": {
"url": "https://cdn.cosmicjs.com/cc633600-2135-11ec-81a5-2b4858c629b3-quino-al-mBQIfKlvowM-unsplash.jpg",
"imgix_url": "https://imgix.cosmicjs.com/cc633600-2135-11ec-81a5-2b4858c629b3-quino-al-mBQIfKlvowM-unsplash.jpg"
View logout.js
// logout.js
module.exports = (app, config, partials) => {
app.get('/logout', (req, res) => {
req.session.destroy()
return res.redirect('/')
})
}
View add-user.js
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
})
View users.js
app.get('/users', (req, res) => {
if(!req.session.user)
return res.redirect('/?message=unauthorized')
res.locals.user = req.session.user
async.series([
callback => {
Cosmic.getObjectType({ bucket: { slug: config.COSMIC_BUCKET } }, { type_slug: 'users' }, (err, response) => {
res.locals.users = response.objects.all
callback()
})
View auth.js
// auth.js
import Cosmic from 'cosmicjs'
import async from 'async'
import _ from 'lodash'
import bcrypt from 'bcrypt'
const saltRounds = 10
module.exports = (app, config, partials) => {
// Submit form
app.post('/auth', (req, res) => {
const data = req.body
View home.js
// home.js
import Cosmic from 'cosmicjs'
module.exports = (app, config, partials) => {
app.get('/', (req, res) => {
Cosmic.getObjects({ bucket: { slug: config.COSMIC_BUCKET } }, (err, response) => {
res.locals.cosmic = response
if (req.query.message === 'unauthorized')
res.locals.unauthorized_message = true
return res.render('index.html', {
partials
View routes.js
// Routes
module.exports = (app, config, partials) => {
require('./home')(app, config, partials)
require('./signup')(app, config, partials)
require('./users')(app, config, partials)
require('./auth')(app, config, partials)
require('./logout')(app, config, partials)
require('./404')(app, config, partials)
}
View app-server.js
// app-server.js
import express from 'express'
import hogan from 'hogan-express'
import http_module from 'http'
import bodyParser from 'body-parser'
import compression from 'compression'
import session from 'express-session'
import config from './config'
import cors from 'cors'
const app = express()