mbkauthe 1.0.14 → 1.0.15
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/README.md +110 -13
- package/docs/db.md +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,19 +4,26 @@
|
|
|
4
4
|
|
|
5
5
|
## Table of Contents
|
|
6
6
|
|
|
7
|
-
- [
|
|
8
|
-
- [
|
|
9
|
-
- [
|
|
10
|
-
- [
|
|
11
|
-
- [
|
|
12
|
-
|
|
13
|
-
- [
|
|
14
|
-
- [
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
- [
|
|
18
|
-
- [
|
|
19
|
-
- [
|
|
7
|
+
- [mbkauthe](#mbkauthe)
|
|
8
|
+
- [Table of Contents](#table-of-contents)
|
|
9
|
+
- [Features](#features)
|
|
10
|
+
- [Installation](#installation)
|
|
11
|
+
- [Usage](#usage)
|
|
12
|
+
- [Implementation in a Project](#implementation-in-a-project)
|
|
13
|
+
- [Basic Setup](#basic-setup)
|
|
14
|
+
- [Middleware Function Documentation](#middleware-function-documentation)
|
|
15
|
+
- [validateSession(session)](#validatesessionsession)
|
|
16
|
+
- [checkRolePermission(userRole, requiredRoles)](#checkrolepermissionuserrole-requiredroles)
|
|
17
|
+
- [validateSessionAndRole(session, userRole, requiredRoles)](#validatesessionandrolesession-userrole-requiredroles)
|
|
18
|
+
- [getUserData(session)](#getuserdatasession)
|
|
19
|
+
- [authenticate(session)](#authenticatesession)
|
|
20
|
+
- [API Endpoints](#api-endpoints)
|
|
21
|
+
- [Login](#login)
|
|
22
|
+
- [Logout](#logout)
|
|
23
|
+
- [Terminate All Sessions](#terminate-all-sessions)
|
|
24
|
+
- [Database Structure](#database-structure)
|
|
25
|
+
- [License](#license)
|
|
26
|
+
- [Contact \& Support](#contact--support)
|
|
20
27
|
|
|
21
28
|
`mbkAuthe` is a reusable authentication system for Node.js applications, designed to simplify session management, user authentication, and role-based access control. It integrates seamlessly with PostgreSQL and supports features like Two-Factor Authentication (2FA), session restoration, and reCAPTCHA verification.
|
|
22
29
|
|
|
@@ -82,6 +89,96 @@ mbkautheVar='{
|
|
|
82
89
|
}'
|
|
83
90
|
```
|
|
84
91
|
|
|
92
|
+
## Middleware Function Documentation
|
|
93
|
+
|
|
94
|
+
### `validateSession(session)`
|
|
95
|
+
Validates the user's session to ensure it is active and not expired.
|
|
96
|
+
|
|
97
|
+
- **Parameters:**
|
|
98
|
+
- `session` (Object): The session object to validate.
|
|
99
|
+
|
|
100
|
+
- **Returns:**
|
|
101
|
+
- `boolean`: Returns `true` if the session is valid, otherwise `false`.
|
|
102
|
+
|
|
103
|
+
Usage
|
|
104
|
+
```
|
|
105
|
+
// Require vaild session or to be login to access this page
|
|
106
|
+
router.get(["/home"], validateSession, (req, res) => {
|
|
107
|
+
// Restricted Code
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
### `checkRolePermission(userRole, requiredRoles)`
|
|
114
|
+
Checks if the user has the required role permissions.
|
|
115
|
+
|
|
116
|
+
- **Parameters:**
|
|
117
|
+
- `userRole` (string): The role of the user.
|
|
118
|
+
- `requiredRoles`(optional) (string[]): An array of roles that are allowed access.
|
|
119
|
+
|
|
120
|
+
- **Returns:**
|
|
121
|
+
- `boolean`: Returns `true` if the user has the required permissions, otherwise `false`.
|
|
122
|
+
|
|
123
|
+
Usage
|
|
124
|
+
```
|
|
125
|
+
// Require vaild session or to be login to access this page
|
|
126
|
+
router.get(["/admin"], validateSession, checkRolePermission("SuperAdmin"), (req, res) => {
|
|
127
|
+
// Restricted Code
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
### `validateSessionAndRole(session, userRole, requiredRoles)`
|
|
133
|
+
Validates both the session and the user's role permissions.
|
|
134
|
+
|
|
135
|
+
- **Parameters:**
|
|
136
|
+
- `session` (Object): The session object to validate.
|
|
137
|
+
- `userRole` (string): The role of the user.
|
|
138
|
+
- `requiredRoles` (optional) (string[]): An array of roles that are allowed access.
|
|
139
|
+
|
|
140
|
+
- **Returns:**
|
|
141
|
+
- `boolean`: Returns `true` if both the session and role permissions are valid, otherwise `false`.
|
|
142
|
+
|
|
143
|
+
Usage
|
|
144
|
+
```
|
|
145
|
+
// Require vaild session or to be login to access this page
|
|
146
|
+
router.get(["/admin"], validateSessionAndRole("SuperAdmin"), (req, res) => {
|
|
147
|
+
// Restricted Code
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
### `getUserData(session)`
|
|
153
|
+
Retrieves user data based on the session.
|
|
154
|
+
|
|
155
|
+
- **Parameters:**
|
|
156
|
+
- `session` (Object): The session object containing user information.
|
|
157
|
+
|
|
158
|
+
- **Returns:**
|
|
159
|
+
- `Object|null`: Returns the user data object if found, otherwise `null`.
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
### `authenticate(session)`
|
|
164
|
+
Authenticates the user by validating the session and retrieving user data.
|
|
165
|
+
|
|
166
|
+
- **Parameters:**
|
|
167
|
+
- `session` (Object): The session object to authenticate.
|
|
168
|
+
|
|
169
|
+
- **Returns:**
|
|
170
|
+
- `Object|null`: Returns the authenticated user data if successful, otherwise `null`.
|
|
171
|
+
|
|
172
|
+
Usage
|
|
173
|
+
```
|
|
174
|
+
// Require vaild session or to be login to access this page
|
|
175
|
+
router.post(["/terminateAllSessions"], authenticate(mbkautheVar.Password), (req, res) => {
|
|
176
|
+
// Restricted Code
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
|
|
85
182
|
## API Endpoints
|
|
86
183
|
|
|
87
184
|
### Login
|
package/docs/db.md
CHANGED
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"HaveMailAccount" BOOLEAN NOT NULL DEFAULT false,
|
|
36
36
|
"SessionId" TEXT,
|
|
37
37
|
"GuestRole" JSONB DEFAULT '{"allowPages": [""], "NotallowPages": [""]}'::jsonb
|
|
38
|
-
"AllowedApps" JSONB DEFAULT '
|
|
38
|
+
"AllowedApps" JSONB DEFAULT '["mbkauthe"]'::jsonb
|
|
39
39
|
);
|
|
40
40
|
```
|
|
41
41
|
|