Tech TutorBangla Web Documentation
Portfolio
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Node.js Documentation20টি অধ্যায়
প্রযুক্তি নির্বাচন
HomeHTMLCSSTailwind CSSJavaScriptTypeScriptReactNext.jsNode.jsExpressSocket.IOExpoWordPressPostgreSQLPrismaPostgresNestJSMongoDBDockerTanStackTanStack TableRTK QueryGitGitHub
  • Introduction
  • Installation
  • Modules
  • NPM
  • File System
  • Events
  • Streams
  • HTTP
  • Async Programming
  • Environment
  • Debugging
  • Express.js
  • API Development
  • Authentication
  • Database
  • Production
  • Advanced Node.js
  • Best Practices
  • Projects
  • Interview
  1. Docs
  2. Node.js
  3. Authentication — JWT ও Session
Node.jsAdvanced

Authentication — JWT ও Session

JWT, bcrypt, session, middleware authentication — Node.js/Express-এ user authentication বাংলায়।

12টি অংশবাংলা ভাষায়

Authentication হলো verify করা যে user কে সে claim করছে। Node.js-এ সবচেয়ে জনপ্রিয় পদ্ধতি হলো JWT (JSON Web Token)।

JWT কী?

JWT হলো একটি token যা server user-কে দেয় login সময়। পরবর্তী request-এ user এই token পাঠায় এবং server verify করে। Token তিনটি অংশে থাকে: Header, Payload, Signature।

JWT ইনস্টল

npm install jsonwebtoken bcryptjs

User Registration

routes/auth.jsjavascript
javascript
const express = require("express");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const router = express.Router();

const users = [];
const JWT_SECRET = process.env.JWT_SECRET || "secret-key";

// Register
router.post("/register", async (req, res) => {
  const { name, email, password } = req.body;

  // check existing
  if (users.find(u => u.email === email)) {
    return res.status(400).json({ error: "Email ইতিমধ্যে আছে" });
  }

  // hash password
  const hashedPassword = await bcrypt.hash(password, 10);

  const user = {
    id: Date.now(),
    name,
    email,
    password: hashedPassword,
  };
  users.push(user);

  res.status(201).json({ message: "Registration successful" });
});

User Login & JWT

// Login
router.post("/login", async (req, res) => {
  const { email, password } = req.body;

  const user = users.find(u => u.email === email);
  if (!user) {
    return res.status(401).json({ error: "Invalid credentials" });
  }

  const isMatch = await bcrypt.compare(password, user.password);
  if (!isMatch) {
    return res.status(401).json({ error: "Invalid credentials" });
  }

  // JWT token তৈরি
  const token = jwt.sign(
    { id: user.id, email: user.email },
    JWT_SECRET,
    { expiresIn: "1h" }
  );

  res.json({ token, user: { id: user.id, name: user.name } });
});

Auth Middleware

// middleware/auth.js
const authenticate = (req, res, next) => {
  const token = req.headers.authorization?.split(" ")[1];

  if (!token) {
    return res.status(401).json({ error: "Token নেই" });
  }

  try {
    const decoded = jwt.verify(token, JWT_SECRET);
    req.user = decoded;
    next();
  } catch (error) {
    res.status(401).json({ error: "Invalid token" });
  }
};

// protected route
router.get("/profile", authenticate, (req, res) => {
  res.json({ user: req.user });
});
নিরাপত্তা

JWT secret key কখনো code-এ hardcode করবেন না — সবসময় environment variable ব্যবহার করুন। Production-এ strong secret ব্যবহার করুন।

শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।
আগেরREST API DevelopmentপরেরNode.js Database Integration
এই পেজে
Tech Tutor

বাংলায় ওয়েব ডেভেলপমেন্ট ডকুমেন্টেশন শিখুন — ধাপে ধাপে।

Homeসব ডকুমেন্টেশনQuick Startআমার নোটস

© 2026 Tech Tutor. বাংলায় শিখুন, বাংলায় গড়ুন।

mdanamolhasan1998@gmail.com