Project Setup: Part One
Explore how to establish user authentication in a Node.js project by installing necessary dependencies, configuring Passport.js with JWT strategy, and securely managing user data. This lesson helps you understand password hashing with bcrypt and handling sensitive environment variables for token security.
Task 1: Install dependencies
-
Navigate to the project folder.
-
Install the following dependencies:
bcryptexpressjsonwebtokenpassportpassport-jwtdotenv
Task 2: Set up files for user authentication routes
- In the previous chapter, we set up a
router,controller, andservicefor the recipes resource in the Express Recipes API. Let’s follow a similar pattern to organize our user authentication logic:
- Create a new folder to store middleware functions. Within it, create a file called
auth.js:
Later on, we’ll configure the Passport.js strategy inside src/middleware/auth.js.
- We’ll store the users’ data in
db/users.json.
Note: This isn’t a robust database solution since there’s no built-in data validation, but it’s sufficient for demonstration purposes.
Add a user ...