Introduction to OAuth 2.0 and JWT: What Are They and Why Do You Need Them?
\nModern web applications rarely function without an authorization system. Whether it's logging in via Google, accessing an API, or protecting routes in your Node.js application — you will need OAuth 2.0 and JWT (JSON Web Tokens). These technologies have become the de facto standard for secure data exchange between client and server.
\nIn this article, we will break down how OAuth 2.0 and JWT work and build a simple authorization system in JavaScript (Node.js). The material is designed for beginner developers, so we will move from theory to practice with detailed code examples.
\n\nPart 1: What is OAuth 2.0 and How Does It Work?
\nOAuth 2.0 is an authorization protocol that allows applications to obtain limited access to user resources without sharing a password. Imagine you want to log into a website using your Google account. OAuth 2.0 is exactly what handles this process.
\nMain Roles in OAuth 2.0
\n- \n
- Resource Owner — the user who owns the data. \n
- Client — your application (e.g., a Node.js server). \n
- Authorization Server — the server that issues tokens (e.g., Google, GitHub). \n
- Resource Server — the server that stores user data. \n
Typical OAuth 2.0 Flow (Authorization Code Grant)
\n- \n
- The user clicks "Sign in with Google". \n
- The application redirects them to the Google authorization server. \n
- The user grants consent. \n
- Google returns an
authorization codeto the application server. \n - The server exchanges this code for an
access token. \n - The server uses the access token to access the user's data. \n
In practice, for simple applications, JWT is often used as the access token format.
\n\nPart 2: JWT — Structure and How It Works
\nJWT (JSON Web Token) is a compact and self-contained way to transmit information between parties as a JSON object. The token is signed, so it can be verified without querying a database.
\nJWT Structure
\nA JWT consists of three parts, separated by dots (.):
header.payload.signature\n- \n
- Header — contains the token type and signing algorithm (e.g., HS256). \n
- Payload — the data (claims), such as user ID, role, expiration time. \n
- Signature — a signature created using a secret key. \n
Example of a Decoded JWT
\n{\n "alg": "HS256",\n "typ": "JWT"\n}\n.\n{\n "sub": "1234567890",\n "name": "John Doe",\n "iat": 1516239022,\n "exp": 1516242622\n}\n.\n[signature]\nImportant: the payload is not encrypted, only signed. Never store confidential data (passwords) in a JWT!
\n\nPart 3: Implementing JWT Authorization in Node.js
\nNow let's move to practice. We will create a simple REST API using Express.js with JWT authorization. For this, we will need the jsonwebtoken and bcryptjs libraries.
Step 1: Installing Dependencies
\nnpm init -y\nnpm install express jsonwebtoken bcryptjs dotenv\nnpm install --save-dev nodemon\nStep 2: Basic Server Setup
\n// server.js\nrequire('dotenv').config();\nconst express = require('express');\nconst jwt = require('jsonwebtoken');\nconst bcrypt = require('bcryptjs');\n\nconst app = express();\napp.use(express.json());\n\nconst users = []; // In a real project — a database\n\nconst PORT = process.env.PORT || 3000;\nconst JWT_SECRET = process.env.JWT_SECRET || 'mysecretkey123';\n\napp.listen(PORT, () => {\n console.log(`Server running on port ${PORT}`);\n});