From d59ebd8857362d61d2d70fc59b6329b5d19c78f2 Mon Sep 17 00:00:00 2001
From: Atiwit Pattanapukdee <65160394@go.buu.ac.th>
Date: Tue, 18 Mar 2025 23:59:09 +0700
Subject: [PATCH] Project Round 7

---
 controllers/tourController.js           |  2 +
 node_modules/.package-lock.json         |  8 +++
 node_modules/connect-flash/.travis.yml  |  6 ++
 node_modules/connect-flash/LICENSE      | 20 ++++++
 node_modules/connect-flash/README.md    | 73 ++++++++++++++++++++++
 node_modules/connect-flash/lib/flash.js | 82 +++++++++++++++++++++++++
 node_modules/connect-flash/lib/index.js |  4 ++
 node_modules/connect-flash/package.json | 32 ++++++++++
 package-lock.json                       |  9 +++
 package.json                            |  1 +
 server.js                               |  3 +-
 11 files changed, 239 insertions(+), 1 deletion(-)
 create mode 100644 node_modules/connect-flash/.travis.yml
 create mode 100644 node_modules/connect-flash/LICENSE
 create mode 100644 node_modules/connect-flash/README.md
 create mode 100644 node_modules/connect-flash/lib/flash.js
 create mode 100644 node_modules/connect-flash/lib/index.js
 create mode 100644 node_modules/connect-flash/package.json

diff --git a/controllers/tourController.js b/controllers/tourController.js
index 3579002..fe5436e 100644
--- a/controllers/tourController.js
+++ b/controllers/tourController.js
@@ -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);
diff --git a/node_modules/.package-lock.json b/node_modules/.package-lock.json
index 3fa9fde..9dad202 100644
--- a/node_modules/.package-lock.json
+++ b/node_modules/.package-lock.json
@@ -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",
diff --git a/node_modules/connect-flash/.travis.yml b/node_modules/connect-flash/.travis.yml
new file mode 100644
index 0000000..79eed11
--- /dev/null
+++ b/node_modules/connect-flash/.travis.yml
@@ -0,0 +1,6 @@
+language: "node_js"
+node_js:
+  - 0.4
+  - 0.6
+  - 0.8
+ 
\ No newline at end of file
diff --git a/node_modules/connect-flash/LICENSE b/node_modules/connect-flash/LICENSE
new file mode 100644
index 0000000..d61567a
--- /dev/null
+++ b/node_modules/connect-flash/LICENSE
@@ -0,0 +1,20 @@
+(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.
diff --git a/node_modules/connect-flash/README.md b/node_modules/connect-flash/README.md
new file mode 100644
index 0000000..b3b58b4
--- /dev/null
+++ b/node_modules/connect-flash/README.md
@@ -0,0 +1,73 @@
+# 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/)>
diff --git a/node_modules/connect-flash/lib/flash.js b/node_modules/connect-flash/lib/flash.js
new file mode 100644
index 0000000..a278bc1
--- /dev/null
+++ b/node_modules/connect-flash/lib/flash.js
@@ -0,0 +1,82 @@
+/**
+ * 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;
+  }
+}
diff --git a/node_modules/connect-flash/lib/index.js b/node_modules/connect-flash/lib/index.js
new file mode 100644
index 0000000..b4d5961
--- /dev/null
+++ b/node_modules/connect-flash/lib/index.js
@@ -0,0 +1,4 @@
+/**
+ * Expose middleware.
+ */
+exports = module.exports = require('./flash');
diff --git a/node_modules/connect-flash/package.json b/node_modules/connect-flash/package.json
new file mode 100644
index 0000000..bc46300
--- /dev/null
+++ b/node_modules/connect-flash/package.json
@@ -0,0 +1,32 @@
+{
+  "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" }
+}
diff --git a/package-lock.json b/package-lock.json
index bd0d0ff..0424e4a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -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",
diff --git a/package.json b/package.json
index 815f81f..7effb26 100644
--- a/package.json
+++ b/package.json
@@ -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",
diff --git a/server.js b/server.js
index 8eb963e..62adeda 100644
--- a/server.js
+++ b/server.js
@@ -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);
-- 
GitLab