I'm having trouble logging into the server to do the authentication tests for my JWT password token, it claims problem
no " required:jwt({"
I tried changing the commas but the problem persisted I believe it is something deeper
I'm having trouble logging into the server to do the authentication tests for my JWT password token, it claims problem
no " required:jwt({"
I tried changing the commas but the problem persisted I believe it is something deeper
const { expressjwt: jwt } = require("express-jwt");
You should require the library with a different syntax, reading documentation:
const { expressjwt: jwt } = require("express-jwt")
Also, it looks that the algorithms
property is a required option. Therefore, on line 20 and 25 of your code, you want to write something like:
jwt({secret:'secret', algorithms: ["HS256"], userProperty: 'payload', getToken: getTokenFromHeader)
The above example just works. Of course you should choose the right strategy and the right algorithm for a strong authentication.
I would suggest to use the the following package. nmicro-router
Using this package, you can configure custom validation for your HTTP request. Also you can define your authentication methods. It has an adapter for JWT authentication, whose link is given below. nmicro-jwt-auth
Also you can validate request parameters.
An example with request validations and JWT authentication is given below.
import express from 'express'
import { JWTAuth } from 'nmicro-jwt-auth'
import { FastestValidatorAdapter } from 'nmicro-fastest-validator'
import {Router} from 'nmicro-router'
const app = express()
app.use(express.json())
const myRouter = new Router(express.Router())
const JWT_VALIDATION_SECRET = `my-jwt-secret`
myRouter.authHandler.setAdapter(new JWTAuth())
myRouter.validator.setAdapter(new FastestValidatorAdapter())
app.use(myRouter.router)
const validations = { name: {optional: false, type: "string", min: 4, max: 50} }
const myFn = (req, res) => res.send(`Hurray! It is working`)
const options = { validations, auth: true }
myRouter.post('/test', myFn, options)
const port = 8081
app.listen(port, () => console.log(`App listening in port ${port}`))