Modify context before main generation


#1

Is there any way to modify Context before auth provider running?
I want to modify the auth param of the Context and I should do it before auth param generation.

How can I do this?


#2

Hey @behnamazimi! :wave:

Create a global middleware that runs directly after AuthInit.


#3

As I said, I want to modify ctx.auth. Even when I add my middleware to the global list, it not effect my route at first request and I should resend the request to see the changes.

Here is what I do in my middleware:

  async handle({request, response, auth}, next) {
    
    Config.set('auth.jwt.model', 'Custom/Model')
    
    await next()
  }

But the ``ctx.auth``` generate before global middlewares too!
Thanks for your reply, @romain.lanz


#4

In your code example you are not touching the context at all.

You need to mutate directly the auth instance you’ve extracted from the actual context.

async handle({ auth }, next) {
  auth.jwt.model = 'Custom/Model'
    
  await next()
}

#5

I faced this,

TypeError

Cannot set property ‘model’ of undefined


#6

Because auth.jwt is undefined, it doesn’t exist in the auth context.

What do you exactly want to do?
It seems from your first code example that you want to modify the configuration on the fly of your application.


#7

All that I want to do is generate two separate JWT token for Admin and User model and handle access for two model.

I thought that A possible way to do is changing the authenticator.model to the target model in the middleware, but it does not affect mu ctx.auth at first.

Update:
I changed the authenticator with Config.set() method.