Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if (!options.algorithms) throw new Error('algorithms should be set'); Error: algorithms should be set

I started learning Nodejs and i am stuck somewhere in the middle. I installed a new library from npm and that was express-jwt, its showing some kind of error after running. Attached the code and the logs of the error, please help me out!

const jwt = require('jsonwebtoken');
require('dotenv').config()
const expressJwt =  require('express-jwt');
const User = require('../models/user');




exports.requireSignin =  expressJwt({ secret:  process.env.JWT_SECRET});

The below thing is the logs of the error.

[nodemon] starting `node app.js`
D:\shubh\proj\Nodejs\nodeapi\node_modules\express-jwt\lib\index.js:22
  if (!options.algorithms) throw new Error('algorithms should be set');
                           ^

**Error: algorithms should be set**
    at module.exports (D:\shubh\proj\Nodejs\nodeapi\node_modules\express-jwt\lib\index.js:22:34)
    at Object.<anonymous> (D:\shubh\proj\Nodejs\nodeapi\controllers\auth.js:64:26)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
 
like image 915
Shubham gupta Avatar asked Jun 30 '20 20:06

Shubham gupta


3 Answers

You should add algorithms property to the jwt constructor.

Example;

expressJwt({ secret:  process.env.JWT_SECRET, algorithms: ['RS256'] });
like image 65
Tugay İlik Avatar answered Nov 20 '22 06:11

Tugay İlik


The issue caused by changes in version 6.0.0. Documentation also has been updated recently, it says:

The algorithms parameter is required to prevent potential downgrade attacks when providing third party libraries as secrets.

So now specifying algorithm property is mandatory, like so:

expressJwt({
  secret: 'secret',
  algorithms: ['HS256']
})
like image 30
Igor Rybak Avatar answered Nov 20 '22 07:11

Igor Rybak


if the above algorithm : ['RS256'] does not work try this, algorithms: ['HS256']

like image 4
Chirag Kushwaha Avatar answered Nov 20 '22 06:11

Chirag Kushwaha