#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Authentication

Why and How?

Before writing any login code, it helps to understand why a web app needs authentication at all and how the frontend and backend share the job. Authentication answers one question: who is making this request? Once the server knows that, it can decide what the person is allowed to see or change.

Why a server cannot just trust the browser

HTTP is stateless. Each request arrives at the server on its own, with no built-in memory of the previous one. A request that says "show me order 42" carries no proof of who sent it unless you add that proof yourself. Anyone can open developer tools or a tool like Postman and send any request they like, so the server must verify identity on every single protected request.

That is also why hiding a button in React is never enough. The interface can guide people, but only the server can actually enforce rules.

Two common ways to remember a user

ApproachWhere the proof livesHow each request is checked
Server sessionsA session ID in a cookie; the session data stays on the serverServer looks up the ID in its session store
Tokens (for example JWT)A signed token kept by the clientServer verifies the token's signature and expiry

Sessions work very well for traditional server-rendered sites. Single page applications that talk to a separate REST API often use tokens instead, because the API can verify a token without keeping a session table, and the same token works for a mobile app too.

How the token flow works

  1. The user submits an email and password from the React login form.
  2. The backend finds the user, compares the password against the stored hash, and if it matches, creates a signed token.
  3. The token goes back in the response. The frontend stores it, for example in memory and localStorage.
  4. Every later request to a protected endpoint includes the token, usually in an Authorization header.
  5. Middleware on the server verifies the token before the route handler runs. Invalid or missing token means a 401 response.
// Frontend: attaching the token to a request
const response = await fetch("/api/orders", {
  headers: {
    Authorization: `Bearer ${token}`,
  },
});

if (response.status === 401) {
  // token missing or expired: send the user to login
}

What the frontend is responsible for

  • Collecting credentials and sending them over HTTPS.
  • Keeping the token and attaching it to requests.
  • Showing the right screens and menu items for logged-in and logged-out users.
  • Logging the user out when the token expires or the server returns 401.

A misunderstanding to avoid early

Many beginners think a JWT is encrypted and therefore safe to fill with private data. A standard JWT is only signed, not encrypted. Anyone holding it can decode the payload with a base64 decoder and read it. The signature only proves the server created it and that nobody changed it. Keep payloads small and limited to things like a user ID and role, and never put passwords or personal details inside.

A second mix-up is between authentication and authorisation. Authentication proves identity; authorisation decides permissions. A logged-in user is authenticated, but that does not mean they may delete another user's posts. Your backend needs both checks.

Takeaway: the server must verify identity on every protected request because HTTP forgets everything between calls; tokens carry that proof from the browser, and the frontend's role is to store, send and react to them.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course