OAuth 2.0 and JWT Authorization in JavaScript: A Complete Guide for Beginners

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to OAuth 2.0 and JWT: What Are They and Why Do You Need Them?

\n

Modern 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.

\n

In 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\n

Part 1: What is OAuth 2.0 and How Does It Work?

\n

OAuth 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.

\n

Main 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
\n

Typical OAuth 2.0 Flow (Authorization Code Grant)

\n
    \n
  1. The user clicks "Sign in with Google".
  2. \n
  3. The application redirects them to the Google authorization server.
  4. \n
  5. The user grants consent.
  6. \n
  7. Google returns an authorization code to the application server.
  8. \n
  9. The server exchanges this code for an access token.
  10. \n
  11. The server uses the access token to access the user's data.
  12. \n
\n

In practice, for simple applications, JWT is often used as the access token format.

\n\n

Part 2: JWT — Structure and How It Works

\n

JWT (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.

\n

JWT Structure

\n

A JWT consists of three parts, separated by dots (.):

\n
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
\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]
\n

Important: the payload is not encrypted, only signed. Never store confidential data (passwords) in a JWT!

\n\n

Part 3: Implementing JWT Authorization in Node.js

\n

Now 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.

\n

Step 1: Installing Dependencies

\n
npm init -y\nnpm install express jsonwebtoken bcryptjs dotenv\nnpm install --save-dev nodemon
\n

Step 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});

                
                

Blogs

Book Recommendations