Skip to content

Commit

Permalink
adds example route using Boom.unauthorized for simplified errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nkamc committed Jun 12, 2014
1 parent 9fed5ae commit cabe17b
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 12 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,6 @@ Next write a test in ./test/**test.js**
(If you aren't used to "Test First" - ***trust*** the process...)

```
var Lab = require("lab"), // the Lab
server = require("../"); // require index.js
Lab.experiment("Authentication Required to View Photo", function() {
// tests
Lab.test("Deny view of photo if unauthenticated /photo/{id*} ", function(done) {
Expand Down Expand Up @@ -302,13 +300,30 @@ The right way is to create a generic route which responds to any request for a p
And since we don't currently have any authentication set up, we ***mock*** (fake) it.
(Don't worry we will get to the authentication in the next step...)

```
var Boom = require('boom');
server.route({
method: 'GET',
path: '/photo/{id*}',
config: { // validate will ensure YOURNAME is valid before replying to your request
validate: { params: { id: Joi.string().max(40).min(2).alphanum() } },
handler: function (req,reply) {
// until we implement authentication we are simply returning a 401:
reply(Boom.unauthorized('Please log-in to see that'));
// the key here is our use of the Boom.unauthorised method.
}
}
});
```


Now our test passes and we can move on to implementing the authentication.


### Authentication



https://github.com/spumko/hapi-auth-cookie


Expand Down
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ server.route({
}
});

var Boom = require('boom'); // Todo: add boom to the top of the file
server.route({
method: 'GET',
path: '/photo/{id*}',
config: { // validate will ensure YOURNAME is valid before replying to your request
validate: { params: { id: Joi.string().max(40).min(2).alphanum() } },
handler: function (req,reply) {
// until we implement authentication we are simply returning a 401:
reply(Boom.unauthorized('Please log-in to see that'));
// lookup the photo in database
// check if the user should be able to see it
// reply with actual url of the image
}
}
});

server.start(function() {
console.log('Now Visit: http://localhost:3000/YOURNAME')
});
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"dependencies": {
"hapi": "^5.1.0",
"joi": "^4.5.0",
"socket.io": "^1.0.4"
"socket.io": "^1.0.4",
"boom": "^2.4.2"
},
"devDependencies": {
"lab": "^3.2.1"
Expand Down
144 changes: 136 additions & 8 deletions test/coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ <h1 id="overview">Code Coverage Report</h1>
</div>
<div class="stats high">
<div class="percentage">100%</div>
<div class="sloc">19</div>
<div class="hits">19</div>
<div class="sloc">34</div>
<div class="hits">34</div>
<div class="misses">0</div>
</div>
<div id="files">
Expand All @@ -270,8 +270,8 @@ <h1 id="overview">Code Coverage Report</h1>
<h2 id="index.js">index.js</h2>
<div class="stats high">
<div class="percentage">100%</div>
<div class="sloc">19</div>
<div class="hits">19</div>
<div class="sloc">34</div>
<div class="hits">34</div>
<div class="misses">0</div>
</div>
<table>
Expand Down Expand Up @@ -440,37 +440,165 @@ <h2 id="index.js">index.js</h2>
<tr class="hit">
<td class="line">20</td>
<td class="hits">1</td>
<td class="source">server.start(function() {</td>
<td class="source">var Boom = require(&#x27;boom&#x27;); // Todo: add boom to the top of the file</td>
</tr>



<tr class="hit">
<td class="line">21</td>
<td class="hits">1</td>
<td class="source"> console.log(&#x27;Now Visit: http://localhost:3000/YOURNAME&#x27;)</td>
<td class="source">server.route({ </td>
</tr>



<tr class="hit">
<td class="line">22</td>
<td class="hits"></td>
<td class="source">});</td>
<td class="source"> method: &#x27;GET&#x27;,</td>
</tr>



<tr class="hit">
<td class="line">23</td>
<td class="hits"></td>
<td class="source"></td>
<td class="source"> path: &#x27;/photo/{id*}&#x27;,</td>
</tr>



<tr class="hit">
<td class="line">24</td>
<td class="hits"></td>
<td class="source"> config: { // validate will ensure YOURNAME is valid before replying to your request</td>
</tr>



<tr class="hit">
<td class="line">25</td>
<td class="hits"></td>
<td class="source"> validate: { params: { id: Joi.string().max(40).min(2).alphanum() } },</td>
</tr>



<tr class="hit">
<td class="line">26</td>
<td class="hits"></td>
<td class="source"> handler: function (req,reply) {</td>
</tr>



<tr class="hit">
<td class="line">27</td>
<td class="hits"></td>
<td class="source"> // until we implement authentication we are simply returning a 401:</td>
</tr>



<tr class="hit">
<td class="line">28</td>
<td class="hits">1</td>
<td class="source"> reply(Boom.unauthorized(&#x27;Please log-in to see that&#x27;));</td>
</tr>



<tr class="hit">
<td class="line">29</td>
<td class="hits"></td>
<td class="source"> // lookup the photo in database</td>
</tr>



<tr class="hit">
<td class="line">30</td>
<td class="hits"></td>
<td class="source"> // check if the user should be able to see it</td>
</tr>



<tr class="hit">
<td class="line">31</td>
<td class="hits"></td>
<td class="source"> // reply with actual url of the image</td>
</tr>



<tr class="hit">
<td class="line">32</td>
<td class="hits"></td>
<td class="source"> }</td>
</tr>



<tr class="hit">
<td class="line">33</td>
<td class="hits"></td>
<td class="source"> }</td>
</tr>



<tr class="hit">
<td class="line">34</td>
<td class="hits"></td>
<td class="source">});</td>
</tr>



<tr class="hit">
<td class="line">35</td>
<td class="hits"></td>
<td class="source"></td>
</tr>



<tr class="hit">
<td class="line">36</td>
<td class="hits">1</td>
<td class="source">server.start(function() {</td>
</tr>



<tr class="hit">
<td class="line">37</td>
<td class="hits">1</td>
<td class="source"> console.log(&#x27;Now Visit: http://localhost:3000/YOURNAME&#x27;)</td>
</tr>



<tr class="hit">
<td class="line">38</td>
<td class="hits"></td>
<td class="source">});</td>
</tr>



<tr class="hit">
<td class="line">39</td>
<td class="hits"></td>
<td class="source"></td>
</tr>



<tr class="hit">
<td class="line">40</td>
<td class="hits">1</td>
<td class="source">module.exports = server;</td>
</tr>
Expand Down

0 comments on commit cabe17b

Please sign in to comment.