Closed zoe-1 closed 6 years ago
After completing assignment9, we have a general understanding of the request lifecycle and
how to extend it. In this assignment, we will build on that knowledge and extend the request lifecycle for the /login
route using prerequisites. The objective will be to tidy up the /login
route handler.
Assigment9's /login
route handler in ./lib/api/user.js
contains all authentication and session generation logic. This makes the handler a bit cluttered. To clean things up, we move the authentication and session generation logic out of handler into two prerequisite functions: getUser() & createSession(). See prerequisite functions here.
Prerequisite functions are executed right before the handler in the request lifecycle. We will use prerequisites to move complex logic from the handler to the prerequisite functions. Assignment requirements below:
/login
route in lib/api/user.js to use the two prerequisites made in the previous step.internals.getUser()
To make ./lib/api/user.js
more readable, move the internals.getUser()
into it's own file./lib/api/extensions.js
. Then, change ./lib/api/user.js
to include the file and edit the /login
route object's ext
to use the function included from ./lib/api/extensions.js
. This makes ./lib/api/user.js
only contain route objects versus being cluttered with functions used to prepared data for the handles.At completion of assignment10 when we read our /login
route we see:
It is an extremely readable route object. One look gives a nice overview of everything going on in the route. The conciseness and readability show off the beauty of the hapi
request lifecycle design pattern. And, the elegance motivates us to use hapi
to write clean and maintainable code.
Take sometime and look over the request lifecycle document. Then, find each element in the /login
route object which relates to the request lifecycle for this route. Locate where in the lifecycle each element will be executed. The goal is to understand how the route object can be configured to execute methods at different stages in the lifecycle.
focus on:
ext
pre
validate
handler
auth
Assignment code located: relevant documentation test/api/user.js lib/api/prerequisites lib/api/user.js
[x] write the assignment text.
[ ] make final edits to assignment text
[ ] create a file for the assignment in assignments folder. Place the below write up in that file.