C
CanxJS
v1.6.2
  • Learn
  • Blog
  • Showcase
C
CanxJS

Ultra-fast async MVC backend framework for Bun. Build production-ready APIs with elegance and speed.

Resources

  • Documentation
  • Learn
  • Blog
  • Showcase

Documentation

  • Introduction
  • Installation
  • Core Concepts
  • CLI Commands
  • API Reference

Legal

  • Privacy Policy
  • Terms of Service

© 2026 CanxJS. All rights reserved.

Built with ❤️ for Candra Kirana

Back to Learning Center
Intermediate
35 min

Authentication & Security

Implement JWT authentication and protect routes.

Authentication & Security

Security is a first-class citizen in CanxJS. Let's implement JWT authentication.

Auth Middleware

First, ensure your auth middleware is set up to verify tokens:

typescript
import { Middleware } from "canxjs";

export const AuthMiddleware: Middleware = async (req, res, next) => {
  const token = req.header("Authorization");
  
  if (!isValid(token)) {
    return res.status(401).json({ error: "Unauthorized" });
  }

  return next();
};

Protecting Routes

Apply the middleware to routes you want to protect:

typescript
app.group("/dashboard", (router) => {
  router.middleware(AuthMiddleware);
  
  router.get("/", (req, res) => res.json({ data: "Secret Data" }));
});

Hashing Passwords

Always hash passwords before storing them. CanxJS provides a Hash facade:

typescript
import { Hash } from "canxjs";

const hashedPassword = await Hash.make("my-secret-password");

if (await Hash.check("input-password", hashedPassword)) {
    // Passwords match!
}

Have questions?

Join the discussion on GitHub