Node.js 4 - Passport custom callback
10 Feb 2015In my previous post, I wrote about how to use passport authentication with Basic Strategy. One thing that bugged me was I cannot return a result right from passport callback. Below is the code from my previous post.
You can see that after we authenticate user, we proceed to the function(req,res){ block. In this block, we have to query information for user again which is redundant work. We can avoid this by using Passport custom callback as below
With this, we can authenticate user and return the result to respond at the same time. However, this might not be your desired behavior for Passport Strategy because it’s against Single responsibility principle.
In the end, it’s up to you to decide which one suits you most. Until next time!