Created
November 9, 2015 23:19
-
-
Save kulakowka/9a336e29b5025e3549b9 to your computer and use it in GitHub Desktop.
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
import {Router} 'express' | |
import {auth, logged} from 'actions/auth' | |
import {list, show} from 'actions/users' | |
import {render} from 'actions/common' | |
export default const router = Router() | |
router.use(auth) | |
router.get('/items', list, render('list')) | |
router.get('/items/new', logged, show, render('show')) | |
router.get('/items/:id', show, render('show')) | |
// actions/users/index.js | |
module.exports = { | |
show: (req, res, next) => { | |
User | |
.findOne({username: req.params.username}) | |
.exec() | |
.catch(next) | |
.then(user => { | |
res.locals.user = user | |
next() | |
}) | |
}, | |
list: (req, res, next) => { | |
User | |
.find() | |
.order({createdAt: -1}) | |
.exec() | |
.catch(next) | |
.then(users => { | |
res.locals.users = users | |
next() | |
}) | |
} | |
} | |
// actions/common/index.js | |
module.exports = { | |
render: (name) => { | |
return (req, res, next) => res.render(name) | |
} | |
} | |
// actions/auth/index.js | |
module.exports = { | |
auth: (req, res, next) => { | |
res.locals.currentUser = req.user | |
next() | |
}, | |
logged: (req, res, next) => { | |
if (req.user) return next() | |
var err = new Error('Not authorised') | |
err.status = 400 | |
next(err) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment