forest-express-mongoose icon indicating copy to clipboard operation
forest-express-mongoose copied to clipboard

Smart Ations fields hooks endpoint cannot authenticate

Open dejanpzg opened this issue 2 years ago • 5 comments

Expected behavior

When I Create an action

    {
      name: 'Remove Video',
      type: 'single',
      httpMethod: 'DELETE',
      endpoint: '/api/forest/page-contents/playlists/videos',
      download: false,
      fields: [
        {
          field: 'video',
          isRequired: true,
          type: 'Enum',
          enums: [],
        },
      ],
      hooks: {
        load: ({ fields, request }) => {
          return fields;
        },
      },
    }

I expect endpoint POST 'http://localhost:3000/api/forest/page-contents/playlists/videos/hooks/load' to be called and authentication completed and my endpoint to be called that returns a list of fields

  @Post('page-contents/playlists/videos')
  removePlaylistVideos(@Body() request: ForestRequestDto<PlaylistContentDto>): any {
    return {
      fields: [
        {
          field: 'video',
          type: 'Enum',
          enums: ['Video 1', 'Video 2'],
          position: 0,
          defaultValue: null,
          isRequired: false,
          description: null,
          reference: null,
          widget: null,
          value: null,
        },
      ],
    };
  }

Actual behavior

Current behavior is that it calls my endpoint 'http://localhost:3000/api/forest/page-contents/playlists/videos/hooks/load' but authentication fails with message Unauthorized: Forest cannot authenticate the user for this request. For some reason it doesn't have request.user and authentication fails even though all headers are sent as expected.

Smart action that is passing: image

And hook that is not passing: image

Failure Logs

[Nest] 11268 - 23. 10. 2023. 19:45:42 ERROR [ExceptionsHandler] Forest cannot authenticate the user for this request.
Unauthorized: Forest cannot authenticate the user for this request.
at ensureAuthenticated (\node_modules\forest-express\dist\services\auth.js:16:17)
at dispatch (\node_modules\compose-middleware\src\index.ts:70:21)
at middleware (\node_modules\compose-middleware\src\index.ts:85:12)
at \node_modules\compose-middleware\src\index.ts:19:45
at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
at next (\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5)
at \node_modules\express\lib\router\index.js:284:15
at Function.process_params (\node_modules\express\lib\router\index.js:346:12)
at next (\node_modules\express\lib\router\index.js:280:10)
at requestUnflattener (\node_modules\forest-express-mongoose\dist\services\flattener.js:253:16) at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (\node_modules\express\lib\router\index.js:328:13) at \node_modules\express\lib\router\index.js:286:9 at Function.process_params (\node_modules\express\lib\router\index.js:346:12) at next (\node_modules\express\lib\router\index.js:280:10) at expressInit (\node_modules\express\lib\middleware\init.js:40:5) at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (\node_modules\express\lib\router\index.js:328:13) at \node_modules\express\lib\router\index.js:286:9 at Function.process_params (\node_modules\express\lib\router\index.js:346:12)

Context

"@nestjs/common": "^10.0.2",
"@nestjs/core": "^10.0.2",
"@nestjs/event-emitter": "^2.0.2",
"@nestjs/mongoose": "^10.0.1",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/serve-static": "^4.0.0",
  • Package Version: 9.3.11
  • Express Version: 4.18.2
  • Mongoose Version: 7.5.4
  • MongoDB Version: 5.8.1

dejanpzg avatar Oct 23 '23 17:10 dejanpzg

Hello, Can you share your route declaration please?

Scra3 avatar Oct 24 '23 08:10 Scra3

I don't have route declarations, this is all I have regarding the config

app.use(await forest.init({ configDir: 'forest-config', envSecret: process.env.FOREST_ENV_SECRET, authSecret: process.env.FOREST_AUTH_SECRET, objectMapping: mongoose, connections: connections, }));

dejanpzg avatar Oct 24 '23 08:10 dejanpzg

Do you follow this documentation to declare your smart action ?

Scra3 avatar Oct 24 '23 09:10 Scra3

I'm using it with nestjs so not really. Just the part defining collection actions, not the part with router

dejanpzg avatar Oct 24 '23 09:10 dejanpzg

You must use the forestadmin middleware on the routes to be authenticated.

Scra3 avatar Oct 24 '23 09:10 Scra3