// LOADING
// LOADING
// LOADING_ARTICLE
Every secure website needs a way to verify who its users are. This process is called authentication. Over the years, authentication methods have evolved from simple username and password systems to more advanced approaches like OAuth and JWT. Understanding these concepts is essential for modern web developers.
This blog breaks down how authentication works, compares different methods, and shows real code examples that you can apply in your projects.
Authentication is the process of confirming a user’s identity before allowing access to protected parts of an application. Common examples include logging into a website, mobile app, or online dashboard.
A basic authentication flow usually involves
In session based authentication, the server creates a session for the user after login and stores it in memory or a database. A session ID is sent to the user in a cookie.
Each request then includes this cookie so the server can recognize the user.
const express = require("express");
const session = require("express-session");
const app = express();
app.use(session({
secret: "mySecretKey",
resave: false,
saveUninitialized: true
}));
app.post("/login", (req, res) => {
const { username, password } = req.body;
if (username === "admin" && password === "1234") {
req.session.user = username;
res.send("Login successful");
} else {
res.send("Invalid credentials");
}
});
app.get("/dashboard", (req, res) => {
if (req.session.user) {
res.send("Welcome to your dashboard");
} else {
res.send("Please log in first");
}
});
app.listen(3000);
Pros
Cons
JWT stands for JSON Web Token. Instead of storing user data on the server, the server creates a signed token and sends it to the client.
The client stores this token, usually in local storage, and sends it with every request.
const jwt = require("jsonwebtoken");
const token = jwt.sign(
{ user: "admin" },
"secretKey",
{ expiresIn: "1h" }
);
console.log(token);
jwt.verify(token, "secretKey", (err, decoded) => {
if (err) {
console.log("Invalid token");
} else {
console.log("User:", decoded.user);
}
});
Pros
Cons
OAuth is an authorization framework that allows users to log in using third party providers like Google, GitHub, or Facebook.
Instead of creating a new account, users can sign in using an existing account.
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
passport.use(new GoogleStrategy({
clientID: "YOUR_GOOGLE_CLIENT_ID",
clientSecret: "YOUR_GOOGLE_CLIENT_SECRET",
callbackURL: "/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
return done(null, profile);
}));
Use OAuth when
Session based
JWT based
OAuth
Modern authentication methods like sessions, JWT, and OAuth each have their own strengths. Choosing the right one depends on your application type, scalability needs, and security requirements. By understanding these systems and using them correctly, you can build secure and user friendly web applications.