0

I'm having trouble logging into the server to do the authentication tests for my JWT password token, it claims problem

no " required:jwt({"

grateful to whoever helps me enter image description here enter image description here

user side of authentication enter image description here

I tried changing the commas but the problem persisted I believe it is something deeper

3 Answers3

0
const { expressjwt: jwt } = require("express-jwt");
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Nov 02 '22 at 08:09
0

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.

radar155
  • 1,796
  • 2
  • 11
  • 28
  • a new problem now on server side again i get this error in terminal throw new Error(msg); ^ Error: Route.put() requires a callback function but got a [object Undefined] – João Victor Nov 02 '22 at 00:57
  • in the users.js part it is like this router.post("/login", usuarioController.login); router.post("/registrar", usuarioController.store); router.put("/", auth.required, usuarioController.update); router.delete("/", auth.required, usuarioController.remove); – João Victor Nov 02 '22 at 00:59
  • There should be a problem with your `usuarioController.update` method, but I can't see the code looking at your question. Please edit and include the code. Or maybe, better, create a new quesion. – radar155 Nov 02 '22 at 01:09
0

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}`))
Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90