DEV Community

Cover image for User authentication with Node.js series: boilerplate
Imad
Imad

Posted on • Originally published at bemoore.hashnode.dev

User authentication with Node.js series: boilerplate

Introduction

User authentication is the core feature of every website and mobile application. Writing authentication the correct and secure way prevents malicious users from accessing sensitive application data.

There are many technologies we can use to enforce secure access to application resources, the most common one is OAuth.

You can read more about OAuth here.

But, for this series, we will build the authentication system from scratch, and make it as simple as possible so it can be further customized and “plugged” with any existing application.


Prerequisites

To ensure a smooth and pleasant experience, please make sure before cloning the starter repository to have the following tools installed:

  • latest version of Node.js
  • latest version of npm
  • git
  • Text editor
  • Terminal

Project repository

In this section, we will clone the starter project hosted on Github, get familiar with the folder structure, and explore the project dependencies.

  1. Open a Terminal session and run >
git clone https://github.com/2imad/node-js-authentication.git
cd node-js-authentication
Enter fullscreen mode Exit fullscreen mode
  1. Install server dependencies

    npm install
    
  2. Install client dependencies

    cd client
    npm install && cd ..
    
  3. Git checkout boilerplate branch

    git checkout boilerplate
    
  4. Open the project with your favorite text editor, at this stage it should look like this:

    |-- node-js-authentication
    |-- config
    |-- db
    |-- mailer
    |-- middlewares
    |-- routes
    |-- .env
    |-- .gitignore
    |-- index.js
    |-- LICENSE
    |-- package-lock.json
    |-- package.json
    |-- README.md
    |-- client
    |   |-- .gitignore
    |   |-- package-lock.json
    |   |-- package.json
    |   |-- README.md
    |   |-- public
    |   |   |-- favicon.ico
    |   |   |-- index.html
    |   |   |-- logo192.png
    |   |   |-- logo512.png
    |   |   |-- manifest.json
    |   |   |-- robots.txt
    |   |-- src
    |       |-- App.css
    |       |-- App.js
    |       |-- App.test.js
    |       |-- index.css
    |       |-- index.js
    |       |-- logo.svg
    |       |-- reportWebVitals.js
    |       |-- setupTests.js
    
  5. Start the development server

    npm run server
    

If you see output like below, it means you are ready to roll :)

[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json  
[nodemon] starting `node index.js`
Listening on 8000

Project dependencies

Here is the current dependency list as found on package.json.
With each list item, you find a link to the package homepage on npm and a brief introduction.

  • bcrypt bcrypt is a powerful hashing function, we will make use of its power to add hashing and salting to user passwords.
  • concurrently This package enables running multiple commands simultaneously.
  • cors Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any other origins (domain, protocol, or port) than its own from which a browser should permit loading of resources. CORS also relies on a mechanism by which browsers make a “preflight” request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request. In that preflight, the browser sends headers that indicate the HTTP method and headers that will be used in the actual request. During development, our client and server are both running on localhost. Most browsers deny cross-origin requests for security reasons, but Cors will help us get around that.
  • dotenv This package enables retrieving environment variables stored in the .env file and using them without risking sensitive data exposure. > Important note: .dotenv file should always be included in .gitignore before committing the code.
  • express express is the module we are using to create a server and configure authentication routes.
  • jsonwebtoken JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA. - source.
  • mongoose Mongoose provides a straight-forward, schema-based solution to model our application data. It includes built-in type casting, validation, query building, business logic hooks, and more, out of the box.
  • nodemailer Nodemailer is a module for Node.js applications to allow easy email sending.
  • validator This library validates and sanitizes strings.

Resources

Finally, some additional resources to help you understand the functionality of each dependency we are using in the project, in case you know any other resources, please do let me know!


Conclusion

In this first part of the series, we talked about authentication with Node.js, cloned the starter repository, and installed the dependencies. Hopefully, you are as excited as I am to get to the next chapter where we will create a MongoDB database and connect it to our project with mongoose, and finally create the signup route.

The next chapter should appear soon, so stay tuned! or follow me and receive it immediately in your mailbox :)

Cheers!

IYO

Bemoore

Top comments (0)