npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@dax-crafta/auth

v2.1.6

Published

A powerful, flexible, and secure authentication plugin for the Crafta framework. Supports JWT, social login, 2FA, RBAC, audit logging, and enterprise-grade security features.

Readme

@dax-crafta/auth

Production-ready auth for Crafta apps with plug-and-play defaults and clean JSON customization.

npm version License Downloads

Why this package

  • You should not rebuild auth from scratch for every project.
  • This package gives you login, refresh tokens, password reset, RBAC, 2FA, audit logs, and feature toggles out of the box.
  • Keep defaults for speed, customize only where needed.

What you get

  • JWT auth with refresh token rotation
  • Register, login, verify, forgot/reset password routes
  • Optional 2FA (TOTP + backup codes)
  • RBAC with role and permission service
  • Route-level rate limiting
  • Password policy and password history checks
  • Optional Google OAuth
  • Audit logs (Mongo + file)

1. Install

npm install crafta @dax-crafta/auth

2. Minimal setup (copy-paste)

Create server.js:

const { crafta } = require('crafta');
const { auth } = require('@dax-crafta/auth');

const app = crafta();

auth({
  env: {
    JWT_SECRET: process.env.JWT_SECRET || 'dev-secret-123'
  },
  mongoUrl: process.env.MONGO_URL || 'mongodb://127.0.0.1:27017/crafta-auth-demo',

  // Local dev ke liye simple mode
  features: {
    emailVerification: false,
    loginAlerts: false,
    csrf: false
  }
})(app);

app.get('/health', (req, res) => {
  res.json({ ok: true, message: 'Auth is running' });
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

Run it:

node server.js

3. First Postman test flow

Health

  • GET /health

Register

  • POST /register
{
  "name": "Test User",
  "email": "[email protected]",
  "password": "Strong@1234"
}

Login

  • POST /login
{
  "email": "[email protected]",
  "password": "Strong@1234"
}

Use returned accessToken on protected routes:

  • Authorization: Bearer <token>

Profile (protected)

  • PUT /profile
{
  "name": "Updated Name"
}

Refresh token

  • POST /refresh-token
{
  "refreshToken": "<token-from-login>"
}

4. Customize with JSON (main power)

Everything is controlled from one config object.

auth({
  strategy: 'jwt',
  fields: ['name', 'email', 'password', 'age'],

  routes: {
    register: '/api/auth/register',
    login: '/api/auth/login',
    verify: '/api/auth/verify',
    forgotPassword: '/api/auth/forgot-password',
    resetPassword: '/api/auth/reset-password',
    refreshToken: '/api/auth/refresh-token',
    profile: '/api/auth/profile',
    twoFactor: '/api/auth/2fa'
  },

  passwordPolicy: {
    minLength: 10,
    requireUppercase: true,
    requireLowercase: true,
    requireNumbers: true,
    requireSpecialChars: true,
    expiryDays: 90,
    minStrength: 3
  },

  limits: {
    loginMax: 10,
    twoFAMax: 20,
    forgotPasswordMax: 5,
    refreshMax: 30
  }
})(app);

5. Feature toggles (fast on/off)

Set any feature to false and it turns off.

auth({
  features: {
    emailVerification: false,
    loginAlerts: false,
    securityAttempts: true,
    rateLimit: true,
    auditLogs: true,
    twoFactor: true,
    csrf: false
  }
})(app);

Notes:

  • If SMTP is missing, emailVerification/loginAlerts are auto-disabled with warning.
  • If securityAttempts: false, lockout attempt handling is disabled.

6. Email setup (optional)

Enable this only when SMTP is ready:

auth({
  features: {
    emailVerification: true,
    loginAlerts: true
  },
  smtp: {
    host: process.env.SMTP_HOST,
    port: Number(process.env.SMTP_PORT || 587),
    auth: {
      user: process.env.SMTP_USER,
      pass: process.env.SMTP_PASS
    },
    from: process.env.SMTP_FROM
  },
  baseUrl: process.env.BASE_URL || 'http://localhost:3000'
})(app);

7. Google OAuth setup (optional)

auth({
  social: {
    google: {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: 'http://localhost:3000/auth/google/callback'
    }
  }
})(app);

Routes:

  • GET /auth/google
  • GET /auth/google/callback

8. RBAC quick use

const role = await app.roleService.createRole({
  name: 'admin',
  permissions: [
    { resource: 'users', actions: ['create', 'read', 'update', 'delete'] }
  ]
});

const allowed = await app.roleService.checkPermission('admin', 'users', 'delete');

9. Environment variables

JWT_SECRET=my-super-secret
MONGO_URL=mongodb://127.0.0.1:27017/crafta-auth-demo
SMTP_HOST=smtp.example.com
SMTP_PORT=587
[email protected]
SMTP_PASS=your-password
[email protected]
BASE_URL=http://localhost:3000
GOOGLE_CLIENT_ID=...
GOOGLE_CLIENT_SECRET=...

10. Troubleshooting

  • 401 on protected route

    • Token missing or expired.
    • Send Authorization: Bearer <accessToken>.
  • Register fails on password policy

    • Use uppercase + lowercase + number + special char.
  • Email not sending

    • Check SMTP credentials and port.
    • If SMTP absent, email features auto-disable by design.
  • Refresh token invalid

    • Refresh token is rotated; use latest token returned by API.

11. Release confidence

  • Built-in smoke suite is available:
npm test

License

MIT © Dax Crafta