Skip to content

Commit 6e6ed06

Browse files
committed
Add req.session dependency
1 parent 3d3ba03 commit 6e6ed06

3 files changed

Lines changed: 107 additions & 6 deletions

File tree

README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,40 @@ Easily plan, manage, and track client commissions.
44

55
![Banner](/frontend/public/banner.png)
66

7+
## Requirements
8+
9+
- Must be using `express` on Node.js
10+
- Must be using `express-session` for session management
11+
- Must be using a valid session store (e.g., `express-mysql-session` for MySQL)
12+
713
## Usage
814

915
Include the `commtrackr` package in your Express.js application:
1016

1117
```javascript
1218
const express = require('express');
13-
const commtrackr = require('commtrackr'); // Import the commtrackr package
19+
20+
const session = require("express-session");
21+
const MySQLStore = require('express-mysql-session')(session); // Use a MySQL session store
22+
const sessionStore = new MySQLStore({
23+
host: '',
24+
port: 3306,
25+
user: '',
26+
password: '',
27+
database: ''
28+
});
29+
app.use(session({
30+
name: '',
31+
key: '',
32+
secret: '',
33+
store: sessionStore,
34+
resave: false,
35+
saveUninitialized: false
36+
}));
37+
1438
const app = express();
39+
40+
const commtrackr = require('commtrackr'); // Import the commtrackr package
1541
app.use('/commtrackr', commtrackr.routes); // Mount routes to /commtrackr path
1642
commtrackr.init({ // Initialize CommTracker with configurations
1743
tenant: {
@@ -29,8 +55,69 @@ commtrackr.init({ // Initialize CommTracker with configurations
2955
url: '', // URL to redirect to for authentication
3056
},
3157
},
58+
vars: {
59+
userId: 'username', // req.session object variable for unique user identification
60+
userName: 'name', // req.session object variable for user name
61+
role: 'role', // req.session object variable for user role
62+
commissions: 'commissions', // req.session object variable for user commissions array
63+
},
3264
});
65+
3366
app.listen(3000, () => {
3467
console.log('Server is running on http://localhost:3000');
3568
});
3669
```
70+
71+
## Session Variables Setup
72+
73+
These variables must be set in your `req.session` object to enable CommTrackr functionality:
74+
75+
### userId
76+
77+
`req.session.userId` must contain a unique identifier for the user, such as a username or user ID. This is used to track user-specific data. This must be present for CommTrackr to detect a logged-in user.
78+
79+
Type: `String`
80+
81+
Fallback: None
82+
83+
Example: `'username'`
84+
85+
### userName
86+
87+
`req.session.userName` should contain the name of the user. This is used for display purposes in the CommTrackr interface. Fallback to `userId` if not set.
88+
89+
Type: `String`
90+
91+
Fallback: `req.session.userId`
92+
93+
Example: `'John Doe'`
94+
95+
### role
96+
97+
`req.session.role` should contain the role of the user: 'admin', `dev`, or 'user'. This is used to control access to certain features and functionalities within CommTrackr.
98+
99+
Type: `String`
100+
101+
Fallback: `user`
102+
103+
Example: `'user'`, `'dev'`, `'admin'`
104+
105+
### commissions
106+
107+
`req.session.commissions` should be an array of commission objects associated with the user. Each commission object should have the following structure:
108+
109+
Type: `Array`
110+
111+
Example:
112+
113+
```javascript
114+
[
115+
{
116+
id: 'unique-commission-id', // Unique identifier for the commission
117+
client: 'Client Name', // Name of the client
118+
amount: 1000, // Commission amount
119+
date: '2023-10-01', // Date of the commission
120+
status: 'pending' // Status of the commission (e.g., 'pending', 'paid')
121+
}
122+
]
123+
```

frontend/public/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ main {
113113
height: 100%;
114114
position: relative;
115115
transition-timing-function: linear;
116+
opacity: 0;
116117

117118
> img {
118119
width: 50px;

index.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ app.use(rateLimit({
2323

2424
let on = false;
2525
let tenant = {};
26+
let vars = {};
2627
let returnHandler = null;
27-
let user = {};
2828

2929
function init({
3030
tenant: newTenant = {
@@ -42,6 +42,11 @@ function init({
4242
url: '',
4343
},
4444
},
45+
vars: newVars = {
46+
userId: 'username',
47+
role: 'role',
48+
commissions: 'commissions',
49+
},
4550
handler: newHandler = null
4651
}) {
4752
tenant = {
@@ -60,6 +65,12 @@ function init({
6065
},
6166
...newTenant
6267
};
68+
vars = {
69+
userId: 'username',
70+
role: 'role',
71+
commissions: 'commissions',
72+
...newVars
73+
},
6374
returnHandler = newHandler;
6475
};
6576

@@ -68,13 +79,15 @@ function activate(isOn = true) {
6879
if (!on) user = {};
6980
};
7081

71-
function setUser(newUser = {}) {
72-
if (!newUser.id) return user = {};
73-
user = newUser;
74-
};
82+
// function setUser(newUser = {}) {
83+
// if (!newUser.id) return user = {};
84+
// user = newUser;
85+
// };
7586

7687
app.get('/', async (req, res) => {
88+
console.log('Session:', req.session);
7789
if (!on) return res.render('off', { tenant, title: 'Activation - ' });
90+
if (!req.session) return res.render('session', { tenant, title: 'Session - ' });
7891
if (!tenant.slug || !tenant.name || !tenant.domain) return res.render('tenant', { tenant, title: 'Configuration - ' });
7992
if (tenant.auth && tenant.auth.enabled && !user.id) return res.render('auth', { tenant, title: 'Authenticate - ' });
8093
res.send(tenant.slug);

0 commit comments

Comments
 (0)