Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit d59ebd88 authored by 65160394's avatar 65160394
Browse files

Project Round 7

parent 0cf237be
No related branches found
No related tags found
No related merge requests found
......@@ -43,6 +43,7 @@ exports.postRegister = async (req, res) => {
const userId = await User.create(name, email, password);
req.session.userId = userId;
req.flash('success', 'ลงทะเบียนสำเร็จ! กรุณาล็อกอินเพื่อเข้าระบบ');
res.redirect('/login');
} catch (err) {
console.error(err);
......@@ -67,6 +68,7 @@ exports.postLogin = async (req, res) => {
req.session.userId = user.email; // เก็บ email ใน session
req.session.userName = user.name; // เก็บชื่อผู้ใช้ใน session
req.flash('success', 'เข้าสู่ระบบสำเร็จ!');
res.redirect('/'); // เมื่อ login สำเร็จให้ไปหน้า home
} catch (err) {
console.error(err);
......
......@@ -164,6 +164,14 @@
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg=="
},
"node_modules/connect-flash": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/connect-flash/-/connect-flash-0.1.1.tgz",
"integrity": "sha512-2rcfELQt/ZMP+SM/pG8PyhJRaLKp+6Hk2IUBNkEit09X+vwn3QsAL3ZbYtxUn7NVPzbMTSLRDhqe0B/eh30RYA==",
"engines": {
"node": ">= 0.4.0"
}
},
"node_modules/content-disposition": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
......
language: "node_js"
node_js:
- 0.4
- 0.6
- 0.8
\ No newline at end of file
(The MIT License)
Copyright (c) 2012-2013 Jared Hanson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# connect-flash
The flash is a special area of the session used for storing messages. Messages
are written to the flash and cleared after being displayed to the user. The
flash is typically used in combination with redirects, ensuring that the message
is available to the next page that is to be rendered.
This middleware was extracted from [Express](http://expressjs.com/) 2.x, after
Express 3.x removed direct support for the flash. connect-flash brings this
functionality back to Express 3.x, as well as any other middleware-compatible
framework or application. +1 for [radical reusability](http://substack.net/posts/b96642/the-node-js-aesthetic).
## Install
$ npm install connect-flash
## Usage
#### Express 3.x
Flash messages are stored in the session. First, setup sessions as usual by
enabling `cookieParser` and `session` middleware. Then, use `flash` middleware
provided by connect-flash.
```javascript
var flash = require('connect-flash');
var app = express();
app.configure(function() {
app.use(express.cookieParser('keyboard cat'));
app.use(express.session({ cookie: { maxAge: 60000 }}));
app.use(flash());
});
```
With the `flash` middleware in place, all requests will have a `req.flash()` function
that can be used for flash messages.
```javascript
app.get('/flash', function(req, res){
// Set a flash message by passing the key, followed by the value, to req.flash().
req.flash('info', 'Flash is back!')
res.redirect('/');
});
app.get('/', function(req, res){
// Get an array of flash messages by passing the key to req.flash()
res.render('index', { messages: req.flash('info') });
});
```
## Examples
For an example using connect-flash in an Express 3.x app, refer to the [express3](https://github.com/jaredhanson/connect-flash/tree/master/examples/express3)
example.
## Tests
$ npm install --dev
$ make test
[![Build Status](https://secure.travis-ci.org/jaredhanson/connect-flash.png)](http://travis-ci.org/jaredhanson/connect-flash)
## Credits
- [Jared Hanson](http://github.com/jaredhanson)
- [TJ Holowaychuk](https://github.com/visionmedia)
## License
[The MIT License](http://opensource.org/licenses/MIT)
Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)>
/**
* Module dependencies.
*/
var format = require('util').format;
var isArray = require('util').isArray;
/**
* Expose `flash()` function on requests.
*
* @return {Function}
* @api public
*/
module.exports = function flash(options) {
options = options || {};
var safe = (options.unsafe === undefined) ? true : !options.unsafe;
return function(req, res, next) {
if (req.flash && safe) { return next(); }
req.flash = _flash;
next();
}
}
/**
* Queue flash `msg` of the given `type`.
*
* Examples:
*
* req.flash('info', 'email sent');
* req.flash('error', 'email delivery failed');
* req.flash('info', 'email re-sent');
* // => 2
*
* req.flash('info');
* // => ['email sent', 'email re-sent']
*
* req.flash('info');
* // => []
*
* req.flash();
* // => { error: ['email delivery failed'], info: [] }
*
* Formatting:
*
* Flash notifications also support arbitrary formatting support.
* For example you may pass variable arguments to `req.flash()`
* and use the %s specifier to be replaced by the associated argument:
*
* req.flash('info', 'email has been sent to %s.', userName);
*
* Formatting uses `util.format()`, which is available on Node 0.6+.
*
* @param {String} type
* @param {String} msg
* @return {Array|Object|Number}
* @api public
*/
function _flash(type, msg) {
if (this.session === undefined) throw Error('req.flash() requires sessions');
var msgs = this.session.flash = this.session.flash || {};
if (type && msg) {
// util.format is available in Node.js 0.6+
if (arguments.length > 2 && format) {
var args = Array.prototype.slice.call(arguments, 1);
msg = format.apply(undefined, args);
} else if (isArray(msg)) {
msg.forEach(function(val){
(msgs[type] = msgs[type] || []).push(val);
});
return msgs[type].length;
}
return (msgs[type] = msgs[type] || []).push(msg);
} else if (type) {
var arr = msgs[type];
delete msgs[type];
return arr || [];
} else {
this.session.flash = {};
return msgs;
}
}
/**
* Expose middleware.
*/
exports = module.exports = require('./flash');
{
"name": "connect-flash",
"version": "0.1.1",
"description": "Flash message middleware for Connect.",
"keywords": ["connect", "express", "flash", "messages"],
"repository": {
"type": "git",
"url": "git://github.com/jaredhanson/connect-flash.git"
},
"bugs": {
"url": "http://github.com/jaredhanson/connect-flash/issues"
},
"author": {
"name": "Jared Hanson",
"email": "jaredhanson@gmail.com",
"url": "http://www.jaredhanson.net/"
},
"licenses": [ {
"type": "MIT",
"url": "http://www.opensource.org/licenses/MIT"
} ],
"main": "./lib",
"dependencies": {
},
"devDependencies": {
"vows": "0.6.x"
},
"scripts": {
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js"
},
"engines": { "node": ">= 0.4.0" }
}
......@@ -11,6 +11,7 @@
"dependencies": {
"bcryptjs": "^3.0.2",
"body-parser": "^1.20.3",
"connect-flash": "^0.1.1",
"dotenv": "^16.4.7",
"ejs": "^3.1.10",
"express": "^4.21.2",
......@@ -178,6 +179,14 @@
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg=="
},
"node_modules/connect-flash": {
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/connect-flash/-/connect-flash-0.1.1.tgz",
"integrity": "sha512-2rcfELQt/ZMP+SM/pG8PyhJRaLKp+6Hk2IUBNkEit09X+vwn3QsAL3ZbYtxUn7NVPzbMTSLRDhqe0B/eh30RYA==",
"engines": {
"node": ">= 0.4.0"
}
},
"node_modules/content-disposition": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
......
......@@ -12,6 +12,7 @@
"dependencies": {
"bcryptjs": "^3.0.2",
"body-parser": "^1.20.3",
"connect-flash": "^0.1.1",
"dotenv": "^16.4.7",
"ejs": "^3.1.10",
"express": "^4.21.2",
......
......@@ -3,6 +3,7 @@ const bodyParser = require('body-parser');
const dotenv = require('dotenv');
const tourRoutes = require('./routes/tourRoutes');
const session = require('express-session');
const flash = require('connect-flash');
dotenv.config();
const app = express();
......@@ -19,7 +20,7 @@ app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(flash());
app.use('/', tourRoutes);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment