@mostajs/auth 1.0.4 → 2.0.2
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.
- package/dist/helpers/permissions.d.ts +3 -0
- package/dist/helpers/permissions.js +16 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/lib/auth-check.js +1 -1
- package/dist/lib/auth.js +7 -13
- package/dist/register.d.ts +7 -2
- package/dist/server.d.ts +3 -0
- package/dist/server.js +4 -0
- package/package.json +4 -5
|
@@ -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 '
|
|
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 (
|
|
5
|
-
export { hasPermission, hasAllPermissions, hasAnyPermission } from '
|
|
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
|
package/dist/lib/auth-check.js
CHANGED
|
@@ -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 '
|
|
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 {
|
|
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
|
|
42
|
-
const user = await
|
|
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
|
|
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
|
|
104
|
-
const
|
|
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
|
|
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;
|
package/dist/register.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
|
|
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:
|
|
7
|
+
register(r: ModuleRegistrationLike): void;
|
|
4
8
|
}): void;
|
|
9
|
+
export {};
|
package/dist/server.d.ts
CHANGED
|
@@ -6,4 +6,7 @@ export { hashPassword, comparePassword } from './lib/password';
|
|
|
6
6
|
export { getPermissionsForRoleFromDB } from '@mostajs/rbac/lib/permissions-server';
|
|
7
7
|
export { seedRBAC } from '@mostajs/rbac/lib/rbac-seed';
|
|
8
8
|
export type { SeedRBACOptions } from '@mostajs/rbac/lib/rbac-seed';
|
|
9
|
+
export { createAdmin } from '@mostajs/rbac/lib/create-admin';
|
|
10
|
+
export type { CreateAdminOptions, CreateAdminResult } from '@mostajs/rbac/lib/create-admin';
|
|
11
|
+
export { getSchemas, moduleInfo } from '@mostajs/rbac/lib/module-info';
|
|
9
12
|
export { UserRepository, RoleRepository, PermissionRepository, PermissionCategoryRepository } from '@mostajs/rbac/server';
|
package/dist/server.js
CHANGED
|
@@ -11,5 +11,9 @@ export { hashPassword, comparePassword } from './lib/password';
|
|
|
11
11
|
export { getPermissionsForRoleFromDB } from '@mostajs/rbac/lib/permissions-server';
|
|
12
12
|
// RBAC seed (re-export from rbac/server)
|
|
13
13
|
export { seedRBAC } from '@mostajs/rbac/lib/rbac-seed';
|
|
14
|
+
// Create admin (re-export from rbac — auth delegates to rbac for user creation)
|
|
15
|
+
export { createAdmin } from '@mostajs/rbac/lib/create-admin';
|
|
16
|
+
// Module schemas (re-export from rbac — auth has no own schemas)
|
|
17
|
+
export { getSchemas, moduleInfo } from '@mostajs/rbac/lib/module-info';
|
|
14
18
|
// Repositories (re-export from rbac/server)
|
|
15
19
|
export { UserRepository, RoleRepository, PermissionRepository, PermissionCategoryRepository } from '@mostajs/rbac/server';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mostajs/auth",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.2",
|
|
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/
|
|
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": "^
|
|
108
|
-
"@mostajs/socle": "^2.0.0",
|
|
107
|
+
"@mostajs/rbac": "^2.0.3",
|
|
109
108
|
"@types/bcryptjs": "^2.4.0",
|
|
110
109
|
"@types/node": "^25.3.3",
|
|
111
110
|
"@types/react": "^19.0.0",
|