Server middleware is calling before the controller function

I need to save in user_id the user_id that create one record.

Actually i make this middleware:

'use strict'

class Auditory{
  async handle ({ request, auth }, next) {
    console.log('inside the auditory')
    const userLogged= await auth.getUser()
    const idUser=

    request.user_id = idUser

    await next()

module.exports = Auditory

I put one console.log() inside the Auditory class and one console.log() in the method store of my controller:

    async store ({ request }){

        let user = request.all()
        user = await User.create(user)
        return user


It’s printing in this order:

{ username: 'dsadasdas',
  password: 'safasfasf',
  status: false,
  permission: 'Administrador',
  created_at: '2019-10-31T14:03:33.543Z',
  updated_at: '2019-10-31T14:03:33.543Z' }
inside the auditory

so, the server middleware is calling the method handle before the store method, i need the reverse to save the user_id.

I’m forgetting something?

I declared my middleware in server middleware:

const serverMiddleware = [
  // 'Adonis/Middleware/Static',
1 Like

Can u look

Section Upstream & Downstream Middleware speaks about when execute middleres. If you put your code before “await next()” middleware executes before route loading and if u put middleware after middleware executes after controller execution.