-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide epilogue away in users and show how to use it alongside custom r…
…outes.
- Loading branch information
1 parent
ac5f92a
commit 977e122
Showing
3 changed files
with
56 additions
and
41 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,17 @@ | ||
'use strict' | ||
const epilogue = require('epilogue') | ||
|
||
const db = require('APP/db') | ||
const api = require('express').Router() | ||
const api = module.exports = require('express').Router() | ||
|
||
api | ||
.get('/heartbeat', (req, res) => res.send({ok: true,})) | ||
.use('/auth', require('./auth')) | ||
|
||
// Epilogue can make routes for us | ||
epilogue.initialize({app: api, sequelize: db}) | ||
|
||
var users = epilogue.resource({ | ||
model: db.model('users'), | ||
endpoints: ['/users', '/users/:id'] | ||
}); | ||
|
||
const mustBeLoggedIn = (req, res, context) => { | ||
if (!req.user) { | ||
res.status(401).send('You must be logged in') | ||
return context.stop | ||
} | ||
|
||
return context.continue | ||
} | ||
|
||
const selfOnly = action => (req, res, context) => { | ||
if (req.params.id !== req.user.id) { | ||
res.status(403).send(`You can only ${action} yourself.`) | ||
return context.stop | ||
} | ||
return context.continue | ||
} | ||
|
||
const forbidden = message => (req, res, context) => { | ||
res.status(403).send(message) | ||
return context.stop | ||
} | ||
|
||
users.delete.auth(mustBeLoggedIn) | ||
users.delete.auth(selfOnly) | ||
users.list.auth(forbidden) | ||
users.read.auth(mustBeLoggedIn) | ||
|
||
.use('/users', require('./users')) | ||
|
||
// Send along any errors | ||
api.use((err, req, res, next) => { | ||
res.status(500).send(err) | ||
}) | ||
|
||
// No routes matched? 404. | ||
api.use((req, res) => res.status(404).end()) | ||
|
||
module.exports = api | ||
api.use((req, res) => res.status(404).end()) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const epilogue = require('epilogue') | ||
const api = require('./api') | ||
const db = require('APP/db') | ||
|
||
// Epilogue can make routes for us | ||
epilogue.initialize({app: api, sequelize: db}) | ||
|
||
const mustBeLoggedIn = (req, res, context) => { | ||
if (!req.user) { | ||
res.status(401).send('You must be logged in') | ||
return context.stop | ||
} | ||
|
||
return context.continue | ||
} | ||
|
||
const selfOnly = action => (req, res, context) => { | ||
if (req.params.id !== req.user.id) { | ||
res.status(403).send(`You can only ${action} yourself.`) | ||
return context.stop | ||
} | ||
return context.continue | ||
} | ||
|
||
const forbidden = message => (req, res, context) => { | ||
res.status(403).send(message) | ||
return context.stop | ||
} | ||
|
||
epilogue.filters = {mustBeLoggedIn, selfOnly, forbidden,} | ||
module.exports = epilogue |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
const epilogue = require('./epilogue') | ||
const db = require('APP/db') | ||
|
||
const customUserRoutes = require('express').Router() | ||
|
||
// Custom routes go here. | ||
|
||
module.exports = customUserRoutes | ||
|
||
// Epilogue will automatically create standard RESTful routes | ||
const users = epilogue.resource({ | ||
model: db.model('users'), | ||
endpoints: ['/users', '/users/:id'] | ||
}) | ||
|
||
const {mustBeLoggedIn, selfOnly, forbidden} = epilogue.filters | ||
users.delete.auth(mustBeLoggedIn) | ||
users.delete.auth(selfOnly) | ||
users.list.auth(forbidden) | ||
users.read.auth(mustBeLoggedIn) |