Wednesday 24 May 2017

TypeError using the jwt-express package

i just tried to work with the JWT-Express package for NodeJS in order to create a JWT tocken when a user wants to log in. Unfortunately, I receive a TypeError with the package.

Following my code:

var express = require('express');
var app = express();

var jwt = require('jwt-express');
app.use(jwt.init('secret'));

app.get('/login', function(req, res) {
    var user = {
        first: "Firstname",
        last: "Lastname",
        is_admin: true
    };

    // we are using cookies so the JWT is 
    // automatically stored for us
    var jwt = res.jwt({
        admin: user.is_admin,
        name: user.first + ' ' + user.last
    });

    // we now have access to the JWT Object
    console.log(jwt);

    // if we weren't using cookies, we could
    // now send the token to the client
    res.send(jwt.token);
});

app.listen(8080);

I verified that both the object jwt in line 3 and the call to jwt.init returns something unequal to undefined. The error seems to stick somehow in the package internally. The system crashes even before the GET requests is being processed.

The error is:

TypeError: Cannot read property 'jwt-express' of undefined
    at Object.<anonymous> (/Users/stuckenholz/auth/node_modules/jwt-express/jwt-express.js:218:36)
    at Layer.handle [as handle_request] (/Users/stuckenholz/auth/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/stuckenholz/auth/node_modules/express/lib/router/index.js:317:13)
    at /Users/stuckenholz/auth/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/stuckenholz/auth/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/stuckenholz/auth/node_modules/express/lib/router/index.js:275:10)
    at expressInit (/Users/stuckenholz/auth/node_modules/express/lib/middleware/init.js:40:5)
    at Layer.handle [as handle_request] (/Users/stuckenholz/auth/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/stuckenholz/auth/node_modules/express/lib/router/index.js:317:13)
    at /Users/stuckenholz/auth/node_modules/express/lib/router/index.js:284:7

Any ideas?

Best regards...



via LosWochos

No comments:

Post a Comment