Skip to content

๐Ÿš€ Migration Guide: v2.x to v3.0.0 โ€‹

This guide helps you migrate from Fire Shield v2.x to v3.0.0.

โœ… What's New in v3.0.0 โ€‹

New Features โ€‹

  • ๐Ÿงช Plugin System - Extensible architecture for custom logic
  • ๐Ÿ“ฆ Platform Independence - Removed fs module dependency

Breaking Changes โ€‹

Featurev2.xv3.0.0Action Required
RBAC.fromFile()AvailableโŒ REMOVEDUse loader packages
RBAC.fromFileSync()AvailableโŒ REMOVEDUse loader packages
RBAC.fromJSONConfig()Availableโœ… Still worksNo action
RBAC.validateConfig()Availableโœ… Still worksNo action
Plugin SystemNot availableโœ… NEWOptional - see plugin guide

๐Ÿ“ฆ Migration Paths โ€‹

1. Node.js Backend โ€‹

Before (v2.x) โ€‹

typescript
import { RBAC } from '@fire-shield/core';

// Load from file (NOT AVAILABLE IN v3.0.0)
const rbac = await RBAC.fromFile('./rbac.config.json');

After (v3.0.0) - Use fromJSONConfig โ€‹

typescript
import { RBAC } from '@fire-shield/core';
import { readFileSync } from 'fs';

// Load from file manually
const json = JSON.parse(readFileSync('./rbac.config.json', 'utf-8'));
const config = JSON.parse(json);

// Create RBAC from JSON config
const rbac = new RBAC({
  preset: config
});

// Or use static method
const rbac2 = RBAC.fromJSONConfig(JSON.stringify(config));

// Everything works to same!
rbac.hasPermission(user, 'posts:write');

2. Browser / SSR / Edge โ€‹

Before (v2.x) โ€‹

typescript
import { RBAC } from '@fire-shield/core';

// In browser, we couldn't use fromFile anyway
// We used fromJSONConfig or manual setup
const json = require('./rbac.config.json');
const rbac = new RBAC({ preset: json });

After (v3.0.0) - No Changes Needed! โ€‹

typescript
import { RBAC } from '@fire-shield/core';

// Browser/SSR/Edge - No changes needed!
const json = require('./rbac.config.json');
const rbac = RBAC.fromJSONConfig(json);

// Works exactly to same
rbac.hasPermission(user, 'posts:read');

3. React / Vue / Angular / Other Frameworks โ€‹

Framework Adapters โ€‹

All framework adapters (React, Vue, Next.js, Nuxt, Angular, Svelte, Express, Fastify, Hono, etc.) are NOT AFFECTED by the v3.0.0 breaking changes.

They use the core RBAC class internally, and the only breaking change was the removal of fromFile() and fromFileSync(). Since framework adapters don't use these methods (they typically use fromJSONConfig() or manual setup), you don't need to change any adapter code.

typescript
// React - NO CHANGES NEEDED
import { RBAC } from '@fire-shield/core';
import { useRBAC } from '@fire-shield/react';

const rbac = new RBAC(); // Works same as before
const { can } = useRBAC(rbac);
vue
<!-- Vue - NO CHANGES NEEDED -->
<script setup>
import { RBAC } from '@fire-shield/core';
import { can } from '@fire-shield/vue';

const rbac = new RBAC(); // Works same as before
</script>
typescript
// Express - NO CHANGES NEEDED
import { createExpressRBAC } from '@fire-shield/express';
import { RBAC } from '@fire-shield/core';

const rbac = new RBAC(); // Works same as before
const rbacMiddleware = createExpressRBAC(rbac, {
  getUser: (req) => req.user
});

๐Ÿ”ง Step-by-Step Migration โ€‹

Step 1: Identify Your Usage โ€‹

Check if you use:

  1. RBAC.fromFile() or RBAC.fromFileSync() - โŒ NEEDS MIGRATION
  2. RBAC.fromJSONConfig() - โœ… NO MIGRATION NEEDED
  3. Manual setup with new RBAC({ config: ... }) - โœ… NO MIGRATION NEEDED

Step 2: Choose Migration Path โ€‹

Best for: All platforms (Node.js, Browser, Edge, SSR)

Code:

typescript
import { RBAC } from '@fire-shield/core';
import { readFileSync } from 'fs'; // Node.js only

// Node.js
import { readFileSync } from 'fs';
const json = JSON.parse(readFileSync('./rbac.config.json', 'utf-8'));
const rbac = RBAC.fromJSONConfig(JSON.stringify(json));

// Browser/Edge
const json = require('./rbac.config.json');
const rbac = RBAC.fromJSONConfig(json);

Pros:

  • โœ… Works on all platforms
  • โœ… No external dependencies
  • โœ… No package installation needed
  • โœ… Future-proof - won't break in v4.0.0

Cons:

  • โš ๏ธ Manual file reading in Node.js
  • โš ๏ธ Need to parse JSON yourself

Best for: Node.js backends only

Code: (Coming in a future release)

typescript
import { NodeLoader } from '@fire-shield/node-loader';

// Load from file
const rbac = await NodeLoader.load('./rbac.config.json');

// Load from directory
const rbac = await NodeLoader.loadFromDirectory('./configs');

// Load with validation
const rbac = await NodeLoader.load('./rbac.config.json', {
  validate: true
});

Pros:

  • โœ… Clean API
  • โœ… Built-in validation
  • โœ… Error handling
  • โœ… Supports multiple formats

Cons:

  • โš ๏ธ Requires package installation
  • โš ๏ธ Node.js only

Step 3: Update Your Code โ€‹

Example 1: Express Server (Node.js) โ€‹

Before (v2.x):

typescript
import express from 'express';
import { RBAC } from '@fire-shield/core';

const rbac = await RBAC.fromFile('./rbac.config.json');

const app = express();
app.use('/admin',
  rbacMiddleware.requirePermission('admin:access')
);

After (v3.0.0):

typescript
import express from 'express';
import { readFileSync } from 'fs';
import { RBAC } from '@fire-shield/core';
import { createExpressRBAC } from '@fire-shield/express';

// Load config manually
const json = JSON.parse(readFileSync('./rbac.config.json', 'utf-8'));
const rbac = new RBAC({ preset: json });

// Or use fromJSONConfig
const rbac2 = RBAC.fromJSONConfig(JSON.stringify(json));

const app = express();
app.use('/admin',
  rbacMiddleware.requirePermission('admin:access')
);

Example 2: Next.js (SSR) โ€‹

Before (v2.x):

typescript
import { RBAC } from '@fire-shield/core';

// In server-side code
const rbac = await RBAC.fromFile('./rbac.config.json');

export default function RootLayout({ children }) {
  return (
    <RBACProvider rbac={rbac}>
      {children}
    </RBACProvider>
  );
}

After (v3.0.0) - No Changes Needed!

typescript
import { RBAC } from '@fire-shield/core';

// In server-side code - fromFile was never used in SSR anyway
const json = require('./rbac.config.json');
const rbac = new RBAC({ preset: json });

export default function RootLayout({ children }) {
  return (
    <RBACProvider rbac={rbac}>
      {children}
    </RBACProvider>
  );
}

Example 3: React App (Browser) โ€‹

Before (v2.x):

typescript
import { RBAC } from '@fire-shield/core';

// In browser, fromFile doesn't work
const json = require('./rbac.config.json');
const rbac = new RBAC({ preset: json });

After (v3.0.0) - Same!

typescript
import { RBAC } from '@fire-shield/core';

// In browser, nothing changes!
const json = require('./rbac.config.json');
const rbac = RBAC.fromJSONConfig(json);

๐Ÿงช Plugin System Migration (Optional) โ€‹

What is the Plugin System? โ€‹

The plugin system in v3.0.0 allows you to extend RBAC functionality with custom logic. You can create plugins for:

  • Database audit logging
  • Analytics tracking
  • Rate limiting
  • External validation
  • And much more!

Do You Need to Migrate? โ€‹

NO! The plugin system is OPTIONAL. You don't need to use it if you don't want to.

Your existing code will work exactly the same without plugins.

How to Use Plugins (Optional) โ€‹

If you want to use the new plugin system:

typescript
import { RBAC, RBACPlugin } from '@fire-shield/core';

// Create a custom plugin
class AuditDatabasePlugin implements RBACPlugin {
  name = 'database-audit';
  
  constructor(private db: any) {}

  async onPermissionCheck(event) {
    // Log to database
    await this.db.insert({
      userId: event.userId,
      permission: event.permission,
      allowed: event.allowed,
      timestamp: event.timestamp
    });
  }
}

// Create RBAC instance
const rbac = new RBAC();

// Register plugin
await rbac.registerPlugin(new AuditDatabasePlugin(database));

// Now all permission checks are logged to database!
rbac.hasPermission(user, 'posts:write');

Learn More: Plugin System Guide

โœ… Verification Checklist โ€‹

After migrating, verify:

  • [ ] All tests pass
  • [ ] Permission checks work as expected
  • [ ] No TypeScript errors
  • [ ] No runtime errors
  • [ ] Config loads correctly
  • [ ] Role hierarchy works
  • [ ] Wildcard permissions work
  • [ ] Deny permissions work
  • [ ] Audit logging works (if used)
  • [ ] Framework adapters work (if used)

๐Ÿ› Common Issues and Solutions โ€‹

Issue 1: "RBAC.fromFile is not a function" โ€‹

Cause: You're trying to use RBAC.fromFile() which was removed in v3.0.0.

Solution: Use RBAC.fromJSONConfig() instead:

typescript
// โŒ DON'T
const rbac = await RBAC.fromFile('./config.json');

// โœ… DO
const json = require('./config.json');
const rbac = RBAC.fromJSONConfig(json);

Issue 2: "fs module not found" (in Browser) โ€‹

Cause: You're importing fs in browser code.

Solution: Don't use fs in browser code. Use require() or fetch():

typescript
// โŒ DON'T (Browser)
import { readFileSync } from 'fs';

// โœ… DO (Browser)
const json = require('./config.json');

Issue 3: "Cannot find module '@fire-shield/node-loader'" โ€‹

Cause: Loader packages are not yet available.

Solution: Use RBAC.fromJSONConfig() for now (Option A), or wait for loader packages in a future release.

๐Ÿ“š Additional Resources โ€‹

๐Ÿ’ก Tips โ€‹

1. Use Presets for Cleaner Code โ€‹

typescript
// Before
const rbac = new RBAC();
rbac.createRole('admin', ['*']);
rbac.createRole('editor', ['posts:read', 'posts:write']);
// ... more setup

// After - Use preset
const preset = {
  name: 'my-app',
  permissions: [
    { name: 'admin', bit: 1 },
    { name: 'posts:read', bit: 2 },
    { name: 'posts:write', bit: 4 }
  ],
  roles: [
    {
      name: 'admin',
      permissions: ['admin', 'posts:read', 'posts:write'],
      level: 10
    },
    {
      name: 'editor',
      permissions: ['posts:read', 'posts:write'],
      level: 5
    }
  ]
};

const rbac = new RBAC({ preset });

2. Use Environment Variables for Config Paths โ€‹

typescript
// Node.js
const configPath = process.env.RBAC_CONFIG_PATH || './rbac.config.json';
const json = readFileSync(configPath, 'utf-8');
const rbac = RBAC.fromJSONConfig(json);

// Browser
const configUrl = process.env.RBAC_CONFIG_URL || '/rbac.config.json';
const response = await fetch(configUrl);
const json = await response.json();
const rbac = RBAC.fromJSONConfig(json);

3. Wrap Loading in Try-Catch โ€‹

typescript
let rbac: RBAC;

try {
  const json = require('./rbac.config.json');
  rbac = RBAC.fromJSONConfig(json);
} catch (error) {
  console.error('Failed to load RBAC config:', error);
  // Use fallback config
  rbac = new RBAC({
    preset: fallbackConfig
  });
}

๐ŸŽฏ Quick Reference โ€‹

Featurev2.xv3.0.0Migration
RBAC.fromFile()โœ…โŒ RemovedUse fromJSONConfig()
RBAC.fromFileSync()โœ…โŒ RemovedUse fromJSONConfig()
RBAC.fromJSONConfig()โœ…โœ…โœ… No change
RBAC.validateConfig()โœ…โœ…โœ… No change
Plugin SystemโŒโœ…Optional
Core functionalityโœ…โœ…โœ… No change
Framework adaptersโœ…โœ…โœ… No change

โœ… You're Ready! โ€‹

After following this migration guide:

  • โœ… Your code works with v3.0.0
  • โœ… You have access to the new plugin system (optional)
  • โœ… Your app is platform-independent (works in Node.js, Browser, Edge)
  • โœ… You're ready for future v3.x releases

Need Help?


Happy migrating! ๐Ÿš€