zeesix
v1.0.5
Published
This package handles validation and verification of user roles and permissions
Downloads
104
Readme
Node Package: zeesix
Overview
The zeesix package provides utilities for managing user data, handling token-based authentication, and managing role-based permissions. It integrates with Redis for data storage and offers functions for validating admin data, generating and verifying tokens, and managing role permissions.
Installation
To use this package in your Node.js project, follow these steps:
- Install the package via npm:
npm install zeesix
Function Documentation
Initialization
Before using the functions for token management or role/permission management, you must initialize the package with your signing key and Redis URL.
const { initializeConnection } = require('zeesix');
const signingKey = 'your-signing-key';
const redisUrl = 'redis-url';
await initializeConnection(redisUrl, signingKey);
Validation Functions
validateAdmin(adminData: AdminData)
Validates the admin user data against the defined schema.
Parameters:
- adminData (AdminData): An object containing the admin user data.
Returns:
- ValidationResult: The result of the validation, including any errors or success.
Example
const adminData = { id: '1', fullname: 'Alpha James', email: '[email protected]', phone: '0708633536', role: '999', status: true };
const result = validateAdmin(adminData);
if (result.error) {
console.error('Validation failed:', result.error.details);
} else {
console.log('Validation successful');
}
Token Management
signToken(authData: AdminData)
Generates a token for the given admin data.
Parameters:
- authData (AdminData): An object containing the admin data to include in the token.
Returns:
- TokenResult: The generated token or any error encountered during the process.
Example:
const tokenResult = signToken(adminData);
console.log('Generated Token:', tokenResult.value);
verifyToken(token: string)
Verifies the provided token and returns the result.
Parameters:
- token (string): The token to verify.
Returns:
- VerifiedTokenResult: The result of the token verification, including any errors or decoded data. Example:
const result = await verifyToken(token);
console.log('Token verification result:', result);
Role and Permission Management
createRolePermissions({ role, permissions }: RolePermissionsData)
Creates or updates role permissions in the system.
Parameters:
- rolePermissions (RolePermissionsData): An object containing the role and its associated permissions.
Returns:
- Promise: Resolves when the role permissions are successfully created or updated. Example:
await createRolePermissions({ role: '99', permissions: ['admin.plan.delete', 'admin.plan.create', 'admin.plan.read'] });
console.log('Role permissions created.');
readRolePermissions(role: string)
Retrieves permissions associated with a specific role.
Parameters:
- role (string): The role whose permissions are to be retrieved.
Returns:
- RolePermissionsData: The retrieved permissions for the specified role. Example:
const permissions = await readRolePermissions('99');
console.log('Role permissions:', permissions);
Authorization
isAuthorized(token: string, permission: string)
Checks if the provided token grants the specified permission.
Parameters:
- token (string): The token to check. permission (string): The permission to validate against the token.
Returns:
- VerifiedTokenResult: The result of the authorization check, including any errors or authorized status. Example:
const result = await isAuthorized(token, 'admin.plan.read');
console.log('Authorization result:', result);
License
This package is licensed under the MIT License.
Contributing
Contributions are welcome! Please fork the repository and submit a pull request.