Skip to content

Commit

Permalink
Auth middleware & dashboard stories
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Traversy authored and Brad Traversy committed Jun 19, 2020
1 parent b5a2c68 commit 1b3ad25
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 4 deletions.
12 changes: 12 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}

// Handlebars Helpers
const { formatDate } = require('./helpers/hbs')

// Handlebars
app.engine(
'.hbs',
exphbs({
helpers: {
formatDate,
},
defaultLayout: 'main',
extname: '.hbs',
})
Expand All @@ -48,6 +54,12 @@ app.use(
app.use(passport.initialize())
app.use(passport.session())

// Set global var
app.use(function (req, res, next) {
res.locals.user = req.user || null
next()
})

// Static folder
app.use(express.static(path.join(__dirname, 'public')))

Expand Down
7 changes: 7 additions & 0 deletions helpers/hbs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const moment = require('moment')

module.exports = {
formatDate: function (date, format) {
return moment(date).utc().format(format)
},
}
16 changes: 16 additions & 0 deletions middleware/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
ensureAuth: function (req, res, next) {
if (req.isAuthenticated()) {
return next()
} else {
res.redirect('/')
}
},
ensureGuest: function (req, res, next) {
if (req.isAuthenticated()) {
res.redirect('/dashboard')
} else {
return next()
}
},
}
28 changes: 28 additions & 0 deletions models/Story.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const mongoose = require('mongoose')

const StorySchema = new mongoose.Schema({
title: {
type: String,
required: true,
trim: true,
},
body: {
type: String,
required: true,
},
status: {
type: String,
default: 'public',
enum: ['public', 'private'],
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
},
createdAt: {
type: Date,
default: Date.now,
},
})

module.exports = mongoose.model('Story', StorySchema)
18 changes: 15 additions & 3 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
const express = require('express')
const router = express.Router()
const { ensureAuth, ensureGuest } = require('../middleware/auth')

const Story = require('../models/Story')

// @desc Login/Landing page
// @route GET /
router.get('/', (req, res) => {
router.get('/', ensureGuest, (req, res) => {
res.render('login', {
layout: 'login',
})
})

// @desc Dashboard
// @route GET /dashboard
router.get('/dashboard', (req, res) => {
res.render('dashboard')
router.get('/dashboard', ensureAuth, async (req, res) => {
try {
const stories = await Story.find({ user: req.user.id }).lean()
res.render('dashboard', {
name: req.user.firstName,
stories,
})
} catch (err) {
console.error(err)
res.render('error/500')
}
})

module.exports = router
39 changes: 38 additions & 1 deletion views/dashboard.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
<h1>Dashboard</h1>
<h6>Dashboard</h6>
<h3>Welcome {{name}}</h3>
<p>Here are your stories</p>
{{#if stories}}
<table class="striped">
<thead>
<tr>
<th>Title</th>
<th>Date</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
{{#each stories}}
<tr>
<td><a href="/stories/{{_id}}">{{title}}</a></td>
<td>{{formatDate createdAt 'MMMM Do YYYY, h:mm:ss a'}}</td>
<td><span class="dash-status">{{status}}</span></td>
<td>
<a href="/stories/edit/{{_id}}" class="btn btn-float">
<i class="fas fa-edit"></i>
</a>

<form action="/stories/{{_id}}" method="POST" id="delete-form">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn red">
<i class="fas fa-trash"></i>
</button>
</form>
</td>
</tr>
{{/each}}
</tbody>
</table>
{{else}}
<p>You have not created any stories</p>
{{/if}}

0 comments on commit 1b3ad25

Please sign in to comment.