Simple Auth Part I (Node)

Authentication is hard. We will try to get down to basics here, hang on to your belts.

Implementing your own authentication system is rarely a good idea, but we still will describe the basic process, for the purposes of illustration:

  1. Front End:

    1. create a form with user and password

    2. send the data to an express route

  2. Back End

    1. in Express, check if the user and password match

    2. if yes, send back a unique id we can use to identify the user

    3. if no, send back an error

  3. Front-end

    1. when an answer is received, if positive, store the id in a cookie (localStorage, ...)

    2. from now on, send the id with each request so the server can recognize the user

There are many, many, many different moving parts to keep track of, including which cryptographic algorithm to use, how to handle password resets, and so on. It is never a good idea to try to do that, unless you are a seasoned professional.

In general, you'll be using Passport. Passport allows to implement relatively easily different strategies, including logging in through Google, Twitter, Github, Facebook, and 502 other ones. However, the documentation of Passport is so notoriously bad that unrelated users maintain an external documentation just to make things easier for everyone.

Before you will implement proper Authentication in your Node app, read Your Node Authentication Tutorial Is Wrong first, and take inspiration from this implementation. If possible at all, use a third party like Firebase or Auth0.

Here's the most important advice: It's impossible to steal your gold if you have no gold; similarly, always thrive to store only the least possible amount of information on your server. Delegate the responsibility of logins to a 3rd party, whenever possible. Actually, don't have logins at all when your project allows it.

Here's the logic for a request to a page that should be behind a login:

Here is the same flow in graph form:

Let's implement that.

We're going to need:

  1. a place to store tokens. This will be a simple javascript object (later on, we can store those in the database or wherever convenient)

  2. a way to authenticate users. We'll just have a static list of usernames & passes (later, we can switch to a database for this too)

  3. a way to login

  4. a way to ensure a user is logged in

You may not know, but routes in Express are stackable. That is, if you do

In the scenario above, the request to /routewill go through first, and second, but never third. A request to /route?pass=ok will pass by all three first handlers, but not the fourth.

However, calling next with an error

...will skip all handlers until the last.

This allows us to do logged in checks relatively easily, in this way:

Armed with this information, let's do this. Create a new file, auth.js

Import the methods in index.js

Remember to place those methods above the error handler.

Now, let's try this! Run the backend, and, in your browser, open:

Tada, you have a login system.

Once you verified everything works to your liking, move to calling the authentication methods React-side

Last updated

Was this helpful?