David Vicente Fuentes in javascript, Developers, Programmers Analista-desarrollador Jan 23, 2018 · 1 min read · ~100

Refresh token with JWT authentication in Node.js

When designing a web application, along with security authentication is one of the key parts. Authentication with tokens was a breakthrough in this regard, and the refresh token came to complement it and make it usable.

Authentication

Authentication systems are divided according to how they verify the user:
– Based on something known (password)
– Based on something possessed (identity card, usb, token)
– Based on physical characteristics (voice, fingerprints, eyes)

Token-based authentication

Tokens were introduced into web applications by modern authentication and authorization. We could say that its use was extended thanks to the OAuth protocol (later OAuth2). These were focused on authorization, and not on authentication as one tends to think.

When we talk about authentication with tokens, we can divide it into 2 types:

  • Translated with www.DeepL.com/Translator

    Until recently it has been the most common authentication mode. When a user logs in, the server returns a token that is typically stored in a cookie. The server saves the session information, either in memory or in a database (Redis, MongoDB…).

    Thus, each time the user makes a request with that token, the server searches for information to know which user is trying to access and if it is valid, it executes the requested method.

    This type of authentication has several problems, such as the overload caused by all the information of authenticated users. As well as scalability, since if there are several instances of the server raised, they would have to share in some way the information of the session so as not to make you log in again.

    In addition, there are vulnerabilities due to this architecture (CORS, CSRF).

    Statusless token-based authentication

    In order to solve all these problems, stateless authentication arises. This means that the server will not store any information, nor will the session.
    When the user is authenticated with his credentials or any other method, he receives an access token in the answer (access token). From that moment on, all requests made to the API will carry this token in an HTTP header so that the server can identify which user makes the request without having to search the database or any other storage system.

    With this approach, the application becomes scalable, since it is the client itself that stores its authentication information, and not the server. This way, requests can reach any instance of the server and can be attended without synchronization.

    Different platforms can use the same API

    It also increases security, avoiding CSRF vulnerabilities, as there are no sessions. And if we add expiration to the token the security will be even greater.



https://solidgeargroup.com/refresh-token-with-jwt-authentication-node-js



Refresh token with JWT authentication in Node.js