Skip to content

Commit 0969fbe

Browse files
author
khanh2906
committed
fix error response error csrf invalid
1 parent 5f7a4b8 commit 0969fbe

File tree

5 files changed

+10
-12
lines changed

5 files changed

+10
-12
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,17 @@ app.use(csrf.generate({
154154

155155
- Default errorResponse is
156156
```javascript
157-
errorResponse: (req, res, next) => {
158-
return res.status(403).send('CSRF token invalid');
157+
errorResponse: (req, res) => {
158+
res.status(403).send('CSRF token invalid');
159159
}
160160
```
161161

162162
- When you custom
163163

164164
```javascript
165165
// when you custom
166-
const newErrorResponse = (req, res, next) => {
167-
return res.status(403).render('<h1>CSRF token invalid</h1>');
166+
const newErrorResponse = (req, res) => {
167+
res.status(403).render('<h1>CSRF token invalid</h1>');
168168
}
169169
app.use(csrf.generate({
170170
errorResponse: newErrorResponse

lib/cjs/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ let csrf = {
8484
getTransmitToken: (req) => {
8585
return req.body._csrf || req.headers['csrf-token'];
8686
},
87-
errorResponse: (req, res, next) => {
88-
return res.status(403).send('CSRF token invalid');
87+
errorResponse: (req, res) => {
88+
res.status(403).send('CSRF token invalid');
8989
}
9090
}
9191

@@ -155,7 +155,7 @@ module.exports = {
155155
const token = csrf.getTransmitToken(req);
156156

157157
if (!token || token !== csrf.getToken(req)) {
158-
return csrf.errorResponse(req, res, next)
158+
return csrf.errorResponse(req, res)
159159
} else {
160160
console.info("DELETE CSRF TOKEN: ", token)
161161
csrf.clearToken(req, res)

lib/esm/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ let csrf = {
8484
getTransmitToken: (req) => {
8585
return req.body._csrf || req.headers['csrf-token'];
8686
},
87-
errorResponse: (req, res, next) => {
88-
return res.status(403).send('CSRF token invalid');
87+
errorResponse: (req, res) => {
88+
res.status(403).send('CSRF token invalid');
8989
}
9090
}
9191

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@knfs-tech/csrf",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"description": "Cross-site request forgery module",
55
"main": "./lib/cjs/index.js",
66
"module": "./lib/esm/index.js",

tests/units/csrf.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ describe('CSRF Middleware', () => {
7373

7474
csrfMiddleware.protect(req, res, next);
7575

76-
expect(next).toHaveBeenCalled();
7776
expect(res.status).not.toHaveBeenCalled();
7877
});
7978

@@ -84,7 +83,6 @@ describe('CSRF Middleware', () => {
8483

8584
csrfMiddleware.protect(req, res, next);
8685

87-
expect(next).not.toHaveBeenCalled();
8886
expect(res.status).toHaveBeenCalledWith(403);
8987
expect(res.send).toHaveBeenCalledWith('CSRF token invalid');
9088
});

0 commit comments

Comments
 (0)