Core: Cannot find custom created Middleware: Cannot find module 'Adonis/Middleware/NoAuth'

Created on 16 Sep 2016  路  7Comments  路  Source: adonisjs/core

Hello,

I created a new middleware for pages where the user can't be authenticated named NoAuth.js

class NoAuth {

  * handle (request, response, next) {
    // here goes your middleware logic
    // yield next to pass the request to next middleware or controlle
    console.log('NoAuth')
    yield next
  }

}
module.exports = NoAuth

And added the middleware in the namedMiddlewares variable in the kernel.js file.

const namedMiddleware = {
  noAuth: 'Adonis/Middleware/NoAuth',
  auth: 'Adonis/Middleware/Auth'
}

And when I add the Middleware to a route, I gives met the following error:
Cannot find module 'Adonis/Middleware/NoAuth

And I've added the middleware as follow:

Route.group('non-auth-routes', () => {

    Route.get('/login', 'AuthController.index').as('login-page')
    Route.post('/login', 'AuthController.login').as('login')

    Route.get('/register', 'AuthController.registerPage').as('register-page')
    Route.post('/register', 'AuthController.register').as('register')

}).middleware('noAuth')

The Auth middleware is working properly but the noAuth middleware doesn't.

Thanks in advance.

Theo

Most helpful comment

@RomainLanz Thanks. It works now.

All 7 comments

It has to be noAuth: 'App/Middleware/NoAuth'. Application specific middleware makes use of Autoloading namespace which is defined inside package.json file.

{
  "autoload": {
      "App": "./app"
   }
}

The examples of autoloading are use('App/Model/User'), use('App/Listeners/Http'), etc.

After changing the namedMiddleware to

const namedMiddleware = {
  auth: 'Adonis/Middleware/Auth',
  noAuth: 'App/Middleware/NoAuth'
}

is gives me the following error: Cannot find module '/Applications/MAMP/htdocs/School/jaar3/rocket-adonis/app/Middleware/NoAuth'

@theobouwman

Your file is on
app/Http/Middleware/NoAuth.js
or
app/Middleware/NoAuth.js?

The default path is the fisrt one.

I've just tested here and It's working :)

//app/Http/kernel.js
const namedMiddleware = {
  auth: 'Adonis/Middleware/Auth',
  noAuth: 'App/Http/Middleware/NoAuth' //Check your file Path With or Without "Http"?
}
//app/Http/routes.js
// http://localhost:3333/pages/myabout (working and logging NoAuth to console)
Route.group('pages-no-auth', () => {
  Route.get('/myabout', function * (request, response) {
    response.send('You are looking at the about page :)')
  })
})
.prefix('/pages')
.middleware('noAuth')

Hi @theobouwman !

It's because you are using the wrong namespace. Think about where the file is from the root of your application.

You should use App/Http/Middleware/NoAuth.

@RomainLanz Thanks. It works now.

Closing since it has been fixed

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aligoren picture aligoren  路  4Comments

navdeepsingh picture navdeepsingh  路  3Comments

ghost picture ghost  路  3Comments

imperez picture imperez  路  4Comments

Extarys picture Extarys  路  4Comments