express-sweet-generator 1.0.9 → 1.0.11

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/CHANGELOG.md CHANGED
@@ -1,6 +1,84 @@
1
1
  # Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [1.0.11] - 2023/7/12
5
+ ### Changed
6
+ - Moved the Ajax determination option (is_ajax) in the authentication configuration (config/authentication) to the basic configuration (config/config).
7
+
8
+ config/config.js:
9
+ ```js
10
+ /**
11
+ * How to determine if it is an ajax request.
12
+ * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
13
+ * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
14
+ *
15
+ * @type {(req: express.Request) => boolean}
16
+ * @example
17
+ * is_ajax: req => {
18
+ * // If the request URL begins with /api, it is assumed to be Ajax.
19
+ * return /^\/api/.test(req.path);
20
+ * // return !!req.xhr;
21
+ * }
22
+ */
23
+ is_ajax: req => !!req.xhr,
24
+ ```
25
+ - The error handle option (error_handler) in the basic configuration (config/config.js) has been removed and an option to hook error handles (hook_handle_error) added instead.
26
+
27
+ config/config.js:
28
+ ```js
29
+ /**
30
+ * Hooks the default behavior on request errors.
31
+ * If unset, simply returns an error HTTP status. (<code>res.status(err.status||500).end();</code>)
32
+ *
33
+ * @type {(err: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
34
+ * @example
35
+ * hook_handle_error: (err, req, res, next) => {
36
+ * if (err.status === 404)
37
+ * // If the URL cannot be found, a 404 error screen (views/error-404.hbs) is displayed.
38
+ * res.render('error-404');
39
+ * else
40
+ * // For other errors, unknown error screen (views/error-unknown.hbs) is displayed.
41
+ * res.render('error-unknown');
42
+ * },
43
+ */
44
+ hook_handle_error: undefined,
45
+ ```
46
+
47
+ ## [1.0.10] - 2023/7/11
48
+ ### Changed
49
+ - The URL to redirect to when login fails (failure_redirect) option in the authentication configuration (config/authentication.js) can now be defined with a function.
50
+
51
+ config/authentication.js:
52
+ ```js
53
+ // Set the URL to redirect to in case of login failure as a string.
54
+ failure_redirect: '/login',
55
+
56
+ // Dynamically set the url to redirect to on login failure.
57
+ failure_redirect: (req, res) => {
58
+ // If the role stored in the cookie is admin, redirect to the admin login screen.
59
+ return req.cookies.role === 'admin' ? '/adminlogin' : 'login';
60
+ },
61
+ ```
62
+ - The arguments of the failureRedirect method of the authentication service class (services/Authentication) have changed.
63
+ The argument to the Authentication.failureRedirect method used to be just express.Response, but now it requires express.Request and express.Response.
64
+
65
+ Example of login routes:
66
+ ```js
67
+ import {Router} from 'express';
68
+ import * as sweet from 'express-sweet';
69
+ const router = Router();
70
+ const Authentication = sweet.services.Authentication;
71
+
72
+ router.post('/login', async (req, res, next) => {
73
+ const isAuth = await Authentication.authenticate(req, res, next);
74
+ if (isAuth)
75
+ Authentication.successRedirect(res);
76
+ else
77
+ Authentication.failureRedirect(req, res);
78
+ });
79
+ export default router;
80
+ ```
81
+
4
82
  ## [1.0.9] - 2023/6/29
5
83
  ### Changed
6
84
  - Changed helper function names in the view (Handlebars) from SnakeCase to CamelCase.
@@ -161,4 +239,6 @@ All notable changes to this project will be documented in this file.
161
239
  [1.0.6]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.5...v1.0.6
162
240
  [1.0.7]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.6...v1.0.7
163
241
  [1.0.8]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.7...v1.0.8
164
- [1.0.9]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.8...v1.0.9
242
+ [1.0.9]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.8...v1.0.9
243
+ [1.0.10]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.9...v1.0.10
244
+ [1.0.11]: https://github.com/takuya-motoshima/express-sweet-generator/compare/v1.0.10...v1.0.11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-sweet-generator",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Express Sweet application generator.",
5
5
  "main": "bin/express-sweet-cli.js",
6
6
  "files": [
@@ -45,7 +45,18 @@ module.exports = {
45
45
 
46
46
  /**
47
47
  * URL to redirect after log off, defaults to `/login`.
48
- * @type {string}
48
+ *
49
+ * @example
50
+ * // Set the URL to redirect to in case of login failure as a string.
51
+ * failure_redirect: '/login',
52
+ *
53
+ * // Dynamically set the url to redirect to on login failure.
54
+ * failure_redirect: (req, res) => {
55
+ * // If the role stored in the cookie is admin, redirect to the admin login screen.
56
+ * return req.cookies.role === 'admin' ? '/adminlogin' : 'login';
57
+ * },
58
+ *
59
+ * @type {string|((req: express.Request, res: express.Response) => string)}
49
60
  */
50
61
  failure_redirect: '/login',
51
62
 
@@ -95,19 +106,4 @@ module.exports = {
95
106
  * @type {number}
96
107
  */
97
108
  expiration: 24 * 3600000,
98
-
99
- /**
100
- * How to determine if it is an ajax request.
101
- * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
102
- * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
103
- *
104
- * @type {(req: express.Request) => boolean}
105
- * @example
106
- * is_ajax: req => {
107
- * // If the request URL begins with /api, it is assumed to be Ajax.
108
- * return /^\/api/.test(req.path);
109
- * // return !!req.xhr;
110
- * }
111
- */
112
- is_ajax: req => !!req.xhr
113
109
  }
@@ -51,28 +51,34 @@ module.exports = {
51
51
  },
52
52
 
53
53
  /**
54
- * This is a hook for error handling.
55
- * For example, you can use it when you want to send an external notification of the error received by this hook.
56
- *
54
+ * How to determine if it is an ajax request.
55
+ * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
56
+ * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
57
+ *
58
+ * @type {(req: express.Request) => boolean}
59
+ * @example
60
+ * is_ajax: req => {
61
+ * // If the request URL begins with /api, it is assumed to be Ajax.
62
+ * return /^\/api/.test(req.path);
63
+ * // return !!req.xhr;
64
+ * }
65
+ */
66
+ is_ajax: req => !!req.xhr,
67
+
68
+ /**
69
+ * Hooks the default behavior on request errors.
70
+ * If unset, simply returns an error HTTP status. (<code>res.status(err.status||500).end();</code>)
71
+ *
72
+ * @type {(err: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
57
73
  * @example
58
- * error_handler: async err => {
59
- * // Notify system administrator of error.
60
- * return new Promise((resolve, reject) => {
61
- * const sendmail = require('sendmail')();
62
- * sendmail({
63
- * from: 'no-reply@example.com',
64
- * to: 'administrator@example.com',
65
- * subject: 'Error occurred',
66
- * text: err.message
67
- * }, err => {
68
- * err ? reject(err) : resolve();
69
- * });
70
- * });
71
- * }
72
- *
73
- * @type {(err: any): void|Promise<void>}
74
+ * hook_handle_error: (err, req, res, next) => {
75
+ * if (err.status === 404)
76
+ * // If the URL cannot be found, a 404 error screen (views/error-404.hbs) is displayed.
77
+ * res.render('error-404');
78
+ * else
79
+ * // For other errors, unknown error screen (views/error-unknown.hbs) is displayed.
80
+ * res.render('error-unknown');
81
+ * },
74
82
  */
75
- error_handler: err => {
76
- console.error(`An error has occurred. Error message: ${err.message}`);
77
- }
83
+ hook_handle_error: undefined,
78
84
  }
@@ -64,7 +64,7 @@
64
64
  <!--begin::User menu-->
65
65
  <div class="app-navbar-item ms-1 ms-lg-3" id="kt_header_user_menu_toggle">
66
66
  <!--begin::Menu wrapper-->
67
- <div class="cursor-pointer symbol symbol-35px symbol-md-40px" data-kt-menu-trigger="click" data-kt-menu-attach="parent" data-kt-menu-placement="bottom-end">
67
+ <div class="cursor-pointer symbol symbol-35px symbol-md-40px bg-dark" data-kt-menu-trigger="click" data-kt-menu-attach="parent" data-kt-menu-placement="bottom-end">
68
68
  <img src="{{session.icon}}?{{formatDate 'x' ''}}" alt="user" />
69
69
  {{!-- <img src="/build/theme/media/avatars/blank.png" alt="user" /> --}}
70
70
  </div>
@@ -15,7 +15,6 @@ export default class extends Modal {
15
15
  let user;
16
16
  if (this.#mode === 'update')
17
17
  user = (await this.#userApi.getUser(this.#userId)).data;
18
- console.log('user=', user);
19
18
  const node = this.render(user);
20
19
  const instance = new bootstrap.Modal(node);
21
20
  this.#ref = selectRef(node);
@@ -45,7 +45,18 @@ export default {
45
45
 
46
46
  /**
47
47
  * URL to redirect after log off, defaults to `/login`.
48
- * @type {string}
48
+ *
49
+ * @example
50
+ * // Set the URL to redirect to in case of login failure as a string.
51
+ * failure_redirect: '/login',
52
+ *
53
+ * // Dynamically set the url to redirect to on login failure.
54
+ * failure_redirect: (req, res) => {
55
+ * // If the role stored in the cookie is admin, redirect to the admin login screen.
56
+ * return req.cookies.role === 'admin' ? '/adminlogin' : 'login';
57
+ * },
58
+ *
59
+ * @type {string|((req: express.Request, res: express.Response) => string)}
49
60
  */
50
61
  failure_redirect: '/login',
51
62
 
@@ -95,19 +106,4 @@ export default {
95
106
  * @type {number}
96
107
  */
97
108
  expiration: 24 * 3600000,
98
-
99
- /**
100
- * How to determine if it is an ajax request.
101
- * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
102
- * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
103
- *
104
- * @type {(req: express.Request) => boolean}
105
- * @example
106
- * is_ajax: req => {
107
- * // If the request URL begins with /api, it is assumed to be Ajax.
108
- * return /^\/api/.test(req.path);
109
- * // return !!req.xhr;
110
- * }
111
- */
112
- is_ajax: req => !!req.xhr
113
109
  }
@@ -51,28 +51,34 @@ export default {
51
51
  },
52
52
 
53
53
  /**
54
- * This is a hook for error handling.
55
- * For example, you can use it when you want to send an external notification of the error received by this hook.
56
- *
54
+ * How to determine if it is an ajax request.
55
+ * The default is that if there is an XMLHttpRequest in the request header (req.xhr) returns true.
56
+ * For example, if there is no XMLHttpRequest in req(express.Request) and the Ajax endpoint starts with /api, a custom Ajax decision can be made like "return /^\/api\//.test(req.path)".
57
+ *
58
+ * @type {(req: express.Request) => boolean}
59
+ * @example
60
+ * is_ajax: req => {
61
+ * // If the request URL begins with /api, it is assumed to be Ajax.
62
+ * return /^\/api/.test(req.path);
63
+ * // return !!req.xhr;
64
+ * }
65
+ */
66
+ is_ajax: req => !!req.xhr,
67
+
68
+ /**
69
+ * Hooks the default behavior on request errors.
70
+ * If unset, simply returns an error HTTP status. (<code>res.status(err.status||500).end();</code>)
71
+ *
72
+ * @type {(err: any, req: express.Request, res: express.Response, next: express.NextFunction) => void}
57
73
  * @example
58
- * error_handler: async err => {
59
- * // Notify system administrator of error.
60
- * return new Promise((resolve, reject) => {
61
- * const sendmail = require('sendmail')();
62
- * sendmail({
63
- * from: 'no-reply@example.com',
64
- * to: 'administrator@example.com',
65
- * subject: 'Error occurred',
66
- * text: err.message
67
- * }, err => {
68
- * err ? reject(err) : resolve();
69
- * });
70
- * });
71
- * }
72
- *
73
- * @type {(err: any): void|Promise<void>}
74
+ * hook_handle_error: (err, req, res, next) => {
75
+ * if (err.status === 404)
76
+ * // If the URL cannot be found, a 404 error screen (views/error-404.hbs) is displayed.
77
+ * res.render('error-404');
78
+ * else
79
+ * // For other errors, unknown error screen (views/error-unknown.hbs) is displayed.
80
+ * res.render('error-unknown');
81
+ * },
74
82
  */
75
- error_handler: err => {
76
- console.error(`An error has occurred. Error message: ${err.message}`);
77
- }
83
+ hook_handle_error: undefined,
78
84
  }
@@ -64,7 +64,7 @@
64
64
  <!--begin::User menu-->
65
65
  <div class="app-navbar-item ms-1 ms-lg-3" id="kt_header_user_menu_toggle">
66
66
  <!--begin::Menu wrapper-->
67
- <div class="cursor-pointer symbol symbol-35px symbol-md-40px" data-kt-menu-trigger="click" data-kt-menu-attach="parent" data-kt-menu-placement="bottom-end">
67
+ <div class="cursor-pointer symbol symbol-35px symbol-md-40px bg-dark" data-kt-menu-trigger="click" data-kt-menu-attach="parent" data-kt-menu-placement="bottom-end">
68
68
  <img src="{{session.icon}}?{{formatDate 'x' ''}}" alt="user" />
69
69
  {{!-- <img src="/build/theme/media/avatars/blank.png" alt="user" /> --}}
70
70
  </div>