Designing Secure Authentication in Node.js: JWT, SSO and Firebase

/ Security / 7 min read

Illustration of a shield with a padlock and a three-part JWT token bar

How to think about authentication versus authorization, choose between tokens and sessions, and avoid the mistakes that show up again and again in real Node.js backends.

Authentication is the part of a backend where small mistakes become headlines. I have implemented JWT-based login, single sign-on and Firebase authentication in production applications, and I also maintain an open-source Node.js authentication and authorization project. These are the principles I keep applying.

Two different questions

Authentication asks who you are. Authorization asks what you are allowed to do. Mixing them up produces bugs like an endpoint that checks you are logged in but never checks that the record belongs to you. Treat them as separate layers: middleware that establishes identity, and explicit permission checks close to the data.

JWT: what it is good at

A JSON Web Token is a signed claim that the server can verify without a database lookup. That makes it convenient for stateless APIs and for passing identity between services. The trade-off is that you cannot easily revoke one before it expires.

const token = jwt.sign(
  { sub: user.id, role: user.role },
  process.env.JWT_SECRET,
  { expiresIn: '15m', issuer: 'my-api' }
);

Short access tokens, rotating refresh tokens

Keep access tokens short-lived, around fifteen minutes. Pair them with a longer refresh token stored in an httpOnly, secure, sameSite cookie, and rotate it on every use. If an old refresh token is ever replayed, treat that as theft and invalidate the whole family. Keep the refresh token hash server-side so that logout and password change really do end sessions.

Roles and permissions

For multi-level authorization, avoid scattering role string comparisons through your handlers. Define permissions in one place and check against them, so adding a role is a data change rather than a hunt through the codebase.

const can = (user, action, resource) =>
  policies[user.role]?.[action]?.(user, resource) ?? false;

app.delete('/orders/:id', requireAuth, async (req, res) => {
  const order = await Order.findById(req.params.id);
  if (!can(req.user, 'delete', order)) return res.sendStatus(403);
  /* ... */
});

SSO and Firebase: do not reinvent

When the requirement is sign-in with an existing company or Google account, use the standard protocol rather than building your own. SSO via OpenID Connect or SAML, and managed providers such as Firebase Authentication, handle password storage, breached-credential checks and multi-factor flows far better than a hand-rolled version. Your job becomes verifying the provider's token on the server and mapping it to your own user record and roles.

The checklist I run through

Hash passwords with bcrypt or argon2, never a fast hash. Rate-limit login and password reset endpoints. Never put secrets in a JWT payload, because it is only encoded, not encrypted. Verify the algorithm explicitly when decoding. Return the same message for unknown email and wrong password. Log authentication failures so that you can see an attack happening.

Security is mostly a long list of unglamorous details. Writing them down and applying them consistently is what makes a system trustworthy.

{ Topics }
  • Node.js authentication
  • JWT authentication
  • Node.js authorization
  • JWT refresh token
  • SSO implementation
  • Firebase authentication
  • role based access control
  • secure login Node.js
  • Express.js security
  • Node.js developer Kochi
  • Arun Muraleedharan

Each Project I Undertake

is a Unique Opportunity.

Ready to take the next step? Join me now and start transforming your vision into reality with my support.