@@ -6,6 +6,8 @@ const cors = require("cors")
6
6
const authConfig = require ( "@iConfigs/auth" )
7
7
const security = require ( "@iApp/http/middleware/security.mid" )
8
8
const csrf = require ( "@knfs-tech/csrf" ) ;
9
+ const session = require ( "express-session" ) ;
10
+ const flash = require ( "express-flash" ) ;
9
11
10
12
exports . middleware = {
11
13
common : {
@@ -28,8 +30,29 @@ exports.middleware = {
28
30
*/
29
31
bodyParser . urlencoded ( { limit : '2mb' , parameterLimit : 100 , extended : true } ) ,
30
32
bodyParser . json ( { limit : '2mb' } ) ,
31
- cookieParser ( ) ,
33
+ /**
34
+ * Session Handle
35
+ */
36
+ session ( authConfig . session ) ,
37
+ cookieParser ( authConfig . cookie . secret ) ,
32
38
security ( ) ,
39
+ /**
40
+ * CSRF handle
41
+ */
42
+ csrf . generate ( {
43
+ tokenLength : 24 ,
44
+ protectCondition : ( req ) => {
45
+ return req . method === 'POST' || req . method === 'PUT' || req . method === 'PATCH' || req . method === 'DELETE'
46
+ } ,
47
+ storage : {
48
+ type : csrf . CONSTANT . STORAGE . COOKIE ,
49
+ options : {
50
+ httpOnly : true ,
51
+ maxAge : 1 * 24 * 60 * 60 * 1000 , // 1days
52
+ secure : process . env . NODE_ENV === 'production' ? true : false
53
+ }
54
+ }
55
+ } ) ,
33
56
] ,
34
57
after : [
35
58
/**
@@ -39,9 +62,7 @@ exports.middleware = {
39
62
* ***********************************************
40
63
*/
41
64
/**
42
- * **********************************
43
65
* Error handle
44
- * **********************************
45
66
*/
46
67
async ( err , req , res , next ) => {
47
68
if ( process . env . NODE_ENV != "development" ) {
@@ -73,25 +94,17 @@ exports.middleware = {
73
94
req . reqType = 'web'
74
95
next ( ) ;
75
96
} ,
97
+ /**
98
+ * ****************************
99
+ * FLASH SESSION
100
+ * ****************************
101
+ */
102
+ flash ( ) ,
76
103
/**
77
104
* ****************************
78
105
* CSRF
79
106
* ****************************
80
107
*/
81
- csrf . generate ( {
82
- tokenLength : 24 ,
83
- protectCondition : ( req ) => {
84
- return req . method === 'GET' || req . method === 'POST' || req . method === 'PUT' || req . method === 'DELETE'
85
- } ,
86
- storage : {
87
- type : csrf . CONSTANT . STORAGE . COOKIE ,
88
- options : {
89
- httpOnly : true ,
90
- maxAge : 1 * 24 * 60 * 60 * 1000 , // 1days
91
- secure : process . env . NODE_ENV === 'production' ? true : false
92
- }
93
- }
94
- } ) ,
95
108
csrf . setTokenLocalsParam ,
96
109
/**
97
110
* ****************************
0 commit comments