DEV Community

Cover image for βœ… *Authentication & Authorization Basics* πŸ”πŸŒ
ssekabira robert sims
ssekabira robert sims

Posted on

βœ… *Authentication & Authorization Basics* πŸ”πŸŒ

πŸ”Ή What is Authentication?

It’s the process of verifying who a user is.

πŸ”Ή What is Authorization?

It’s the process of verifying what a user is allowed to do after logging in.

βœ… Step 1: Authentication – Common Methods

β€’ Username & Password – Basic login

β€’ OAuth – Login via Google, GitHub, etc.

β€’ JWT (JSON Web Token) – Popular for token-based auth

β€’ Session-Based – Stores session on server with session ID

βœ… Step 2: How Login Works (JWT Example)

  1. User sends email & password to server
  2. Server verifies and sends back a JWT
  3. JWT is stored in browser (usually localStorage)
  4. On each request, client sends JWT in headers
  5. Server checks token before giving access

βœ… Step 3: Authorization Types

β€’ Role-Based Access – Admin, Editor, User

β€’ Resource-Based – Only owners can edit their content

β€’ Route Protection – Block some pages unless logged in

βœ… Step 4: Protecting Routes (Frontend Example)

if (!localStorage.getItem('token')) {
  window.location.href = '/login';
}
Enter fullscreen mode Exit fullscreen mode

βœ… Step 5: Backend Route Protection (Express.js)

function authMiddleware(req, res, next) {
  const token = req.headers.authorization;
if (!token) return res.status(401).send('Access Denied');
  // Verify token and decode user info
  next();
}
Enter fullscreen mode Exit fullscreen mode

βœ… Step 6: Common Tools & Libraries

β€’ bcrypt – Hash passwords

β€’ jsonwebtoken (JWT) – Create & verify tokens

β€’ passport.js – Auth middleware

β€’ OAuth Providers – Google, Facebook, GitHub

βœ… Step 7: Best Practices

β€’ Always hash passwords (never store plain text)

β€’ Use HTTPS

β€’ Set token expiry (e.g. 15 mins)

β€’ Refresh tokens securely

β€’ Don't expose sensitive data in JWT

πŸ’¬ and like for more

Top comments (0)