Passing JWT on server FeathersJS 5

38 views Asked by At

when my server starts, I need to fetch data from external API and then save them into my database. This works well, but I have little problem. My function for saving

import { EventFetcher } from './eventFetcher.js'

const eventFetcher = new EventFetcher()

export const reloadEvents = async () => {
  const newEventData = await eventFetcher.fetchEvents()
  console.log('Aktualizujem udalosti v db...')
  eventFetcher.updateLastFetchedData(newEventData)

  const response = await fetch('http://localhost:3030/reservations-ms', {
    method: 'POST'
  })

  if (response.ok) {
    console.log('Udalosti v databáze aktualizované!')
  } else {
    console.error('Chyba pri ukladaní udalostí.')
  }
}

It makes POST request to my local endpoint, but it doesn't work after I added authentication for this route, because I dont have JWT stored on serverside.

app.service(reservationsMsPath).hooks({
    around: {
      all: [
        authenticate('jwt'),
        schemaHooks.resolveExternal(reservationsMsExternalResolver),
        schemaHooks.resolveResult(reservationsMsResolver)
}

So my question is, do I need somehow to get token here, or are there any others ways to do this?

I thought something like this would work

import { EventFetcher } from './eventFetcher.js'
import { ReservationsMsService, getOptions } from '../services/reservations-ms/reservations-ms.class.js'

const eventFetcher = new EventFetcher()
const reservationsService = new ReservationsMsService()

export const reloadEvents = async () => {
  const newEventData = await eventFetcher.fetchEvents()
  console.log('Aktualizujem udalosti v db...')
  eventFetcher.updateLastFetchedData(newEventData)

  try {
    const response = await reservationsService.post(newEventData)

    if (response.ok) {
      console.log('Udalosti v databáze aktualizované!')
    } else {
      console.error('Chyba pri ukladaní udalostí.')
    }
  } catch (error) {
    console.error('An error occurred while sending the POST request:', error)
  }
}

But now it says

Error: You must provide a Model (the initialized knex object)

1

There are 1 answers

0
Daff On BEST ANSWER

You probably don't need to make a request to the server but instead either import the app object directly from the app file or register a setup hook in the app file that does the initialisation that you need:

const eventFetcher = new EventFetcher()

app.hooks({
  setup: [
    async (context: HookContext, next: NextFunction) => {
      // E.g. wait for MongoDB connection to complete
      const newEventData = await eventFetcher.fetchEvents()
      const reservationsService = context.app.service('reservations')

      console.log('Aktualizujem udalosti v db...')
      eventFetcher.updateLastFetchedData(newEventData)
    
      try {
        const reservation = await reservationsService.create(newEventData)
    
        console.log(reservation)
      } catch (error) {
        console.error('An error occurred while sending the POST request:', error)
      }

      await next()
    }
  ]
})