Skip to content

CSRF token with express 4 #2

@picsoung

Description

@picsoung

I faced a TypeError: Cannot read property 'csrf_tokens' of undefined error when I followed your tutorial. From my understanding it's coming from the latest version of expresse that does not include those middleware by default.

My suggested workaround

npm install csurf cookie-parser express-session

Add:

var csrf = require('csurf');
var cookieParser = require('cookie-parser');
var session = require('express-session')

app.use(cookieParser());
app.use(session({secret: 'keyboard cat'}))

app.use(csrf());
app.use(function(req, res, next) {
  res.cookie('XSRF-TOKEN', req.csrfToken());
  next();
});

and it should be fine 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions