Search⌘ K
AI Features

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

  1. Navigate to the project folder.

  2. Install the following dependencies:

    • bcrypt
    • express
    • jsonwebtoken
    • passport
    • passport-jwt
    • dotenv
Shell
npm install --save bcrypt express jsonwebtoken passport passport-jwt dotenv

Task 2: Set up files for user authentication routes

  1. In the previous chapter, we set up a router, controller, and service for the recipes resource in the Express Recipes API. Let’s follow a similar pattern to organize our user authentication logic:
Shell
touch src/routers/users.js src/services/users.js db/users.json src/controllers/users.js
  1. Create a new folder to store middleware functions. Within it, create a file called auth.js:
Shell
mkdir src/middleware
touch src/middleware/auth.js

Later on, we’ll configure the Passport.js strategy inside src/middleware/auth.js.

  1. We’ll store the users’ data indb/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 ...