This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// logout.js | |
module.exports = (app, config, partials) => { | |
app.get('/logout', (req, res) => { | |
req.session.destroy() | |
return res.redirect('/') | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To keep things concise, use the following bucket variable for the examples. | |
const bucket = Cosmic.bucket({ | |
slug: 'bucket-slug', | |
read_key: "your-read-key-found-in-bucket-settings" | |
}) | |
// Matches Objects with exact title | |
bucket.getObjects({ | |
type: 'posts', | |
props: 'slug,title,content', |