session-flash 1.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.
- package/LICENSE +20 -0
- package/README.md +60 -0
- package/lib/index.js +56 -0
- package/package.json +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
(The MIT License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2012–2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# session-flash
|
|
2
|
+
|
|
3
|
+
The flash is a special area of the session used for storing messages. Messages
|
|
4
|
+
are written to the flash and cleared after being displayed to the user. The
|
|
5
|
+
flash is typically used in combination with redirects, ensuring that the message
|
|
6
|
+
is available to the next page that is to be rendered.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
npm i session-flash
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
#### Express 5.x
|
|
16
|
+
|
|
17
|
+
Flash messages are stored in the session. First, setup sessions as usual by
|
|
18
|
+
enabling `cookieParser` and `session` middleware. Then, use `flash` middleware
|
|
19
|
+
provided by session-flash.
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
const app = express();
|
|
23
|
+
const cookieParser = require('cookie-parser');
|
|
24
|
+
const session = require('express-session');
|
|
25
|
+
const flash = require('session-flash');
|
|
26
|
+
|
|
27
|
+
app.use(cookieParser());
|
|
28
|
+
|
|
29
|
+
app.use(session({
|
|
30
|
+
secret: 'session-secret',
|
|
31
|
+
resave: false,
|
|
32
|
+
saveUninitialized: false,
|
|
33
|
+
cookie: { maxAge: 60000 }
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
app.use(flash());
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
With the `flash` middleware in place, all requests will have a `req.flash()` function
|
|
41
|
+
that can be used for flash messages.
|
|
42
|
+
|
|
43
|
+
```javascript
|
|
44
|
+
app.get('/flash', function(req, res){
|
|
45
|
+
// Set a flash message by passing the key, followed by the value, to req.flash().
|
|
46
|
+
req.flash('info', 'Flash is back!')
|
|
47
|
+
res.redirect('/');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
app.get('/', function(req, res){
|
|
51
|
+
// Get an array of flash messages by passing the key to req.flash()
|
|
52
|
+
res.render('index', { messages: req.flash('info') });
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## License
|
|
57
|
+
|
|
58
|
+
[The MIT License]
|
|
59
|
+
|
|
60
|
+
Copyright (c) 2012-2026
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { format } = require('node:util');
|
|
4
|
+
|
|
5
|
+
module.exports = function flash(options = {}) {
|
|
6
|
+
const safe = options.unsafe === undefined ? true : !options.unsafe;
|
|
7
|
+
|
|
8
|
+
return function flashMiddleware(req, res, next) {
|
|
9
|
+
if (req.flash && safe) return next();
|
|
10
|
+
|
|
11
|
+
Object.defineProperty(req, 'flash', {
|
|
12
|
+
configurable: true,
|
|
13
|
+
enumerable: false,
|
|
14
|
+
writable: false,
|
|
15
|
+
value: flashHandler.bind(req)
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
next();
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function flashHandler(type, msg) {
|
|
23
|
+
if (!this.session) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
'req.flash() requires session middleware (express-session)'
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const store = (this.session.flash ??= Object.create(null));
|
|
30
|
+
|
|
31
|
+
// ADD message(s)
|
|
32
|
+
if (type && arguments.length > 1) {
|
|
33
|
+
if (arguments.length > 2) {
|
|
34
|
+
msg = format(...Array.prototype.slice.call(arguments, 1));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (Array.isArray(msg)) {
|
|
38
|
+
store[type] = store[type] || [];
|
|
39
|
+
store[type].push(...msg);
|
|
40
|
+
return store[type].length;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return (store[type] = store[type] || []).push(msg);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// GET messages of a type
|
|
47
|
+
if (type) {
|
|
48
|
+
const messages = store[type] || [];
|
|
49
|
+
delete store[type];
|
|
50
|
+
return messages;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// GET all messages
|
|
54
|
+
this.session.flash = Object.create(null);
|
|
55
|
+
return store;
|
|
56
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "session-flash",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Flash message middleware for express.",
|
|
5
|
+
"keywords": ["session-flash", "express", "flash", "messages"],
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Andrew Khabweri",
|
|
8
|
+
"email": "andrewkhabweri@gmail.com"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"main": "./lib/index.js",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
},
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">= 18"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
}
|