@mostajs/auth 1.0.4 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export declare function hasPermission(userPermissions: string[], requiredPermission: string): boolean;
2
+ export declare function hasAllPermissions(userPermissions: string[], requiredPermissions: string[]): boolean;
3
+ export declare function hasAnyPermission(userPermissions: string[], requiredPermissions: string[]): boolean;
@@ -0,0 +1,16 @@
1
+ // Permission helpers — pure utility functions (zero dependencies)
2
+ // Originates from @mostajs/socle — inlined here to avoid the dependency
3
+ // Author: Dr Hamid MADANI drmdh@msn.com
4
+ export function hasPermission(userPermissions, requiredPermission) {
5
+ if (!userPermissions || userPermissions.length === 0)
6
+ return false;
7
+ if (userPermissions.includes('*'))
8
+ return true;
9
+ return userPermissions.includes(requiredPermission);
10
+ }
11
+ export function hasAllPermissions(userPermissions, requiredPermissions) {
12
+ return requiredPermissions.every(p => hasPermission(userPermissions, p));
13
+ }
14
+ export function hasAnyPermission(userPermissions, requiredPermissions) {
15
+ return requiredPermissions.some(p => hasPermission(userPermissions, p));
16
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { hasPermission, hasAllPermissions, hasAnyPermission } from '@mostajs/socle';
1
+ export { hasPermission, hasAllPermissions, hasAnyPermission } from './helpers/permissions';
2
2
  export { UserSchema, RoleSchema, PermissionSchema, PermissionCategorySchema } from '@mostajs/rbac';
3
3
  export { usePermissions } from './hooks/usePermissions';
4
4
  export { default as PermissionGuard } from './components/PermissionGuard';
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // @mostajs/auth — Client-safe barrel (NO ORM imports)
2
2
  // Author: Dr Hamid MADANI drmdh@msn.com
3
3
  // For server-side code (repos, seed, auth handlers), use '@mostajs/auth/server'
4
- // Permission helpers (from @mostajs/socle — no ORM)
5
- export { hasPermission, hasAllPermissions, hasAnyPermission } from '@mostajs/socle';
4
+ // Permission helpers (pure utilities — no external dependency)
5
+ export { hasPermission, hasAllPermissions, hasAnyPermission } from './helpers/permissions';
6
6
  // Schemas (re-export from rbac — pure data, no ORM)
7
7
  export { UserSchema, RoleSchema, PermissionSchema, PermissionCategorySchema } from '@mostajs/rbac';
8
8
  // Hooks
@@ -1,7 +1,7 @@
1
1
  // @mosta/auth — Server-side auth guards
2
2
  // Author: Dr Hamid MADANI drmdh@msn.com
3
3
  import { NextResponse } from 'next/server';
4
- import { hasPermission } from '@mostajs/socle';
4
+ import { hasPermission } from '../helpers/permissions';
5
5
  import { getPermissionsForRoleFromDB } from '@mostajs/rbac/lib/permissions-server';
6
6
  /**
7
7
  * Create server-side guard functions bound to your auth() instance.
package/dist/lib/auth.js CHANGED
@@ -3,13 +3,8 @@
3
3
  // Phase 3: schemas/repos imported from @mostajs/rbac
4
4
  import NextAuth from 'next-auth';
5
5
  import CredentialsProvider from 'next-auth/providers/credentials';
6
- import { getDialect, registerSchemas } from '@mostajs/orm';
7
- import { UserRepository, RoleRepository } from '@mostajs/rbac/server';
8
- import { UserSchema, RoleSchema, PermissionSchema, PermissionCategorySchema } from '@mostajs/rbac';
6
+ import { getRbacRepos } from '@mostajs/rbac/lib/repos-factory';
9
7
  import { comparePassword } from './password';
10
- // Auto-register RBAC schemas into ORM registry (idempotent)
11
- // Must run before getDialect() to ensure relations are resolvable
12
- registerSchemas([UserSchema, RoleSchema, PermissionSchema, PermissionCategorySchema]);
13
8
  /**
14
9
  * Create NextAuth handlers configured for MostaAuth RBAC.
15
10
  *
@@ -38,8 +33,8 @@ export function createAuthHandlers(rolePermissions, config) {
38
33
  async authorize(credentials) {
39
34
  if (!credentials?.email || !credentials?.password)
40
35
  return null;
41
- const uRepo = new UserRepository(await getDialect());
42
- const user = await uRepo.findByEmail(credentials.email);
36
+ const { users } = await getRbacRepos();
37
+ const user = await users.findByEmail(credentials.email);
43
38
  if (!user)
44
39
  return null;
45
40
  if (user.status !== 'active')
@@ -47,7 +42,7 @@ export function createAuthHandlers(rolePermissions, config) {
47
42
  const valid = await comparePassword(credentials.password, user.password);
48
43
  if (!valid)
49
44
  return null;
50
- await uRepo.updateLastLogin(user.id);
45
+ await users.updateLastLogin(user.id);
51
46
  const { roleNames, permissions } = await resolveUserPermissions(user.id, rolePermissions);
52
47
  return {
53
48
  id: user.id,
@@ -100,9 +95,8 @@ export function createAuthHandlers(rolePermissions, config) {
100
95
  // ─── Internal: resolve user→roles→permissions ─────────────────
101
96
  async function resolveUserPermissions(userId, fallbackMap) {
102
97
  try {
103
- const uRepo = new UserRepository(await getDialect());
104
- const rRepo = new RoleRepository(await getDialect());
105
- const userWithRoles = await uRepo.findByIdWithRoles(userId);
98
+ const { users, roles: rolesRepo } = await getRbacRepos();
99
+ const userWithRoles = await users.findByIdWithRoles(userId);
106
100
  if (userWithRoles?.roles?.length) {
107
101
  const roleNames = [];
108
102
  const permissions = [];
@@ -112,7 +106,7 @@ async function resolveUserPermissions(userId, fallbackMap) {
112
106
  if (roleName)
113
107
  roleNames.push(roleName);
114
108
  if (roleId) {
115
- const roleWithPerms = await rRepo.findByIdWithPermissions(roleId);
109
+ const roleWithPerms = await rolesRepo.findByIdWithPermissions(roleId);
116
110
  if (roleWithPerms?.permissions) {
117
111
  for (const perm of roleWithPerms.permissions) {
118
112
  const permName = typeof perm === 'string' ? perm : perm?.name;
@@ -1,4 +1,9 @@
1
- import type { ModuleRegistration } from '@mostajs/socle';
1
+ /** Minimal registration shape avoids hard dependency on @mostajs/socle */
2
+ interface ModuleRegistrationLike {
3
+ manifest: Record<string, unknown>;
4
+ [key: string]: unknown;
5
+ }
2
6
  export declare function register(registry: {
3
- register(r: ModuleRegistration): void;
7
+ register(r: ModuleRegistrationLike): void;
4
8
  }): void;
9
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mostajs/auth",
3
- "version": "1.0.4",
3
+ "version": "2.0.0",
4
4
  "description": "Authentication — NextAuth, password hashing, session management",
5
5
  "author": "Dr Hamid MADANI <drmdh@msn.com>",
6
6
  "license": "MIT",
@@ -93,19 +93,18 @@
93
93
  "prepublishOnly": "npm run build"
94
94
  },
95
95
  "dependencies": {
96
- "@mostajs/orm": "^1.0.0",
96
+ "@mostajs/net": "^2.0.0",
97
+ "@mostajs/orm": "^1.7.0",
97
98
  "bcryptjs": "^2.4.3"
98
99
  },
99
100
  "peerDependencies": {
100
101
  "@mostajs/rbac": ">=1.0.0",
101
- "@mostajs/socle": ">=2.0.0",
102
102
  "next": ">=14",
103
103
  "next-auth": ">=5.0.0-beta.25",
104
104
  "react": ">=18"
105
105
  },
106
106
  "devDependencies": {
107
- "@mostajs/rbac": "^1.0.4",
108
- "@mostajs/socle": "^2.0.0",
107
+ "@mostajs/rbac": "^2.0.1",
109
108
  "@types/bcryptjs": "^2.4.0",
110
109
  "@types/node": "^25.3.3",
111
110
  "@types/react": "^19.0.0",