This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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', |
This file contains hidden or 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 run | |
1. Follow Deno Guide to install https://deno.land/#installation | |
2. Add the following code to a file called cosmic-deno-test.ts | |
3. run the following command: deno run --allow-net=127.0.0.1:8000,api.cosmicjs.com cosmic-deno-test.ts | |
*/ | |
import { serve } from "https://deno.land/[email protected]/http/server.ts"; | |
const s = serve({ port: 8000 }); | |
const bucket_slug = `cosmic-js`; | |
const object_type = `articles`; | |
const limit = 10; |