Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add authentication protection to books creation #11

Merged
merged 2 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/middlewares/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function checkAuth(ctx, next) {
const { currentUser } = ctx.state;
if (!currentUser) ctx.throw(401);
return next();
}

module.exports = {
checkAuth,
};
43 changes: 28 additions & 15 deletions src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,37 @@ const session = require('./routes/session');
const router = new KoaRouter();

router.use(async (ctx, next) => {
if (ctx.session.currentUserId){
ctx.state.currentUser = await ctx.orm.user.findByPk(ctx.session.currentUserId);
try {
await next();
} catch (err) {
switch (err.status) {
case 401:
ctx.app.emit('error', err, ctx);
ctx.redirect(ctx.router.url('session.new'));
break;
default:
throw err;
}
return next();
});

}
});

router.use(async (ctx, next) => {
if (ctx.session.currentUserId) {
ctx.state.currentUser = await ctx.orm.user.findByPk(ctx.session.currentUserId);
}
return next();
});

router.use(async (ctx, next) => {
Object.assign(ctx.state, {
paths: {
destroySession: ctx.router.url('session.destroy'),
newSession: ctx.router.url('session.new'),

},
});
return next();
Object.assign(ctx.state, {
paths: {
destroySession: ctx.router.url('session.destroy'),
newSession: ctx.router.url('session.new'),

},
});

return next();
});

router.use('/', index.routes());
router.use('/hello', hello.routes());
Expand All @@ -35,5 +49,4 @@ router.use('/books', books.routes());
router.use('/authors/:authorId/books', booksForAuthors.routes());
router.use('/session', session.routes());


module.exports = router;
3 changes: 3 additions & 0 deletions src/routes/books.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const KoaRouter = require('koa-router');
const { checkAuth } = require('../middlewares/auth');

const router = new KoaRouter();

router.use(checkAuth);

router.get('books.new', '/new', async (ctx) => {
const book = ctx.orm.book.build();
const authorList = await ctx.orm.author.findAll();
Expand Down
3 changes: 3 additions & 0 deletions src/routes/booksForAuthors.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const KoaRouter = require('koa-router');
const { checkAuth } = require('../middlewares/auth');

const router = new KoaRouter();

router.use(checkAuth);

async function getAuthor(ctx, next) {
ctx.state.author = await ctx.orm.author.findByPk(ctx.params.authorId);
if (!ctx.state.author) return ctx.throw(404);
Expand Down
4 changes: 3 additions & 1 deletion src/views/authors/show.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
<li><%= book.title %></li>
<% }) %>
</ul>
<a href="<%= newBookPath %>">Agregar libro</a>
<% if (locals.currentUser) { %>
<a href="<%= newBookPath %>">Agregar libro</a>
<% } %>
<% } %>
4 changes: 3 additions & 1 deletion src/views/index.html.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<%= appVersion %>
</p>
<div>Esta es una aplicaición con código de ejemplo para el uso del template, que no considera estilos. <a href="<%=authorsPath%>">Ver autores</a></div>
<a href="<%= newBookPath %>">Agregar libro</a>
<% if (locals.currentUser) { %>
<a href="<%= newBookPath %>">Agregar libro</a>
<% } %>
<br>
<div id="react-app">Here a React app will be rendered</div>